JavaWeb学习笔记(十三)--JSP入门和基本原理
作者:互联网
1. 什么是JSP
JSP全称是Java Server Pages,它和Servlet技术一样,都是SUN公司定义的一种用于开发动态web资源的技术。
JSP这门技术最大的特点:
- 写JSP就像在写HTML,相比HTML而言,HTML只能为用户提供静态数据,而JSP允许在页面中嵌套Java代码,为用户提供动态数据。
- 相比Servlet而言,Servlet很难对数据进行排版,而JSP除了可以用Java代码产生动态数据的同时,也很容易对数据进行排版。
2. JSP原理
2.1 Web服务器如何调用并执行一个JSP页面
浏览器向服务器发送请求时,无论访问的是什么资源,都要先访问一个Servlet。当浏览器访问一个JSP页面时,服务器先将JSP翻译成一个Servlet,当我们访问JSP页面时,实际上是访问翻译后的Servlet,例如:
<%-- index.jsp --%> <%@ page import="java.util.Date" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>JSP原理</title> </head> <body> <% Date date = new Date(); out.write(date.toLocaleString()); %> </body> </html>
在C:\Users\用户名\.IntelliJIdea2018.3\system\tomcat\Tomcat_8_5_37_jsp(这个是工程名)\work\Catalina\localhost\ROOT\org\apache\jsp目录可以找到翻译后的Servlet,即index_jsp.java。
Mac对应jsp翻译的Servlet的目录/Users/用户名/Library/Caches/IntelliJIdea2018.3/tomcat/工程名/work/Catalina/localhost/ROOT/org/apache/jsp
/* * Generated by the Jasper component of Apache Tomcat * Version: Apache Tomcat/8.5.37 * Generated at: 2019-05-18 11:44:46 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.Date; 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<java.lang.String,java.lang.Long> _jspx_dependants; private static final java.util.Set<java.lang.String> _jspx_imports_packages; private static final java.util.Set<java.lang.String> _jspx_imports_classes; static { _jspx_imports_packages = new java.util.HashSet<>(); _jspx_imports_packages.add("javax.servlet"); _jspx_imports_packages.add("javax.servlet.http"); _jspx_imports_packages.add("javax.servlet.jsp"); _jspx_imports_classes = new java.util.HashSet<>(); _jspx_imports_classes.add("java.util.Date"); } 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 java.util.Set<java.lang.String> getPackageImports() { return _jspx_imports_packages; } public java.util.Set<java.lang.String> getClassImports() { return _jspx_imports_classes; } 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 { 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("\n"); out.write("\n"); out.write("\n"); out.write("<html>\n"); out.write(" <head>\n"); out.write(" <title>JSP原理</title>\n"); out.write(" </head>\n"); out.write(" <body>\n"); out.write(" "); Date date = new Date(); out.write(date.toLocaleString()); out.write("\n"); out.write(" </body>\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); } } }
翻译后的Java类index_jsp继承HttpJspBase,查看Tomcat的API文档, 可以看到HttpJspBase继承HttpServlet,表明index_jsp是一个Servlet。
2.2 JSP页面中的内容是如何发送到客户端的
浏览器向服务发送请求会调用Servlet的service方法,查看翻译后的Servlet的service方法,可以看到Servlet中对于JSP中的HTML标签,直接使用out.write()向浏览器写数据,而JSP中的Java代码,则原封不动的挪到Servlet中:
out.write("\n"); out.write("\n"); out.write("\n"); out.write("<html>\n"); out.write(" <head>\n"); out.write(" <title>JSP原理</title>\n"); out.write(" </head>\n"); out.write(" <body>\n"); out.write(" "); Date date = new Date(); out.write(date.toLocaleString()); out.write("\n"); out.write(" </body>\n"); out.write("</html>");
2.3 Web服务器在调用JSP时,会给JSP提供哪些Java对象
从service方法中可以看到一共定义了8个java对象(其实还有一个exception,后面会说):
HttpServletRequest request HttpServletResponse response PageContext pageContext HttpSession session ServletContext application ServletConfig config JspWriter out Object page // 当前页面
由于这几个对象是在service调用时就创建的,所以在JSP中可以直接使用。
2.4 JSP最佳实践
最佳实践:一个技术可以用在多个地方,但是只有一个地方用是最好的,就叫最佳实践。
不管是JSP还是Servlet,虽然都可以用于开发动态web资源,但是由于这两门技术各自的特点,在长期的软件实践中,人们逐渐把Servlet作为web应用中的控制器来使用,而把JSP作为显示模板来使用。其原因是:程序的数据通常要美化后在输出,让JSP既用Java代码产生动态数据,又做美化,会导致页面难以维护。让Servlet既产生数据,又在里面嵌套HTML标签美化数据,同样会导致程序的可读性差,难以维护。因此最好的方法就是根据这两门技术的特点,让他们各自负责各自的,Servlet只负责响应请求,产生数据,并把数据通过转发技术传递给JSP,JSP用来显示数据。
标签:JavaWeb,入门,write,jsp,jspx,JSP,javax,out 来源: https://www.cnblogs.com/songchj-bear/p/10886978.html