Introduction to the javax.servlet package

Using Java technology to develop WEB applications, in-depth understanding of the mechanism of Servlet will have an important role in promoting the development of applications. If you want to understand the mechanism of Servlet, you have to understand the javax.servlet package.

With the Servlet2.3 specification, we introduce the classes in the javax.servlet package:

The javax.servlet package contains 7 interfaces, 3 classes and 2 exception classes, which are:

接口:RequestDispatcher,Servlet,ServletConfig,ServletContext,ServletRequest,ServletResponse和SingleThreadModel

类:GenericServlet,ServletInputStream和ServletOutputStream

Exception class: ServletException and UnavailableException

Servlet life cycle

A servlet life cycle method is defined in the servlet interface, namely Init, Service and Destroy

A simple servlet demonstrating the servlet lifecycle methods:

import javax.servlet.*; 
import java.io.IOException; 

public class PrimitiveServlet implements Servlet {

  public void init(ServletConfig config) throws ServletException {
    System.out.println("init"); 
  } 

  public void service(ServletRequest request, ServletResponse response) 
    throws ServletException, IOException {
    System.out.println("service"); 
  }   
  public void destroy() {
    System.out.println("destroy"); 
  } 

  public String getServletInfo() {
    return null; 
  } 
  public ServletConfig getServletConfig() {
    return null; 
  } 

}

How to get the ServletConfig object in Servlet?

In the Servlet Init method, the Servlet Container will pass in a ServletConfig object, through which the developer can obtain the Servlet initialization parameters defined in the web.xml file.

Here is an example of getting the initial parameters of a servlet:

import javax.servlet.*; 
import java.util.Enumeration; 
import java.io.IOException; 

public class ConfigDemoServlet implements Servlet {

  public void init(ServletConfig config) throws ServletException {
    Enumeration parameters = config.getInitParameterNames(); 
    while (parameters.hasMoreElements()) {
      String parameter = (String) parameters.nextElement(); 
      System.out.println("Parameter name : " + parameter); 
      System.out.println("Parameter value : " + 
        config.getInitParameter(parameter)); 
    } 
  } 

  public void destroy() {
  } 

  public void service(ServletRequest request, ServletResponse response) 
    throws ServletException, IOException {
  } 

  public String getServletInfo() {
    return null; 
  } 

  public ServletConfig getServletConfig() {
    return null; 
  } 
}

How to get the ServletContext object?

The ServletContext object can be obtained through the getServletContext method of the ServletConfig object

import javax.servlet.*; 
import java.util.Enumeration; 
import java.io.IOException; 

public class ContextDemoServlet implements Servlet {
  ServletConfig servletConfig; 

  public void init(ServletConfig config) throws ServletException {
    servletConfig = config; 
  } 

  public void destroy() {
  } 

  public void service(ServletRequest request, ServletResponse response) 
    throws ServletException, IOException {  
    ServletContext servletContext = servletConfig.getServletContext(); 
    Enumeration attributes = servletContext.getAttributeNames(); 
    while (attributes.hasMoreElements()) {
      String attribute = (String) attributes.nextElement(); 
      System.out.println("Attribute name : " + attribute); 
      System.out.println("Attribute value : " + 
        servletContext.getAttribute(attribute)); 
    } 

    System.out.println("Major version : " + 
servletContext.getMajorVersion()); 
    System.out.println("Minor version : " + 
servletContext.getMinorVersion()); 
    System.out.println("Server info : " + servletContext.getServerInfo()); 
  } 

  public String getServletInfo() {
    return null; 
  } 
  public ServletConfig getServletConfig() {
    return null; 
  } 

}

How to share information between servlets?

We can maintain information shared between different servlets through ServletContext.

How to solve the multi-Thread problem of Servlet?

If the Servlet needs to read and write external resources, we need to consider the problem of Thread. We can use the declarative interface SingleThreadModel to avoid resource conflicts between multiple Threads.
But it should be noted that if the Servlet only reads external resources, we usually This interface should not be implemented. If this interface is implemented, the servlet can only serve one user request at a time, and the later user request must wait in the queue.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325362954&siteId=291194637