放在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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。