Servlets are actually Serverapplets — small servers or service connectors, server-side programs written in Java that interactively browse and modify data to generate dynamic Web content. Like common protocols such as DNS, TCP/IP, and HTTP, servlets exist as a set of specifications; As part of the J2EE standard, javaweb development standards are defined. Servlets provide Java with a set of standards for handling WEB requests, and we just need to follow those standards. In fact, both Struts2’s FilterDispatcher and SpringMvc’s DispatcherServlet are implemented by implementing Sevlet or servlet-type extensions such as GenericServlet.
1. The Servlet interface
The following is the structure diagram in Servlet3.1:
The init method is called and takes a parameter of type ServletConfig to initialize the Servlet, passed in by the container. ServletConfig, as the name implies, contains configuration information for Serlvet. Normally, when we define Serlvet in a web.xml file, we configure the parameters with the init-param tag. In a Springmvc configuration, parameters are typically configured in the following way:
2. The ServletConfig interface
3.GenericServlet
GenericServlet is the default implementation of servlets with the following code:
package javax.servlet;
import java.io.IOException;
import java.io.Serializable;
import java.util.Enumeration;
import java.util.ResourceBundle;
public abstract class GenericServlet
implements Servlet, ServletConfig, Serializable
{
private static final String LSTRING_FILE = "javax.servlet.LocalStrings";
private static ResourceBundle lStrings = ResourceBundle.getBundle("javax.servlet.LocalStrings");
private transient ServletConfig config;
public void destroy()
{
}
public String getInitParameter(String name)
{
ServletConfig sc = getServletConfig();
if (sc == null) {
throw new IllegalStateException(lStrings.getString("err.servlet_config_not_initialized"));
}
return sc.getInitParameter(name);
}
public Enumeration getInitParameterNames()
{
ServletConfig sc = getServletConfig();
if (sc == null) {
throw new IllegalStateException(lStrings.getString("err.servlet_config_not_initialized"));
}
return sc.getInitParameterNames();
}
public ServletConfig getServletConfig()
{
return this.config;
}
public ServletContext getServletContext()
{
ServletConfig sc = getServletConfig();
if (sc == null) {
throw new IllegalStateException(lStrings.getString("err.servlet_config_not_initialized"));
}
return sc.getServletContext();
}
public String getServletInfo()
{
return "";
}
public void init(ServletConfig config)
throws ServletException
{
this.config = config;
init();
}
public void init()
throws ServletException
{
}
public void log(String msg)
{
getServletContext().log(getServletName() + ":" + msg);
}
public void log(String message, Throwable t)
{
getServletContext().log(getServletName() + ":" + message, t);
}
public abstract void service(ServletRequest paramServletRequest, ServletResponse paramServletResponse)
throws ServletException, IOException;
public String getServletName()
{
ServletConfig sc = getServletConfig();
if (sc == null) {
throw new IllegalStateException(lStrings.getString("err.servlet_config_not_initialized"));
}
returnsc.getServletName(); }}Copy the code
In terms of its inheritance and implementation relationships, GenericServlet does three main things: 1. Implements the ServletConfig interface so that we can directly call methods in ServletConfig; GenericServlet implements ServletConfig and can call methods from the ServletConfig directly when needed, without first obtaining ServletConfig objects. For example, you can call getServletContext directly when you get a ServletContext without calling getServletConfig().getServletContext(), but in fact, The underlying internal implementation still internally calls getServletConfig().getServletContext().
4.HttpServlet
HttpServlet is the base class of the Servlet based on Http protocol. When writing a Servlet, you can inherit HttpServlet directly, and there is no need to re-implement the Servlet interface. The dispatcherServlet in SpringMvc is a subclass of HttpServlet. HttpServlet is related to the Http protocol. HttpServlet handles requests mainly by overwriting the service method of the parent class to complete specific request processing. In the service method, the first is to convert ServletRequest and ServletResponse to HttpServletRequest and HttpServletResponse, The request is then routed to different processes based on the request. The most common are doGet and doPost