JSP是J2EE标准之一,和ASP.NET中的aspx作用和开发类似,这篇博客我们通过一个简单的实例,看一下JSP的部署、执行原理及生命周期等。
我们新建一个项目,按照Tomcat要求的基本文档结构,名为MyFirstJSP,在这个项目下新建一个JSP,名为HelloWorld.jsp,目的只是输出一行HelloWorld:
<html> <head> <title>login</title> </head> <body> <% out.println("HelloWorld"); %> </body> <html>
JSP的部署,不像Servlet一样繁琐,不需要修改配置文件,也不需要指定jsp的动作指向,按照上面的步骤新建完毕项目以后,打开Tomcat,输入http://localhost:8080/MyFirstJSP/HelloWorld.jsp即可访问,结果如下:
打开Tomcat根目录下conf下的web.xml,可以发现以下配置:
<!-- The mapping for the JSP servlet --> <servlet-mapping> <servlet-name>jsp</servlet-name> <url-pattern>*.jsp</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>jsp</servlet-name> <url-pattern>*.jspx</url-pattern> </servlet-mapping>现在我们把配置文件中的*.jspx修改为*.test,把我们的HelloWorld.jsp修改为HelloWorld.test,然后访问http://localhost:8080/MyFirstJSP/HelloWorld.test,执行结果为:
和上面相同,其实这个配置的意思是:把什么样的文件当做jsp来解析,此处也可以修改为其它格式。
可见,如果配置合理,拓展名对jsp执行无影响,那么jsp的执行原理是什么?我们继续看这个配置文件,可以发现如下配置:
<servlet> <servlet-name>jsp</servlet-name> <servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class> <init-param> <param-name>fork</param-name> <param-value>false</param-value> </init-param> <init-param> <param-name>xpoweredBy</param-name> <param-value>false</param-value> </init-param> <load-on-startup>3</load-on-startup> </servlet>
可见,此配置的作用是找到处理jsp的类,经过比对代码,其实就是把jsp当做Servlet处理,当然只凭这个说服力不大,我们继续往下看。
进入D:\Program Files (x86)\apache-tomcat-6.0.33\work\Catalina\localhost\MyFirstJSP\org\apache\jsp下,发现如下的文档结构:
在此处会自动生成与之对应的java文件和类文件,因为刚才我们尝试了test的文件,所以此处也生成了对应的test的java和类文件。我们打开HelloWorld_jsp.java,找到主要的函数_jspService:
<span style="font-family:SimSun;">public void _jspService(HttpServletRequest request, HttpServletResponse response) throws java.io.IOException, ServletException { PageContext pageContext = null; HttpSession session = null; ServletContext application = null; ServletConfig config = null; JspWriter out = null; Object page = this; JspWriter _jspx_out = null; PageContext _jspx_page_context = null; try { response.setContentType("text/html"); 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("<html>\r\n"); out.write("\t<head>\r\n"); out.write("\t\t<title>login</title>\r\n"); out.write("\t</head>\r\n"); out.write("\t<body>\r\n"); out.write("\t\t\t"); out.println("HelloWorld"); out.write("\r\n"); out.write("\t</body>\r\n"); out.write("<html>"); } catch (Throwable t) { if (!(t instanceof SkipPageException)){ out = _jspx_out; if (out != null && out.getBufferSize() != 0) try { out.clearBuffer(); } catch (java.io.IOException e) {} if (_jspx_page_context != null) _jspx_page_context.handlePageException(t); } } finally { _jspxFactory.releasePageContext(_jspx_page_context); } }</span>发现什么没有?这个函数的参数为HttpServletRequest和HttpServletResponse,可能的异常为ServletException;可见此处是把jsp当做了Servlet处理。
public abstract class HttpJspBase extends HttpServlet implements HttpJspPage { public final void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { _jspService(request, response); } public abstract void _jspService(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException; }
既然jsp被编译后就是一个Servlet,为什么还要有jsp?jsp的出现在Servlet之后,目的就是简化Servlet的开发和部署。
更多博客,其访问《项目总结》。