Jsp回顾:
Jsp页面运行原理:当一个用户请求一个JSP页面时,Tomcat服务器自动生成Java文件,编译Java文件,并用编译得到的字节码文件在服务器端创建一个对象来响应用户的请求。JSP的根基是Java Servlet技术
1 /* 2 * Generated by the Jasper component of Apache Tomcat 3 * Version: Apache Tomcat/8.5.46 4 * Generated at: 2019-10-15 08:56:20 UTC 5 * Note: The last modified time of this file was set to 6 * the last modified time of the source file after 7 * generation to assist with modification tracking. 8 */ 9 package org.apache.jsp; 10 11 import javax.servlet.*; 12 import javax.servlet.http.*; 13 import javax.servlet.jsp.*; 14 15 public final class index_jsp extends org.apache.jasper.runtime.HttpJspBase 16 implements org.apache.jasper.runtime.JspSourceDependent, 17 org.apache.jasper.runtime.JspSourceImports { 18 19 private static final javax.servlet.jsp.JspFactory _jspxFactory = 20 javax.servlet.jsp.JspFactory.getDefaultFactory(); 21 22 private static java.util.Map<java.lang.String,java.lang.Long> _jspx_dependants; 23 24 private static final java.util.Set<java.lang.String> _jspx_imports_packages; 25 26 private static final java.util.Set<java.lang.String> _jspx_imports_classes; 27 28 static { 29 _jspx_imports_packages = new java.util.HashSet<>(); 30 _jspx_imports_packages.add("javax.servlet"); 31 _jspx_imports_packages.add("javax.servlet.http"); 32 _jspx_imports_packages.add("javax.servlet.jsp"); 33 _jspx_imports_classes = null; 34 } 35 36 private volatile javax.el.ExpressionFactory _el_expressionfactory; 37 private volatile org.apache.tomcat.InstanceManager _jsp_instancemanager; 38 39 public java.util.Map<java.lang.String,java.lang.Long> getDependants() { 40 return _jspx_dependants; 41 } 42 43 public java.util.Set<java.lang.String> getPackageImports() { 44 return _jspx_imports_packages; 45 } 46 47 public java.util.Set<java.lang.String> getClassImports() { 48 return _jspx_imports_classes; 49 } 50 51 public javax.el.ExpressionFactory _jsp_getExpressionFactory() { 52 if (_el_expressionfactory == null) { 53 synchronized (this) { 54 if (_el_expressionfactory == null) { 55 _el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory(); 56 } 57 } 58 } 59 return _el_expressionfactory; 60 } 61 62 public org.apache.tomcat.InstanceManager _jsp_getInstanceManager() { 63 if (_jsp_instancemanager == null) { 64 synchronized (this) { 65 if (_jsp_instancemanager == null) { 66 _jsp_instancemanager = org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager(getServletConfig()); 67 } 68 } 69 } 70 return _jsp_instancemanager; 71 } 72 73 public void _jspInit() { 74 } 75 76 public void _jspDestroy() { 77 } 78 79 public void _jspService(final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response) 80 throws java.io.IOException, javax.servlet.ServletException { 81 82 final java.lang.String _jspx_method = request.getMethod(); 83 if (!"GET".equals(_jspx_method) && !"POST".equals(_jspx_method) && !"HEAD".equals(_jspx_method) && !javax.servlet.DispatcherType.ERROR.equals(request.getDispatcherType())) { 84 response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, "JSPs only permit GET POST or HEAD"); 85 return; 86 } 87 88 final javax.servlet.jsp.PageContext pageContext; 89 javax.servlet.http.HttpSession session = null; 90 final javax.servlet.ServletContext application; 91 final javax.servlet.ServletConfig config; 92 javax.servlet.jsp.JspWriter out = null; 93 final java.lang.Object page = this; 94 javax.servlet.jsp.JspWriter _jspx_out = null; 95 javax.servlet.jsp.PageContext _jspx_page_context = null; 96 97 98 try { 99 response.setContentType("text/html"); 100 pageContext = _jspxFactory.getPageContext(this, request, response, 101 null, true, 8192, true); 102 _jspx_page_context = pageContext; 103 application = pageContext.getServletContext(); 104 config = pageContext.getServletConfig(); 105 session = pageContext.getSession(); 106 out = pageContext.getOut(); 107 _jspx_out = out; 108 109 out.write("This is JSP test"); 110 } catch (java.lang.Throwable t) { 111 if (!(t instanceof javax.servlet.jsp.SkipPageException)){ 112 out = _jspx_out; 113 if (out != null && out.getBufferSize() != 0) 114 try { 115 if (response.isCommitted()) { 116 out.flush(); 117 } else { 118 out.clearBuffer(); 119 } 120 } catch (java.io.IOException e) {} 121 if (_jspx_page_context != null) _jspx_page_context.handlePageException(t); 122 else throw new ServletException(t); 123 } 124 } finally { 125 _jspxFactory.releasePageContext(_jspx_page_context); 126 } 127 } 128 }
Servlet
Java Servlet核心思想是在服务器端创建能够相应用户请求的对象,即创建servlet对象。
1.编写Servlet类
创建servlet对象的类就是编写一个特殊类的子类,这个特殊的类就是 javax.servlet.http 类。HttpServlet类实现了Servlet的接口,实现了响应 用户的方法。
1 package com.alex; 2 import java.io.IOException; 3 import java.io.PrintWriter; 4 import javax.servlet.ServletException; 5 import javax.servlet.annotation.WebServlet; 6 import javax.servlet.http.HttpServlet; 7 import javax.servlet.http.HttpServletRequest; 8 import javax.servlet.http.HttpServletResponse; 9 10 @WebServlet("/hello") 11 public class Hello extends HttpServlet { 12 @Override 13 protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 14 resp.setContentType("text/html;charset = utf-8"); 15 PrintWriter out =resp.getWriter(); 16 out.println("<html><body>"); 17 out.println("<h2>更改This is Servlet</h2>"); 18 out.println("</body></html>"); 19 } 20 }
2.Servlet对象的工作原理
Servlet对象由Tomcat服务器负责管理,Tomcat服务器通过读取web.xml创建并运行servlet对象
3.Jsp页面访问servlet
- 通过表单向servlet提交数据
- 链接
- doGet和doGet方法
- 重定向与转发
Session