Absrtact: This paper mainly studies the Servlet related interfaces and classes.
The interface and class of Servlet
Three ways
There are three ways to implement a Servlet:
Implement the javax.servlet.Servlet interface.
Inherit javax.servlet.GenericServlet class.
Inherit javax.servlet.http.HttpServlet class.
Implement Servlet interface
Servlet interface is the most basic interface. If you want to use servlet, you need to implement this interface or inherit other classes that have implemented this interface.
Create a class and implement the Servlet interface:
1 public class TestServlet implements Servlet { 2 @Override 3 public ServletConfig getServletConfig() { 4 return null; 5 } 6 7 @Override 8 public String getServletInfo() { 9 return null; 10 } 11 12 @Override 13 public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException { 14 System.out.println("service()Method is executed"); 15 } 16 17 @Override 18 public void init(ServletConfig config) throws ServletException { 19 System.out.println("init()Method is executed"); 20 } 21 22 @Override 23 public void destroy() { 24 System.out.println("destroy()Method is executed"); 25 } 26 }
Inherit GenericServlet class
GenericServlet class implements and rewrites some methods of Servlet interface, so that programmers only need to pay attention to the implementation of service() method when developing.
Create a class and inherit the GenericServlet class:
1 public class TestServlet extends GenericServlet { 2 @Override 3 public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException { 4 System.out.println("service()Method is executed"); 5 } 6 }
Inherit HttpServlet class
The HttpServlet class inherits the GenericServlet class and is a special support for HTTP requests. Because the developed projects generally follow the HTTP protocol, the HttpServlet class is often used.
The service(HttpServletRequest, HttpServletResponse) method of HttpServlet will judge whether the current request is GET or POST. If it is GET request, it will call the doGet() method of this class. If it is POST request, it will call the doPost() method, which means that the method of doGet() or doPost() can be overridden in the subclass.
Create a class and inherit the HttpServlet class:
1 public class TestServlet extends HttpServlet { 2 @Override 3 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 4 super.doGet(req, resp); 5 } 6 7 @Override 8 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 9 super.doPost(req, resp); 10 } 11 }
Other interfaces and classes
ServletConfig interface
Represents the configuration information of the current Servlet, encapsulates the Servlet context object and configuration parameter information.
You can get it through the getServletConfig() method in the Servlet:
1 public ServletConfig getServletConfig();
Common methods:
1 public String getServletName();// Get current Servlet The friendly name of. 2 public ServletContext getServletContext();// obtain ServletContext Object. 3 public String getInitParameter(String name);// Get the initialization parameters, which can be found in the web.xml Profile's<servlet></servlet>In the label<init-param></init-param>It's set in the label. 4 public Enumeration<String> getInitParameterNames();// Get all initialization parameters. Initialization parameters also need to be set in the web.xml Profile's<servlet></servlet>In the label<init-param></init-param>It's set in the label.
ServletContext interface
Represents the current Web application. The server creates a corresponding ServletContext object for each Web application, which is shared by all clients. When the Web application is started, it will be created automatically. When the Web application is closed and restarted, and the server is closed, the ServletContext will be destroyed.
You can get it through the getServletContext() method in ServletConfig:
1 public ServletContext getServletContext();
Common methods:
1 public String getServletContextName();// Gets the name of the current project. 2 public String getServerInfo();// return Servlet Container name and version number. 3 public ServletContext getContext(String uripath);// Get by path ServletContext Object. 4 public String getContextPath();// Gets the relative directory of the current project on the server. For example:/HelloWorld. 5 public String getRealPath(String path);// Gets the actual path to the specified file, starting at the application root. For example: TestServlet What you get is a drive letter:\folder\working space\entry name\WebContent\TestServlet. 6 public Set<String> getResourcePaths(String path);// Gets the folder name and file name under the specified relative path. Starting from the application root directory, the specified path must be/start. For example:/What you get is the content in the application root directory. 7 public URL getResource(String path) throws MalformedURLException;// Gets the address under the specified relative path, starting from the application root directory. For example:/What you get is jndi:/domain name/Project name/. 8 public InputStream getResourceAsStream(String path);// Converts the specified file to a stream for reading, starting at the application root. For example: index.html You can get the index.html And turn to flow. 9 public RequestDispatcher getRequestDispatcher(String path);// Create a forwarder that jumps to the specified path. Starting from the application root, the specified path must be/start. 10 public RequestDispatcher getNamedDispatcher(String name);// Create jump to specified Servlet The name of the transponder does not need to be/start. 11 public String getInitParameter(String name);// Gets the specified initialization parameter. The initialization parameter needs to be set in the web.xml Profile's<context-param></context-param>It's set in the label. 12 public Enumeration<String> getInitParameterNames();// Get all the specified initialization parameters. The initialization parameters also need to be set in the web.xml Profile's<context-param></context-param>It's set in the label. 13 public boolean setInitParameter(String name, String value);// Set initialization parameters, the effect is the same as web.xml Profile's<context-param></context-param>The label is the same. 14 public Object getAttribute(String name);// Get the property object by the property name. 15 public Enumeration<String> getAttributeNames();// Gets all property names. 16 public void setAttribute(String name, Object object);// Set the property name and property object. 17 public void removeAttribute(String name);// Delete the property object by the property name.
RequestDispatcher interface
The RequestDispatcher instance object is created by the Servlet engine to wrap a resource to be called by other resources and forward the client's request to the wrapped resource through its methods.
Two methods are defined in the RequestDispatcher interface: the forward () method and the include () method. The two parameters received by forward() and include() methods must be the ServletRequest and ServletResponse objects passed to the service() method of the current Servlet, or the ServletRequestWrapper and ServletResponseWrapper objects for which they are wrapped.
It can be obtained through getRequestDispatcher() method and getNamedDispatcher() method of ServletContext object:
1 public RequestDispatcher getRequestDispatcher(String path); 2 public RequestDispatcher getNamedDispatcher(String name);
Request forwarding:
1 // This method is used to transfer a request from a Servlet Deliver to another on the server Servlet,Jsp Page or Html Files, from the current Servlet Send to another Servlet,This method must be called before the response is submitted to the client. 2 // In the current Servlet You can set properties for the request. The set response header information will not be ignored, but the set response body information may be ignored. 3 public void forward(ServletRequest request, ServletResponse response) throws ServletException, IOException;
The request contains:
1 // This method is used to include Servlet,Jsp Page or Html Files, from the current Servlet Send to client, must be in current Servlet Set the encoding format in. 2 // current Servlet And the resources to be included can set the response body and output to the client in order. 3 public void include(ServletRequest request, ServletResponse response) throws ServletException, IOException;
Comparison between request forwarding and request inclusion:
Request forwarding is mostly used in Servlet, and the forwarding target is mostly Jsp page.
The request inclusion is mostly applied in the Jsp page to complete the combination of multiple pages.