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

配置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也一樣,就是改一下里面顯示的字母就行了,顯示結果如下

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章