Analysis of Java Web 16 JSP principle

Posted by padanaram on Sun, 26 Dec 2021 18:07:52 +0100

6.1 what is JSP

As long as html, there is more java code.

JSP: Java Server Pages (Java server-side pages, like servlets, are used to develop dynamic Web technology!)

Do you know why JSP was born? When we learn Servlet, don't we feel a problem? Ha ha~

Although it is true that the so-called dynamic development web is realized according to various characteristics and functions of Servlet. But using Servlet to write a web page is extremely difficult.


to glance at.. What are you doing...

So JSP appeared.

characteristic:

  • Writing JSP is like writing HTML
  • difference:
  1. HTML only provides users with static data.
  2. Java code can be embedded in JSP pages to provide users with dynamic data.

6.2 JSP principle

Idea:

First, how does JSP execute?

  • There are no problems at the code level (we can't see anything at all!)

  • Work inside the server (you can see it at once)

There is a work directory in Tomcat; When IDEA uses tomcat, it will also generate a work directory in Tomcat of IDEA.

  1. Find the work directory and get ready to reveal the secret!
    For example, if we find the work of the session and keep clicking under it, there will be a jsp folder. Under this folder, we will find that there is no jsp file!!! The corresponding executable file is java!!



You can see that it inherits a class. Let's see what this class is.

Enter HttpJspBase directly in the code, and then ALT + enter
Select add maven to automatically add this package.
You will be surprised to find that this class inherits servlets. Can we say that our jsp has become a java program? And is it a servlet based Java program?

  1. Look at the java file again

    Stone hammer! Indeed, the content of the jsp file is translated into the code of the servlet java program.

  2. Record the core method

//Initialization part
public void _jspInit() {
  }
//Destroyed part
public void _jspDestroy() {
  }
//JSPService 
public void _jspService(final jakarta.servlet.http.HttpServletRequest request, final jakarta.servlet.http.HttpServletResponse response)
      throws java.io.IOException, jakarta.servlet.ServletException {

    if (!jakarta.servlet.DispatcherType.ERROR.equals(request.getDispatcherType())) {}  
  1. How to judge the request
  if (!jakarta.servlet.DispatcherType.ERROR.equals(request.getDispatcherType())) {
      final java.lang.String _jspx_method = request.getMethod();
      if ("OPTIONS".equals(_jspx_method)) {
        response.setHeader("Allow","GET, HEAD, POST, OPTIONS");
        return;
      }
      if (!"GET".equals(_jspx_method) && !"POST".equals(_jspx_method) && !"HEAD".equals(_jspx_method)) {
        response.setHeader("Allow","GET, HEAD, POST, OPTIONS");
        response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, "JSP Only allowed GET,POST or HEAD. Jasper Also allow OPTIONS");
        return;
      }
    }
  1. Built in some required objects
final jakarta.servlet.jsp.PageContext pageContext;//Page context
jakarta.servlet.http.HttpSession session = null;//session
final jakarta.servlet.ServletContext application;// applicationContext
final jakarta.servlet.ServletConfig config;//config
jakarta.servlet.jsp.JspWriter out = null;//out
final java.lang.Object page = this;// Page current page
jakarta.servlet.jsp.JspWriter _jspx_out = null;
jakarta.servlet.jsp.PageContext _jspx_page_context = null;

application is actually the ServletContext of the servlet.

  1. Code to be added to the output page
  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("<html>\n");
      out.write("<body>\n");
      out.write("<h2>Hello World!</h2>\n");
      out.write("</body>\n");
      out.write("</html>\n");
  1. Explore whether the work directory will automatically generate java files when pages are dynamically generated?

When we run the program, you will find that the work folder will be killed! Then it seems to regenerate.



Again..
When we go to visit hello JSP will change again.

So the so-called dynamic generation is to generate the corresponding java file when accessing the request! ha-ha. Is it simple.

  1. JSP overall process


6. Simply test the jsp code

<%--
  Created by IntelliJ IDEA.
  User: muqua
  Date: 2021/8/13
  Time: 9:04
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
    String name = "Moo moo";
%>
name:<%=name%>
</body>
</html>

Why was JSP eliminated?

A: the coupling is too strong. Today's front and rear end separation no longer has a great favor for JSP.

Topics: Java JSP servlet