JSP基本构成和执行过程
JSP(Java Server Page)页面是指扩展名为 .jsp 的文件。
JSP是服务器端运行的页面,不像html文件直接就可以在浏览器中运行。JSP页面必须部署到web容器中编译成Servlet才能执行,可以说,JSP就是Servlet的一种形式。
事实上,JSP是Servlet发展的产物,关于JSP和Servlet的关系以后再在博客中介绍。
JSP基本页面由指令标签,HTML标记语言,注释,Java代码,JSP动作标签5个部分组成。
1.JSP指令标签不会产生任何内容输出到网页中,主要用于定义整个JSP页面的相关信息,如使用的语言,导入的类包,指定错误处理页面等。
JSP指令有3种分别为:
page----用于定义整个JSP页面的相关属性,这些属性在JSP被服务器解析成Servlet时会转换成相应的java代码
主要属性有language extends import pageEncoding contentType session 等
include--文件包含指令,它可以在JSP页面中包含另一个文件内容(可以是一段java代码,html代码或JSP页面)
taglib---引用标签库指令
2.HTML标记语言
3 Java代码
语法格式 <% 编写java代码%>
JSP表达式<%=表达式%>可以直接把Java表达式结果输出到JSP页面中。表达式最终结果将被转换为字符串类型。
4 注释 包括html注释JSP注释java代码注释等
5 JSP动作标签,主要用来实现特殊的功能如转发用户请求,包含其他文件 操作JavaBean等。
即是静态包含,先包含再编译成class文件(静态包含不能传递参数)。而
在JSP页面第一次被请求时,JSP引擎会将JSP原始文件转换为Servlet源代码,然后调用Java编译器,编译成Servlet,并在Servlet引擎中执行,容器将生成的页面返回给客户端显示。当再次有请求时,JSP引擎会根据JSP文件是否存在更新,如果没有更新,运行Servlet;如果存在更新,就会重新执行转换和编译的过程。JSP的执行性能和Servlet相比,差别只在第一次的执行,当重复调用时,就直接执行第一次产生的Servlet。
如下为index.jsp的一个最简单代码
<%@ page language=java import=java.util.* pageEncoding=UTF-8%> <% String path = request.getContextPath(); String basePath = request.getScheme()+://+request.getServerName()+:+request.getServerPort()+path+/; %>
发布在Tomcat中执行后生成2个文件 index_jsp.class和index_jsp.java
/* * Generated by the Jasper component of Apache Tomcat * Version: Apache Tomcat/8.0.17 * Generated at: 2015-05-18 12:09:56 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 java.util.*; public final class index_jsp extends org.apache.jasper.runtime.HttpJspBase implements org.apache.jasper.runtime.JspSourceDependent, org.apache.jasper.runtime.JspSourceImports { private static final javax.servlet.jsp.JspFactory _jspxFactory = javax.servlet.jsp.JspFactory.getDefaultFactory(); private static java.util.Map _jspx_dependants; private static final java.util.Set _jspx_imports_packages; private static final java.util.Set _jspx_imports_classes; static { _jspx_imports_packages = new java.util.HashSet<>(); _jspx_imports_packages.add(javax.servlet); _jspx_imports_packages.add(java.util); _jspx_imports_packages.add(javax.servlet.http); _jspx_imports_packages.add(javax.servlet.jsp); _jspx_imports_classes = new java.util.HashSet<>(); } private javax.el.ExpressionFactory _el_expressionfactory; private org.apache.tomcat.InstanceManager _jsp_instancemanager; public java.util.Map getDependants() { return _jspx_dependants; } public java.util.Set getPackageImports() { return _jspx_imports_packages; } public java.util.Set getClassImports() { return _jspx_imports_classes; } public void _jspInit() { //初始化 _el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory(); _jsp_instancemanager = org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager(getServletConfig()); } 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 { final java.lang.String _jspx_method = request.getMethod(); if (!GET.equals(_jspx_method) && !POST.equals(_jspx_method) && !HEAD.equals(_jspx_method) && !javax.servlet.DispatcherType.ERROR.equals(request.getDispatcherType())) { response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, JSPs only permit GET POST or HEAD); return; } 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=UTF-8); 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(' '); out.write(' '); String path = request.getContextPath(); String basePath = request.getScheme()+://+request.getServerName()+:+request.getServerPort()+path+/; out.write( ); out.write( ); out.write( ); out.write( ); out.write( ); out.write( ); out.write( ); out.write( ); out.write( ); out.write( ); out.write( ); out.write( ); out.write( ); out.write( ); out.write( ); out.write( ); out.write( ); out.write( This is my JSP page. ); out.write( ); out.write( ); } 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); } } }
Web容器处理JSP文件请求的执行过程主要包括如下4个部分
1客户端发出Request请求
2JSP容器将JSP转译成Servlet代码
3将转译的Servlet代码经过编译后,加载到内存执行
4把结果Request至客户端