<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # JSP ## JSP指令 新建一個jsp文件,最上面一行就是jsp指令 ~~~ <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> ~~~ JSP指令(directive)是為JSP引擎而設計的,它們并不直接產生任何可見輸出,而只是告訴引擎如何處理JSP頁面中的其余部分。 JSP指令的基本語法格式: <%@ 指令 屬性名="值" %> 舉例:<%@ page contentType="text/html;charset=gb2312"%> 注意:屬性名部分是大小寫敏感的 在目前的JSP 2.0中,定義了page、include和taglib這三種指令,每種指令中又都定義了一些各自的屬性。 如果要在一個JSP頁面中設置同一條指令的多個屬性,可以使用多條指令語句單獨設置每個屬性,也可以使用同一條指令語句設置該指令的多個屬性。 第一種方式: <%@ page contentType="text/html;charset=gb2312"%> <%@ page import="java.util.Date"%> 第二種方式: <%@ page contentType="text/html;charset=gb2312" import="java.util.Date"%> *** ### Page指令 ~~~ JSP 2.0規范中定義的page指令的完整語法: <%@ page // jsp支持的語言類型 [ language="java" ] // jsp翻譯成Servlet可以繼承哪些類 [ extends="package.class" ] // 導包 [ import="{package.class | package.*}, ..." ] // 當前頁面是否允許使用session [ session="true | false" ] [ buffer="none | 8kb | sizekb" ] [ autoFlush="true | false" ] [ isThreadSafe="true | false" ] [ info="text" ] [ errorPage="relative_url" ] [ isErrorPage="true | false" ] // 指定當前jsp頁面的響應類型 [ contentType="mimeType [ ;charset=characterSet ]" | "text/html ; charset=ISO-8859-1" ] // 指定當前jsp頁面的字符編碼 [ pageEncoding="characterSet | ISO-8859-1" ] // 指定當前jsp頁面是否可以使用EL表達式,通常取值為true [ isELIgnored="true | false" ] %> ~~~ errorPage和isErrorPage ~~~ <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" errorPage="/error.jsp" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <% int i = 10 / 0; %> </body> </html> ~~~ error.jsp ~~~ <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isErrorPage="true"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <h1>ErrorPage</h1> <%= exception.getMessage() %> </body> </html> ~~~ 一般來說,這樣的錯誤頁面不建議直接訪問,那么如何處理能夠達到這樣的效果。 對于Tomcat服務器而言,WEB-INF下的文件是不能通過在瀏覽器中直接輸入URL來直接訪問的(即將error.jsp剪切到WEB-INF),但通過請求的轉發是可以的,所以可以這么處理一下 ~~~ <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" errorPage="/WEB-INF/error.jsp" %> ~~~ *** 在web.xml也可以配置錯誤頁面(jsp中的errorPage刪除) ~~~ <error-page> <!-- 404:沒有指定資源;500,內部錯誤 --> <error-code>500</error-code> <location>/WEB-INF/error.jsp</location> </error-page> ~~~ 或 ~~~ <error-page> <exception-type>java.lang.ArithmeticException</exception-type> <location>/WEB-INF/error.jsp</location> </error-page> ~~~ *** ### include指令 ~~~ <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <h1>A Page</h1> <!-- 在a.jsp中包含b.jsp,file后的資源寫的是相對路徑,如果有/代表是wen應用根目錄 --> <%@include file="b.jsp" %> </body> </html> ~~~ 靜態包含,也稱為源碼級包含,只有一個.java源文件 *** ### JSP標簽 ①jsp:include ~~~ <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <h1>A Page</h1> <!-- 在a.jsp中包含b.jsp --> <%-- <%@include file="b.jsp" %> --%> <jsp:include page="b.jsp"></jsp:include> </body> </html> ~~~ <jsp:include page="b.jsp"></jsp:include>與include指令不太一樣,是動態包含,存在兩個.java源文件 ②jsp:forward a.jsp ~~~ <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <jsp:forward page="/b.jsp"> <jsp:param value="test" name="username"/> </jsp:forward><jsp:forward page="/b.jsp"></jsp:forward> </body> </html> ~~~ b.jsp ~~~ <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <h1>Page 222</h1> <%= request.getParameter("username") %> </body> </html> ~~~ 相當于request.getRequestDispatcher("/b.jsp").forward(request,response);還能夠使用jsp:param傳遞一些參數,可以通過request.getParameter(String name)獲取請求參數。 *** ### 中文亂碼問題 ①在JSP頁面上輸入中文,請求頁面后不出現亂碼: ~~~ <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> ~~~ 以及瀏覽器顯示的字符編碼與請求的jsp字符編碼一致 ②獲取請求參數中存在中文(默認字符編碼為ISO-8859-1) a.jsp ~~~ <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <form action="b.jsp" method="post"> username:<input type="text" name="username"> <input type="submit" value="Submit"> </form> </body> </html> ~~~ b.jsp ~~~ <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <h1>Page 222</h1> <%= request.getParameter("username") %> </body> </html> ~~~ 在a.jsp提交表單后出現亂碼 ![](https://box.kancloud.cn/b60e14f86da56a63b45f40c16a9c1eed_382x255.png) 解決方式 ~~~ <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <h1>Page 222</h1> // 對于post請求,在獲取請求參數之前,指定請求參數的字符編碼為UTF-8,即可解決中文亂碼 <% request.setCharacterEncoding("UTF-8"); %> <%= request.getParameter("username") %> </body> </html> ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看