Often write business code, it is time to wait for a complementary foundation 😊

Servlet

A Servlet begins its life cycle when the server is started or first requested, and ends its life cycle when the server ends. No matter how many servlets are requested, there is at most one Servlet instance. When multiple clients request a Servlet concurrently, the server starts multiple threads to execute the Servlet’s service method separately

An overview of the

The working process
  • Tomcat encapsulates the request submitted by the browser as an HttpServletRequest object and the output stream as an HttpServletResponse object
  • Tomcat takes request and response as parameters and calls corresponding methods of servlets, such as doGet(request, response), etc
  • Business logic is handled in servlets
interface

Implement the Javax.servlet.servlet interface class, specifying specific methods to handle specific requests. Developers only need to implement the Servlet’s methods, which Tomcat calls to complete business processing when a user accesses the Web application

Java Web directory structure
folder describe
/ Web application root directory
/WEB-INF/ Tomcat will hide all files and folders in this folder, protecting them from direct browser access
/WEB-INF/web.xml The main configuration file
/WEB-INF/classes/ Class files, including Servlet classes
/WEB-INF/lib/ Jar file location

configuration

servlet

<servlet>
  <servlet-name>[Unique name]</servlet-name>
  <servlet-class>[Path containing package name]</servlet-class>
  <init-param>
    <param-name>[Configuration name]</param-name>
    <param-value>[Configuration value]</param-value>
  </init-param>
  <load-on-startup>1[0: load at request; 1: load at startup]</load-on-startup>
</servlet>
Copy the code

Access path Configure servlet-mapping

<servlet-mapping>
  <servlet-name>[Corresponding to name in servlet configuration]</servlet-name>
  <url-pattern>[Access path]</url-pattern>
</servlet-mapping>
Copy the code

Context parameter context-param

Shared globally and read by all servlets

<context-param>
  <param-name>[Configuration name]</param-name>
  <param-value>[Configuration value]</param-value>
</context-param>
Copy the code

In the Servlet class, through getServletConfig. GetServletContext ServletContext, then parameters obtained through its corresponding context

Servlet life Cycle

  • init
  • doGet
  • doPost
  • .
  • Destroy

The Servlet annotations

  • @PostContructor
    • The init function is called after constructor execution and before execution
  • @PreDestroy
    • The Servlet is called after the destroy method and before it is completely uninstalled
  • Annotations can affect the startup speed of the server
    • At startup, the server iterates through all class files under WEB-INF/classes and JAR files under WEB-INF/lib of the Web application to check which classes use annotations

Thread safety

Hidden trouble reason

Since there is only one instance of a Servlet, Tomcat will generate multiple threads to execute the Servlet code when multiple users request the same Servlet at the same time, so the Servlet has the potential to be thread unsafe.

redirect

Server redirection

This is done through the Forward (HttpServletRequest, HttpServletResponse) of the RequestDispatcher object; The address bar of the browser displays the access address of the Servlet before the jump

Browser redirection

  • 301 Permanent redirection
    • The original address was removed by the search engine
  • 302 Temporary redirection
    • It is possible that the page end address is unchanged, resulting in hidden dangers of hijack
    • Page Rank correlation is affected