您好,登錄后才能下訂單哦!
放在web應用程序中除了WEB-INF及其子目錄之外的任何目錄下。
每個JSP頁面在第一次被訪問時,JSP引擎將它翻譯成一個Servlet源程序,接著再把這個Servlet源程序編譯成Servlet的class類文件,然后再由WEB容器(Servlet引擎)像調用普通Servlet程序一樣的方式來裝在和解釋執行這個由JSP頁面翻譯成的Servlet程序。
JSP文件翻譯成的Java文件去這個路徑下找
E:\JavaWorkSpace(eclipse工程目錄)\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\work\Catalina\localhost\day_0206\org\apache\jsp\xxx.jsp
例:hello_jsp.java文件如下
/* * Generated by the Jasper component of Apache Tomcat * Version: Apache Tomcat/7.0.75 * Generated at: 2017-02-16 02:13:19 UTC * Note: The last modified time of this file was set to * the last modified time of the source file after * generation to assist with modification tracking. */ package org.apache.jsp; import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.jsp.*; import day_0206.Person; public final class hello_jsp extends org.apache.jasper.runtime.HttpJspBase implements org.apache.jasper.runtime.JspSourceDependent { private static final javax.servlet.jsp.JspFactory _jspxFactory = javax.servlet.jsp.JspFactory.getDefaultFactory(); private static java.util.Map<java.lang.String,java.lang.Long> _jspx_dependants; private volatile javax.el.ExpressionFactory _el_expressionfactory; private volatile org.apache.tomcat.InstanceManager _jsp_instancemanager; public java.util.Map<java.lang.String,java.lang.Long> getDependants() { return _jspx_dependants; } public javax.el.ExpressionFactory _jsp_getExpressionFactory() { if (_el_expressionfactory == null) { synchronized (this) { if (_el_expressionfactory == null) { _el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory(); } } } return _el_expressionfactory; } public org.apache.tomcat.InstanceManager _jsp_getInstanceManager() { if (_jsp_instancemanager == null) { synchronized (this) { if (_jsp_instancemanager == null) { _jsp_instancemanager = org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager(getServletConfig()); } } } return _jsp_instancemanager; } public void _jspInit() { } public void _jspDestroy() { } public void _jspService(final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response) throws java.io.IOException, javax.servlet.ServletException { //隱含對象在這里,還有一個叫exception的隱含對象 final javax.servlet.jsp.PageContext pageContext; javax.servlet.http.HttpSession session = null; final javax.servlet.ServletContext application; final javax.servlet.ServletConfig config; javax.servlet.jsp.JspWriter out = null; final java.lang.Object page = this; javax.servlet.jsp.JspWriter _jspx_out = null; javax.servlet.jsp.PageContext _jspx_page_context = null; try { response.setContentType("text/html; charset=ISO-8859-1"); pageContext = _jspxFactory.getPageContext(this, request, response, null, true, 8192, true); _jspx_page_context = pageContext; application = pageContext.getServletContext(); config = pageContext.getServletConfig(); session = pageContext.getSession(); out = pageContext.getOut(); _jspx_out = out; out.write("\r\n"); out.write("\r\n"); out.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\r\n"); out.write("<html>\r\n"); out.write("<head>\r\n"); out.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\">\r\n"); out.write("<title>Insert title here</title>\r\n"); out.write("</head>\r\n"); out.write("<body>\r\n"); out.write("\r\n"); //在JSP頁面中使用<% %>編寫的代碼在這個位置 Person person = new Person(); System.out.println(person.getInfo()); out.write("\r\n"); out.write("</body>\r\n"); out.write("</html>"); } catch (java.lang.Throwable t) { if (!(t instanceof javax.servlet.jsp.SkipPageException)){ out = _jspx_out; if (out != null && out.getBufferSize() != 0) try { if (response.isCommitted()) { out.flush(); } else { out.clearBuffer(); } } catch (java.io.IOException e) {} if (_jspx_page_context != null) _jspx_page_context.handlePageException(t); else throw new ServletException(t); } } finally { _jspxFactory.releasePageContext(_jspx_page_context); } } }
JSP頁面的隱含變量:沒有聲明就可以使用的對象,有request, response, pageContext, session, application, config,out, page, exception
request:HttpServletRequest的對象
response:HttpServletResponse的一個對象(在JSP頁面中幾乎不會調用response的任何方法)
pageContext:頁面的上下文,是PageContext的一個對象,可以從該對象中獲取到其他8個隱含對象,也可以從中獲取到當前頁面的其他信息
session:代表瀏覽器和服務器的一次會話,是HttpSession的一個對象
application:代表當前web應用,是ServletContext對象
config:當前JSP對應的Servlet的ServletConfig對象。(開發的時候幾乎不用)若需要訪問當前JSP配置的初始化參數,需要通過映射的地址才可以獲取到
<servlet> <servlet-name>hellojsp</servlet-name> <jsp-file>/hello.jsp</jsp-file> <init-param> <param-name>test</param-name> <param-value>testValue</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>hellojsp</servlet-name> <url-pattern>/hellojsp</url-pattern><!-- 通過這個地址訪問才能獲取到初始化參數--> </servlet-mapping>
7.out:JspWriter對象,調用out.println()可以直接把字符串打印到瀏覽器上
8.page:指向當前JSP對應的Servlet對象的引用,但為Object類型,只能調用Object類的方法(幾乎不使用)
9.exception:聲明了<%@ page isErrorPage = "true" %>的頁面才可以使用
pageContext,request, session, application 作用域的范圍從小到大
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。