JSP动态网页开拓道理详解

一、什么是JSP

     JSP全称是Java Server Pages,它和servle技能一样,都是SUN公司界说的一种用于开动员态web资源的技能。
  JSP这门技能的最大的特点在于,写jsp就像在写html,但它对比html而言,html只能为用户提供静态数据,而Jsp技能答允在页面中嵌套java代码,为用户提供动态数据。

二、JSP道理

2.1、Web处事器是如何挪用并执行一个jsp页面的?

  欣赏器向处事器发请求,不管会见的是什么资源,其实都是在会见Servlet,所以当会见一个jsp页面时,其实也是在会见一个Servlet,处事器在执行jsp的时候,首先把jsp翻译成一个Servlet,所以我们会见jsp时,其实不是在会见jsp,而是在会见jsp翻译事后的谁人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+"https://www.jb51.net/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="https://www.jb51.net/<%=basePath%>" > <title>First Jsp</title> </head> <body> <% out.print("Hello Jsp"); %> </body> </html>

  当我们通过欣赏器会见index.jsp时,处事器首先将index.jsp翻译成一个index_jsp.class,在Tomcat处事器的work\Catalina\localhost\项目名\org\apache\jsp目次下可以看到index_jsp.class的源代码文件index_jsp.java,index_jsp.java的代码如下:

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 { private static final JspFactory _jspxFactory = JspFactory.getDefaultFactory(); private static java.util.List _jspx_dependants; private javax.el.ExpressionFactory _el_expressionfactory; private org.apache.AnnotationProcessor _jsp_annotationprocessor; public Object getDependants() { return _jspx_dependants; } public void _jspInit() { _el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory(); _jsp_annotationprocessor = (org.apache.AnnotationProcessor) getServletConfig().getServletContext().getAttribute(org.apache.AnnotationProcessor.class.getName()); } public void _jspDestroy() { } 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;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('\r'); out.write('\n'); String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"https://www.jb51.net/"; out.write("\r\n"); out.write("\r\n"); out.write("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\r\n"); out.write("<html>\r\n"); out.write(" <head>\r\n"); out.write(" <base href=https://www.jb51.net/article/\""); out.print(basePath); out.write("https://www.jb51.net/article/\">\r\n"); out.write(" \r\n"); out.write(" <title>First Jsp</title>\r\n"); out.write("\t\r\n"); out.write(" </head>\r\n"); out.write(" \r\n"); out.write(" <body>\r\n"); out.write(" "); out.print("Hello Jsp"); out.write("\r\n"); out.write(" </body>\r\n"); out.write("</html>\r\n"); } 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); } } }

  我们可以看到,index_jsp这个类是担任 org.apache.jasper.runtime.HttpJspBase这个类的,通过查察Tomcat处事器的源代码,可以知道在apache-tomcat-6.0.20-src\java\org\apache\jasper\runtime目次下存HttpJspBase这个类的源代码文件,如下图所示: 

JSP动态网页开辟原理详解

我们可以看看HttpJsBase这个类的源代码,如下所示:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wsddjx.html