What is the difference between three ways to implement a servlet? --Reprint

Source: Internet
Author: User

Copyright belongs to the author.
Commercial reprint please contact the author for authorization, non-commercial reprint please specify the source.
Guo Unintentionally
Links: https://www.zhihu.com/question/38971252/answer/79165562
Source: Know

First, it is clear that the servlet is not specifically designed to handle HTTP requests. Then say three different ways of connection and difference.
1) Native Servlet interface
package javax.servlet;import java.io.IOException;public interface Servlet {    public void init(ServletConfig config) throws ServletException;        public ServletConfig getServletConfig();        public void service(ServletRequest req, ServletResponse res)throws ServletException, IOException;    public String getServletInfo();        public void destroy();}
You can see that the servlet interface is defined in the Javax.servlet package and that the interface defines 5 methods.
Init (): After the servlet instantiation, the servlet container invokes the Init () method to initialize the object, primarily to allow the Servlet object to perform some initialization work before processing the client request, such as establishing a connection to the database and obtaining configuration information.

Service (): The container invokes the service () method to process the client's request.

Destroy (): When the container detects that a Servlet object should be removed from the server, the container invokes the object's Destroy method so that the Servlet object can free the resources it uses, save the data to the persisted storage device, For example, to save the in-memory data to the database and close the connection to the database.

2) Genericservlet
Why does Genericservlet appear? If we write a servlet class directly by implementing a servlet interface, we need to implement the 5 methods defined in the Servlet interface, in order to simplify the writing of the servlet, In the Javax.servlet package, we provide an abstract class Genericservlet, which provides a simple implementation of 4 other methods except the service () method. The Genericservlet class defines a common servlet that does not rely on a specific protocol
Package Javax.servlet;import Java.io.ioexception;import Java.util.enumeration;import java.util.ResourceBundle; Public abstract class Genericservlet implements Servlet, ServletConfig, java.io.serializable{private static final    String lstring_file = "Javax.servlet.LocalStrings";    private static ResourceBundle lstrings = Resourcebundle.getbundle (lstring_file);        private transient servletconfig config;  Public Genericservlet () {} public void Destroy () {} public string Getinitparameter (string name)        {ServletConfig sc = getservletconfig (); if (sc = = null) {throw new IllegalStateException (Lstrings.getstring ("err.servlet_config_not_in        Itialized "));    } return Sc.getinitparameter (name);        } public enumeration<string> Getinitparameternames () {ServletConfig sc = getservletconfig (); if (sc = = null) {throw new IllegalStateException (LstrinGs.getstring ("err.servlet_config_not_initialized"));    } return Sc.getinitparameternames ();    } public ServletConfig Getservletconfig () {return config;        } public ServletContext Getservletcontext () {ServletConfig sc = getservletconfig (); if (sc = = null) {throw new IllegalStateException (Lstrings.getstring ("err.servlet_config_not_in        Itialized "));    } return Sc.getservletcontext ();    Public String Getservletinfo () {return "";    } public void init (ServletConfig config) throws servletexception {this.config = Config;this.init (); } public void Init () throws servletexception {} public void log (String msg) {getservletcontext (). log (g    Etservletname () + ":" + msg);    public void log (String message, Throwable t) {getservletcontext (). log (Getservletname () + ":" + message, T); } public abstract void service (ServletRequest req, servletresponse res) throws Servletexception, IOException;        Public String Getservletname () {ServletConfig sc = getservletconfig (); if (sc = = null) {throw new IllegalStateException (Lstrings.getstring ("err.servlet_config_not_in        Itialized "));    } return Sc.getservletname (); }}

3) HttpServlet
Since most Web applications are browsers accessing server resources through the HTTP protocol, the servlet we write is primarily a request and response to the HTTP protocol, in order to quickly develop a servlet applied to the HTTP protocol, Sun has provided us with an abstract class HttpServlet in the Javax.servlet.http package, which he inherits from the Genericservlet class, which is used to create an HTTP servlet suitable for a Web site.

For example, DoPost doget These methods, starting from request parsing requests, parsing according to the HTTP protocol format, distributing to different request method processing, DoPost Doget is definitely called by the service method.

What is the difference between three ways to implement a servlet? --Reprint

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.