Basic understanding of JSP

Posted by Someone789 on Tue, 01 Feb 2022 07:54:36 +0100

Causes of JSP

In Servlet, we can output HTML through PrintWriter, such as the following. But obviously, outputting HTML with PrintWriter is basically impossible. Therefore, we can use Servlet to simplify the complex work of this series of printwriters.

PrintWriter pw = resp.getWriter();
pw.write("<html>");
pw.write("<body>");
pw.write("<h1>Welcome, " + name + "!</h1>");
pw.write("</body>");
pw.write("</html>");
pw.flush();

First understanding of JSP

  • JSP is the abbreviation of Java Server Pages
  • The jsp file must be placed under / src/main/webapp, and the file name must begin with jsp end
  • When accessing a JSP page, specify the full path directly: for example: http://localhost:8080/hello.jsp
<html>
<head>
    <title>Hello World - JSP</title>
</head>
<body>
    <%-- JSP Comment --%>
    <h1>Hello World!</h1>
    <p>
    <%
         out.println("Your IP address is ");
    %>
    <span style="color:red">
        <%= request.getRemoteAddr() %>
    </span>
    </p>
</body>
</html>

Operation results:

JSP syntax

  1. instructions

<% – comments –% > for comments
<% Java% > for writing java code
<% = XXX% > used to quickly output the value of a variable
<% @ page import = "% > Guide Package
<% @ include file = "xxx.jsp" "% > Import JSP file

  1. variable

out: indicates the PrintWriter of HttpServletResponse;
session: indicates the current HttpSession object;
request: indicates the HttpServletRequest object;

Connection between JSP and Servlet

In fact, there is no difference between them, because JSP is first compiled into a Servlet before execution. In the temporary directory of tomcat, you can find a hello_jsp.java source file, which is the Servlet source code that Tomcat automatically converts JSP into

  • JSP original code:
<html>
<head>
    <title>Hello World - JSP</title>
</head>
<body>
    <%-- JSP Comment --%>
    <h1>Hello World!</h1>
    <p>
    <%
         out.println("Your IP address is ");
    %>
    <span style="color:red">
        <%= request.getRemoteAddr() %>
    </span>
    </p>
</body>
</html>
  • Convert to Servlet Code:
public final class hello_jsp extends org.apache.jasper.runtime.HttpJspBase
    implements org.apache.jasper.runtime.JspSourceDependent,
               org.apache.jasper.runtime.JspSourceImports {

    ...

    public void _jspService(final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response)
        throws java.io.IOException, javax.servlet.ServletException {
        ...
        out.write("<html>\n");
        out.write("<head>\n");
        out.write("    <title>Hello World - JSP</title>\n");
        out.write("</head>\n");
        out.write("<body>\n");
        ...
    }
    ...
}

Nine built-in objects

  • Built in object concept
    The built-in Java objects have been defaulted in the JSP page system, and these objects can be used without explicit declaration by developers.

  • Built in object classification

  1. Output and input objects: request object, response object and out object;
  2. Communication control objects: pageContext object, session object and application object;
  3. Servlet object: page object, config object;
  4. Error handling object: exception object.
  • Built in object function

Request object: represents the request object, which is mainly used to receive customer segment data.

Response object: represents the response object, which is mainly used to send data to the client

Out object: mainly used to output data to the client

Session object: it is mainly used to maintain the data that needs to be reserved between the server and a client. When the client closes all web pages of the website or closes the browser, the data saved in the session object will be automatically cleared. Like a shopping cart.

Application object: it is used to save user information. Multiple users in a container share an application object, and the saved information is shared by all users. When one user runs out, other users may use it.

PageContext object: used to manage web page properties and wrap the context of the page for JSP pages.

Config object: the configuration object of the Servlet

Page object: used to process JSP Web pages, which refers to the JSP page object itself

Exception object: handle errors and exceptions that occur during the execution of JSP files. Exception objects can be used on this page only after isErrorPage = "true" is specified in the page instruction of JSP page.

Topics: JSP