Detailed explanation of JSP technology and Servlet technology

  1. What is Servlet and the advantages of using Servlet.
  2. Servlet steps to handle client requests.
  3. The advantages of JSP.
  4. Similarities and differences between JSP and Servlet.
  5. JSP other knowledge points.

What is Servlet and the advantages of using Servlet.

What is Servlet? Servlet is a server-side program written in Java language. It runs in the Servlet container in the Web server. Its main function is to provide a request / response Web service model, which can generate dynamic Web content, which is what HTML does not have. Function.
advantage:

  1. Better portability. Because the Java language has the characteristics of cross-platform and strong portability, Servlet also has good portability.
  2. High efficiency. Because CGI (Common Gateway Interface) creates a process for each request to process, and Servlet creates a thread for each request to execute, and creating a thread is less expensive than creating a process, so compared with CGI, Servlet is interacting There is a shorter response time in the process, and the response efficiency is higher.
  3. Powerful. Servlet can interact with the web server, but CGI cannot directly interact with the web server.
  4. Easy to use.
  5. Strong scalability.

    To better illustrate Servlet, here is an example:

package xupt.edu.ttms.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/MyServlet")
public class MyServlet extends HttpServlet
{
    private static final long serialVersionUID = 1L;

    public MyServlet()
    {
        super();
        // TODO Auto-generated constructor stub
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        // TODO Auto-generated method stub
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        // TODO Auto-generated method stub
    }

}

From the above example, we can see the basic structure of Servlet: MyServlet inherits HttpServlet, and the class contains 2 methods doPost and doGet. "@WebServlet (" / MyServlet ")" is to use @WebServlet annotation to realize the mapping between servlet and url.


Servlet steps to handle client requests

  1. The user initiates a request to the Servlet by clicking a link.
  2. After receiving the request, the Web server will hand over the request to the corresponding container for processing. When the container finds that this is a request to the Servlet, the container will create two objects at this time: HttpServletRequest and HttpServletResponse.
  3. The container can find the corresponding Servlet according to the URL message in the request message, and then create a separate thread for the request, and pass the two objects created in the second step to the newly created thread in the form of parameters.
  4. The container calls the Servlet's service () method to complete the response to the user's request. The service () method calls the doPost () and doGet () methods to complete the specific response task, and returns the generated dynamic page to the container.
  5. The container assembles the response message into HTTP format and returns it to the client. At this point, the thread finishes running and deletes the 2 objects created in the second step.
    The above processing flow chart is as follows:
    Write a picture description here

Advantages of JSP

The introduction of JSP solves the shortcomings of Servlet to a certain extent. Its realization concept is to let each Servlet only be responsible for its corresponding business logic processing, and let JSP be responsible for the user's HTML display, so it realizes the separation of business logic and view implementation, thus greatly improving the scalability of the system.


Similarities and differences between JSP and Servlet

Same point:

JSP can be regarded as a special Servlet, it is just an extension of Servlet, as long as it can be done by JSP, using Servlet can be completed, for example, generating dynamic pages. Since the JSP page will eventually be converted into a Servlet to run, the processing request is actually a compiled Servlet.

Differences:
1. The implementation of Servlet embeds HTML code in Java. It is very inconvenient to write and modify HTML, so it is more suitable for process control and business processing; while the implementation of JSP embeds Java code in HTML, which is more suitable for page display.
2. There are no built-in objects in Servlet. The built-in objects in JSP must be obtained through HttpServletRequest object, HttpServletResponse object and HttpServlet object.

JSP execution process:
Write a picture description here
translation and compilation
Write a picture description here


JSP other knowledge points

1. JSP page elements: static content, instructions, expressions, scriptlets, declarations, actions, comments
2. JSP instructions: page, include, taglib
3. JSP built-in objects: request, response, out, session, application, pageContext, page , Config, exception

Published 19 original articles · praised 5 · visits 7740

Guess you like

Origin blog.csdn.net/qq_38119372/article/details/79564318