Understanding init, service and ServletConfig(how a servlet is initialized, the methods that get called and the objects used on initialization and execution)
There are methods which run before the doGet and doPost. 代码都可以在HttpServlet.java中查看 init(servletConfig):第一次call servlet的时候,进行servlet初始化 service():service通过判断HTTP code来调用doGet或者其他方法
if (method.equals(METHOD_GET)) { long lastModified = getLastModified(req); if (lastModified == -1) { // servlet doesn't support if-modified-since, no reason // to go through further expensive logic doGet(req, resp); } else { long ifModifiedSince; try { ifModifiedSince = req.getDateHeader(HEADER_IFMODSINCE); } catch (IllegalArgumentException iae) { // Invalid date header - proceed as if none was set ifModifiedSince = -1; } if (ifModifiedSince < (lastModified / 1000 * 1000)) { // If the servlet mod time is later, call doGet() // Round down to the nearest second for a proper compare // A ifModifiedSince of -1 will always be less maybeSetLastModified(resp, lastModified); doGet(req, resp); } else { resp.setStatus(HttpServletResponse.SC_NOT_MODIFIED); } }
/* * Generated by the Jasper component of Apache Tomcat * Version: Apache Tomcat/9.0.33 * Generated at: 2020-03-30 18:00:34 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;
out.write("\n"); out.write("<!DOCTYPE html>\n"); out.write("<html>\n"); out.write("<head>\n"); out.write("<meta charset=\"UTF-8\">\n"); out.write("<title>Test jsp</title>\n"); out.write("</head>\n"); out.write("<body>\n"); out.write("\t"); int i = 1; int j = 2; int k; k = i + j; out.println(k); out.write("\n"); out.write("\tThe value of k is: "); out.print( k ); out.write("\n"); out.write("\t\n"); out.write("\t"); out.write('\n'); out.write(' ');
int t = add(234, 123); out.write("\n"); out.write("\tt's value is: "); out.print(t ); out.write("\n"); out.write("\t\n"); out.write("\t"); for(int p = 0;p<=5;p++){ out.write("\n"); out.write("\t<br> \n"); out.write("\tThe value of p is: "); out.print(p ); out.write('\n'); out.write(' '); } 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); elsethrownew ServletException(t); } } finally { _jspxFactory.releasePageContext(_jspx_page_context); } } }
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <% String userName = request.getParameter("userName"); application.setAttribute("applicationContextName", "defaultName"); if(userName!=null){ session.setAttribute("sessionName", userName); application.setAttribute("applicationContextName", userName);//application context object pageContext.setAttribute("pageContextName", userName);//page context object } %> <br> The user name in the request object is : <%= userName %> <br> The user name in the session object is : <%= session.getAttribute("sessionName") %> <br> The user name in the application context object is : <%= application.getAttribute("applicationContextName") %> <br> The user name in the page context object is : <%= pageContext.getAttribute("pageContextName") %> </body> </html>