JSP stands for Java Server Pages. Like servle technology, JSP is a technology defined by SUN for developing dynamic web resources.
Origin:
In many dynamic web pages, most of the content is fixed, and only some content needs to be dynamically generated and changed. If the Servlet program is used to output webpages with only some content that needs to be dynamically changed, and all the static content also needs to be generated by programmers using Java program code, the code of the entire Servlet program will be very bloated, writing and maintenance will be very difficult.
Solution:
To make up for Servlet defects, SUN introduced the JSP (Java Server Pages) technology as a solution based on Servlet. JSP is a technology that simplifies Servlet writing. It combines Java code and HTML statements in the same file and uses Java code to write only the content to be dynamically generated on the webpage, the fixed static content is written in the form of common static HTML pages.
Compared with Html:
Html can only provide static data for users, while Jsp technology allows embedding java code in pages to provide dynamic data for users.
Compared with Servlet:
Servlet is difficult to typeset data. In addition to using java code to generate dynamic data, jsp also easily typeset data, avoiding the need to write a lot of HTML code in the servlet.
Think: Why can JSP be called dynamic web resource development technology like Servlet?
Explanation: Jsp is actually a Servlet. The following is a detailed explanation of this sentence!
Schematic diagram of JSP call process:
Execution Process introduction:
1. When the WEB Container (Servlet Engine) receives an access request from a URL with the. jsp extension, it will send the access request to the JSP Engine for processing.
2. the JSP Engine in Tomcat is a Servlet program, which is responsible for interpreting and executing JSP pages.
3. When we access Jsp for the first time, the Jsp Engine translates the Jsp into a Servlet, which is stored in the work directory of Tomcat.
4. Compile the Servlet Source program into a Servlet class file.
5. Then, the WEB Container (Servlet Engine) loads and interprets the Servlet program translated from the JSP page in the same way as calling a common Servlet program.
Sample Code:
Jsp page:
HelloWorld <%out.println("HelloWorld");%>
In the web. xml file of the conf file of tomcat
jsp
org.apache.jasper.servlet.JspServlet
fork
false
xpoweredBy
false
3
jsp
*.jsp
jsp
*.jspx
Implementation of the JSPServlet class in tomcat source code.
This class inherits HttpServlet. Override the service method of HttpServlet.
If jsp is used for the first time, compile it into a servlet. After compilation, We will generate our related file HelloWorld_jsp.java.
public void service (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String jspUri = null; String jspFile = (String) request.getAttribute(Constants.JSP_FILE); if (jspFile != null) { // JSP is specified via
in
declaration jspUri = jspFile; } else { /* * Check to see if the requested JSP has been the target of a * RequestDispatcher.include() */ jspUri = (String) request.getAttribute(Constants.INC_SERVLET_PATH); if (jspUri != null) { /* * Requested JSP has been target of * RequestDispatcher.include(). Its path is assembled from the * relevant javax.servlet.include.* request attributes */ String pathInfo = (String) request.getAttribute( "javax.servlet.include.path_info"); if (pathInfo != null) { jspUri += pathInfo; } } else { /* * Requested JSP has not been the target of a * RequestDispatcher.include(). Reconstruct its path from the * request's getServletPath() and getPathInfo() */ jspUri = request.getServletPath(); String pathInfo = request.getPathInfo(); if (pathInfo != null) { jspUri += pathInfo; } } } if (log.isDebugEnabled()) { log.debug("JspEngine --> " + jspUri); log.debug("\t ServletPath: " + request.getServletPath()); log.debug("\t PathInfo: " + request.getPathInfo()); log.debug("\t RealPath: " + context.getRealPath(jspUri)); log.debug("\t RequestURI: " + request.getRequestURI()); log.debug("\t QueryString: " + request.getQueryString()); } try { boolean precompile = preCompile(request); serviceJspFile(request, response, jspUri, null, precompile); } catch (RuntimeException e) { throw e; } catch (ServletException e) { throw e; } catch (IOException e) { throw e; } catch (Throwable e) { throw new ServletException(e); } }
Generated servlet class -- HelloWorld_jsp.java
package org.apache.jsp;import javax.servlet.*;import javax.servlet.http.*;import javax.servlet.jsp.*;public final class HelloWorld_jsp extends org.apache.jasper.runtime.HttpJspBase implements org.apache.jasper.runtime.JspSourceDependent { private static java.util.List _jspx_dependants; public Object getDependants() { return _jspx_dependants; } public void _jspService(HttpServletRequest request, HttpServletResponse response) throws java.io.IOException, ServletException { JspFactory _jspxFactory = null; 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 { _jspxFactory = JspFactory.getDefaultFactory(); response.setContentType("text/html"); 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\n"); out.write("\t\r\n"); out.write("\t\tHelloWorld\r\n"); out.write("\t\r\n"); out.write("\t\r\n"); out.write("\t\t"); out.println("HelloWorld"); out.write("\r\n"); out.write("\t\r\n"); out.write(""); } catch (Throwable t) { if (!(t instanceof SkipPageException)){ out = _jspx_out; if (out != null && out.getBufferSize() != 0) out.clearBuffer(); if (_jspx_page_context != null) _jspx_page_context.handlePageException(t); } } finally { if (_jspxFactory != null) _jspxFactory.releasePageContext(_jspx_page_context); } }}
As we can see, this class inherits from org. apache. jasper. runtime. HttpJspBase. The specific tomcat source code of the HttpJspBase class is as follows:
According to the time sequence diagram, call the service method first, and then call the _ jspService abstract method internally in the service method. This method is not implemented, so it inherits its HelloWorld_jsp.java class implementation.
package org.apache.jasper.runtime;import java.io.IOException;import javax.servlet.ServletConfig;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.jsp.HttpJspPage;import javax.servlet.jsp.JspFactory;import org.apache.jasper.compiler.Localizer;/** * This is the super class of all JSP-generated servlets. * * @author Anil K. Vijendran */public abstract class HttpJspBase extends HttpServlet implements HttpJspPage { protected HttpJspBase() { } public final void init(ServletConfig config) throws ServletException { super.init(config);jspInit(); _jspInit(); } public String getServletInfo() {return Localizer.getMessage("jsp.engine.info"); } public final void destroy() {jspDestroy();_jspDestroy(); } /** * Entry point into service. */ public final void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { _jspService(request, response); } public void jspInit() { } public void _jspInit() { } public void jspDestroy() { } protected void _jspDestroy() { } public abstract void _jspService(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException;}
If:
Making jsp generate dynamic data using java code and beautify it will lead to excessive jsp duties and difficult page maintenance.
Making servlet generate both data and embed html code inside to beautify data also results in poor program readability and difficulty in maintenance.
Summary:
Both JSP and Servlet can be used to develop dynamic web resources (learning in common ). However, due to their respective characteristics, servlet is gradually used as a controller component in web applications in long-term software practices, JSP technology can be used as a data display template (different points can also be said to give full play to their respective advantages! Among them, JSP saves programmers the process of manually splicing html code in the background ).
Therefore, the best way is to let these two technologies take responsibility for each of them. servlet is only responsible for responding to requests to generate data and bringing the data to jsp through forwarding technology, the data display jsp is used, so that the single responsibility and easy maintenance are also in line with the idea of layering and single responsibility in our software design.
In fact, the emergence or formation of various technologies and various design patterns we have learned is a reflection of the software design idea!