MENU

配置404、Error页面以及其他常用技巧

September 12, 2018 • Read: 3467 • JSP阅读设置

配置404、Error页面

假设你jsp项目里java代码片出现某些问题,产生异常等等之类的,网页总是显示一堆英文提示,那你就等着电话被客户打爆吧......开玩笑的,举个简单的例子,你代码片中做了一个简单的除法,但是被除数是0,运行时就会产生错误页面

<!--divide.jsp-->
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'divide.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>
  
  <body>
       <%
           int x = 1 / 0;
       %>
  </body>
</html>

还有一种错误,就是用户输入错了网页地址,本来是divide.jsp,用户不知道就输成了什么地址(永远把用户想的特别弱智,这是一个写程序的人应该做的),最后结果就是用户访问半天进不去,以为你服务器崩溃了,你电话又被打爆了......

上述两个问题,你电话都被打爆了,这应该是任何程序员都不希望发生的事情,所以,解决办法就来了,如果出现上述两个错误,我们应该修改用户看到的东西,相当于一个监听自动执行的超链接,当发生某件事的时候,立刻执行跳转到某个页面,所以我们先写两个页面,分别是404页面和代码出错的页面

<!-- page404.jsp -->
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'page404.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>
  
  <body>
    <h1>页面走失了,再逛逛</h1>
  </body>
</html>
<!-- error.jsp -->
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'Error.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>
  
  <body> 
    <h1>系统故障:联系我,12345678910</h1>
  </body>
</html>

接下来需要修改WebRoot/WEB-INF/web.xml文件,将这两个网页配置进去

<!-- web.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  <display-name></display-name>    
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  <error-page>
      <error-code>404</error-code>
      <location>/page404.jsp</location>
  </error-page>
  
  <error-page>
      <exception-type>java.lang.Exception</exception-type>
      <location>/Error.jsp</location>      
  </error-page>
  
</web-app>

最主要的是那两段error-page,照着直接写就行了,然后重启Tomcat,访问网页,就能达到想要的效果了

把网页变成一个可下载的excel表格形式

我们知道,excel文件的后缀名一般是xls或其他格式,我们要找到这个xls对应的MIME类型,通过MIME参考手册,xls对应的MIME类型是application/vnd.ms-excel,然后我们在网页中写一个表格,并且引入

<%@ page contentType = "application/vnd.ms-excel" %>

直接给出示例代码

<!-- excel.jsp -->
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ page contentType = "application/vnd.ms-excel" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'excel.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>
  
  <body>
      <table>
          <thead>
              <tr>
                  <th>学号</th><th>姓名</th>
              </tr>
          </thead>
          <tbody>
              <tr>
                  <td>1</td><td>张三</td>
              </tr>
              <tr>
                  <td>1</td><td>张三</td>
              </tr>
              <tr>
                  <td>1</td><td>张三</td>
              </tr>
              <tr>
                  <td>1</td><td>张三</td>
              </tr>
              <tr>
                  <td>1</td><td>张三</td>
              </tr>
          </tbody>
      </table>
  </body>
</html>

当你访问这个网页的时候,就会自动下载一个.jsp文件,下载下来之后把后缀名改成.xls,就变成excel表了,只不过打开以后,里面的中文是乱码,这个后面在讲如何解决,这里先会用即可

jsp:include

不知道你有没有见过这样的网站,网站有一个导航栏,点击导航栏里面的超链接,能访问到另一个页面,这个页面也有导航栏,和之前的导航栏是一模一样的,如果是你来写这个网页,你会选择同样的导航栏,所有的jsp文件都复制粘贴一遍,还是把这个导航栏样式单独写成一个jsp,让其他jsp直接"包含"进来

在写导航栏之前,先了解一下jsp:includeinclude file的用法,我先写两个网页,一个叫include.jsp,一个叫included.jsp,然后在include.jsp中将included.jsp包含进来

<!-- include.jsp -->
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'include.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>
  
  <body>
    <%@ include file = "included.jsp" %>
  </body>
</html>
<!-- included.jsp -->
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
这是被包含的页面
<% 
    int i = 0;
%>

访问include.jsp网页,就能看到网页上有一句话“这是被包含的页面”,这里注意一点,我把included.jsp中很多的内容都删了,包括String path和String basePath,必须删除,不删除会报错,这与包含的方式有关,include file这种是静态包含,也就是相当于将included.jsp里的全部内容复制到include.jsp中,然后随着include.jsp一起翻译为.class字节码,试问,你写java程序,能定义两个同名的变量吗?当然不能,所以jsp中也不能,如果有就会报错。

但是,有静态包含就有动态包含,动态包含的方式就是利用jsp:include

<!-- included.jsp -->
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'include.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>
  
  <body>
    <jsp:include page="included.jsp"></jsp:include>
  </body>
</html>

用了这种动态包含后,不管你included.jsp里有多少跟include.jsp重复的变量,都不会报错。动态包含的执行过程是分别将包含与被包含的jsp文件分别翻译成各自的字节码.class,然后调用,所以不会有冲突

最后利用bootStrap做一个导航栏(有点丑)

<!-- nav.jsp -->
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'a.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <link rel="stylesheet" type="text/css" href="ReSources/css/bootstrap.min.css">

  </head>
  
  <body>
      <ul class = "nav nav-pills nav-justified">
        <li role="presentation" class = "active"><a href = "a.jsp">a</a></li>
        <li role="presentation"><a href = "b.jsp">b</a></li>
        <li role="presentation"><a href = "c.jsp">c</a></li>
        <li role="presentation"><a href = "d.jsp">d</a></li>    
        <li role="presentation" class="disabled"><a href="">Disabled link</a></li>
        <li role="presentation" class="disabled"><a href="">Disabled link</a></li>
    </ul>
  </body>
</html>
<!-- a.jsp -->
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'a.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>
  
  <body>
      <jsp:include page = "nav.jsp"></jsp:include>
        a
  </body>
</html>
<!-- b.jsp -->
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'a.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>
  
  <body>
      <jsp:include page = "nav.jsp"></jsp:include>
        b
  </body>
</html>

c,d也一样,就是改一下里面显示的字母就行了,显示结果如下

Last Modified: October 7, 2018
Archives Tip
QR Code for this page
Tipping QR Code