GenericServlet vs HttpServlet

 1>>>>>>>>

Difference between HttpServlet vs Generic Severlet

javax.servlet.Servlet----is an interface defining what a servlet must implement.it defines methods for all the implementations - that‘s what interfaces usually do.

javax.servlet.GenericServlet------is just that, a generic, protocol-independent servlet.  It is abstract, so it is not to be directly instantiated. It is the usable class to extend  if you some day have to write servlet for protocol other than HTTP.

javax.servlet.http.HttpServlet------- is abstract class to be extended if you want to communicate over HTTP protocol. Most likely you only have to care about this one.

Servlet:-

  1. The Servlets runs as a thread in a web-container instead of in a seperate OS process.
  2. Only one object is created first time when first request comes, other request share the same object.
  3. Servlet is platform independent.
  4. Servlet is fast.

GenericServlet:-

  1. General for all protocol.
  2. Implements Servlet Interface.
  3. Use Service method.

HttpServlet:-

  1. Only for HTTP Protocol.
  2. Inherit GenericServlet class.
  3. Use doPost, doGet method instead of service method.

 2>>>>>>>>

HttpServlet

public abstract class HttpServlet
extends GenericServlet
implements java.io.Serializable

Provides an abstract class to be subclassed to create an HTTP servlet suitable for a Web site. A subclass of HttpServlet must override at least one method, usually one of these:

  • doGet, if the servlet supports HTTP GET requests
  • doPost, for HTTP POST requests
  • doPut, for HTTP PUT requests
  • doDelete, for HTTP DELETE requests
  • init and destroy, to manage resources that are held for the life of the servlet
  • getServletInfo, which the servlet uses to provide information about itself
Constructor Summary
HttpServlet() 
          Does nothing, because this is an abstract class.
Method Summary
protected  void doDelete(HttpServletRequest req, HttpServletResponse resp) 
          Called by the server (via the service method) to allow a servlet to handle a DELETE request.
protected  void doGet(HttpServletRequest req, HttpServletResponse resp) 
          Called by the server (via the service method) to allow a servlet to handle a GET request.
protected  void doHead(HttpServletRequest req, HttpServletResponse resp) 
          Receives an HTTP HEAD request from the protected service method and handles the request.
protected  void doOptions(HttpServletRequest req, HttpServletResponse resp) 
          Called by the server (via the service method) to allow a servlet to handle a OPTIONS request.
protected  void doPost(HttpServletRequest req, HttpServletResponse resp) 
          Called by the server (via the service method) to allow a servlet to handle a POST request.
protected  void doPut(HttpServletRequest req, HttpServletResponse resp) 
          Called by the server (via the service method) to allow a servlet to handle a PUT request.
protected  void doTrace(HttpServletRequest req, HttpServletResponse resp) 
          Called by the server (via the service method) to allow a servlet to handle a TRACE request.
protected  long getLastModified(HttpServletRequest req) 
          Returns the time the HttpServletRequest object was last modified, in milliseconds since midnight January 1, 1970 GMT.
protected  void service(HttpServletRequest req, HttpServletResponse resp) 
          Receives standard HTTP requests from the public service method and dispatches them to the doXXX methods defined in this class.
 void service(ServletRequest req, ServletResponse res) 
          Dispatches client requests to the protected service method.

why httpservlet is abstract?

If you extend the httpservlet class without overriding any methods, you will get a useless servlet;

GenericServlet

public abstract class GenericServlet

extends java.lang.Object

implements ServletServletConfig, java.io.Serializable

Constructor Summary
GenericServlet() 
          Does nothing.
Method Summary
 void destroy() 
          Called by the servlet container to indicate to a servlet that the servlet is being taken out of service.
 String getInitParameter(String name) 
          Returns a String containing the value of the named initialization parameter, or null if the parameter does not exist.
 Enumeration getInitParameterNames() 
          Returns the names of the servlet‘s initialization parameters as an Enumeration of String objects, or an empty Enumeration if the servlet has no initialization parameters.
 ServletConfig getServletConfig() 
          Returns this servlet‘s ServletConfig object.
 ServletContext getServletContext() 
          Returns a reference to the ServletContext in which this servlet is running.
 String getServletInfo() 
          Returns information about the servlet, such as author, version, and copyright.
 String getServletName() 
          Returns the name of this servlet instance.
 void init() 
          A convenience method which can be overridden so that there‘s no need to call super.init(config).
 void init(ServletConfig config) 
          Called by the servlet container to indicate to a servlet that the servlet is being placed into service.
 void log(String msg) 
          Writes the specified message to a servlet log file, prepended by the servlet‘s name.
 void log(String message, Throwable t) 
          Writes an explanatory message and a stack trace for a given Throwable exception to the servlet log file, prepended by the servlet‘s name.
abstract  void service(ServletRequest req, ServletResponse res) 
          Called by the servlet container to allow the servlet to respond to a request.

3>>>>>>>>

When we should use Service() or toGet(), toPost()?

The differences between the doGet() and doPost() methods are that they are called in theHttpServlet that your servlet extends by its service() method when it recieves a GET or a POST request from a HTTP protocol request.

A GET request is a request to get a resource from the server. This is the case of a browser requesting a web page. It is also possible to specify parameters in the request, but the length of the parameters on the whole is limited. This is the case of a form in a web page declared this way in html: <form method="GET"> or <form>.

A POST request is a request to post (to send) form data to a resource on the server. This is the case of of a form in a web page declared this way in html: <form method="POST">. In this case the size of the parameters can be much greater.

The GenericServlet has a service() method that gets called when a client request is made. This means that it gets called by both incoming requests and the HTTP requests are given to the servlet as they are (you must do the parsing yourself).

The HttpServlet instead has doGet() and doPost() methods that get called when a client request is GET or POST. This means that the parsing of the request is done by the servlet: you have the appropriate method called and have convenience methods to read the request parameters.

NOTE: the doGet() and doPost() methods (as well as other HttpServlet methods) are called by the service() method.

Concluding,

if you must respond to GET or POST requests made by a HTTP protocol client (usually a browser) don‘t hesitate to extend HttpServlet and use its convenience methods.
If you must respond to requests made by a client that is not using the HTTP protocol, you must useservice().


up vote22down vote

This is how service() is typically implemented (very simplified):

protected void service(HttpServletRequest req, HttpServletResponse resp) {
    String method = req.getMethod();

    if (method.equals(METHOD_GET)) {
            doGet(req, resp);
    } else if (method.equals(METHOD_HEAD)) {
        doHead(req, resp);
    } else if (method.equals(METHOD_POST)) {
        doPost(req, resp);

    } else if (method.equals(METHOD_PUT)) {
        doPut(req, resp);   

    } else if (method.equals(METHOD_DELETE)) {
        doDelete(req, resp);

    } else if (method.equals(METHOD_OPTIONS)) {
        doOptions(req,resp);

    } else if (method.equals(METHOD_TRACE)) {
        doTrace(req,resp);

    } else {
        resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED, errMsg);
    }
}

时间: 2024-10-31 06:36:38

GenericServlet vs HttpServlet的相关文章

servlet实现的三种方式对比(servlet 和GenericServlet和HttpServlet)

第一种: 实现Servlet 接口 第二种: 继承GenericServlet 第三种 继承HttpServlet (开发中使用) 通过查看api文档发现他们三个(servlet 和GenericServlet和HttpServlet)的关系是 Servlet是一个接口,其中含有很多方法如:init(),service(),destory()方法. GenericServlet是一个实现了Servlet接口的实现类,他可以使用Servlet中的方法. HttpServlet是GenericSer

servlet、genericservlet、httpservlet之间的区别(转)

当编写一个servlet时,必须直接或间接实现servlet接口,最可能实现的方法就是扩展javax.servlet.genericservlet或javax.servlet.http.httpservlet当实现javax.servlet.servlet接口时必须实现5个方法 init(servletconfig   config)    service(servletrequest   req,servletresponse   resp)    destroy()    getservle

Servlet,GenericServlet和HttpServlet的继承关系

HttpServlet是GenericServlet的子类. GenericServlet是个抽象类,必须给出子类才能实例化.它给出了设计servlet的一些骨架,定义了servlet生命周期,还有一些得到名字.配置.初始化参数的方法,其设计的是和应用层协议无关的,也就是说 你有可能用非http协议实现它. HttpServlet是子类,当然就具有GenericServlet的一切特性,还添加了doGet, doPost, doDelete,doPut, doTrace等方法对应处理http协议

GenericServlet和HttpServlet

1 GenericServlet是Servlet接口和ServletConfig接口的实现类,但是一个抽象类,其中service为抽象方法 2 如果新建的Servlet程序直接继承GenericServlet会是开发更简洁 3 具体实现: 1)该类中声明了一个ServletConfig类型的成员变量,在inti(ServletConfig)方法进行初始化 2)利用servletConfig成员变量的方法实现ServletConfig接口的方法 3)定义了一个init()方法,在inti(Serv

Servlet,GenericServlet,httpServlet区别

Servlet接口:定义五个方法,其中包括三个生命周期方法 GenericServlet类:实现了Servlet定义的方法,参与了Servlet的生命周期 HttpServlet类:继承了GenericServlet,无需参与Servlet的生命周期,可以直接调用GenreicServlet的方法,并且还扩展了doGet, doPost, doDelete, doPut, doTrace等方法 使用Servlet的三种方法 Implements Servlet接口:你这个Servlet类会去实现

servlet理解

一.Servlet简介 Servlet是sun公司提供的一门用于开发动态web资源的技术. Sun公司在其API中提供了一个servlet接口,用户若想用发一个动态web资源(即开发一个Java程序向浏览器输出数据),需要完成以下2个步骤: 1.编写一个Java类,实现servlet接口. 2.把开发好的Java类部署到web服务器中. 按照一种约定俗成的称呼习惯,通常我们也把实现了servlet接口的java程序,称之为Servlet 二.Servlet的运行过程 Servlet程序是由WEB

servlet开篇

今天在看关于servlet的知识,像java程序都继承了object类一样.servlet程序都是servlet的子类,意思是.全部servlet程序都要继承于servlet(可继承的类有:GenericServlet,HttpServlet). 在我写第一个servlet程序时,居然不用写web.xml文件.仅仅是在类的外面写了以下的一句话: @WebServlet(name="servletname",urlPttern={"/servletReaplace"}

web开发之Servlet 一

因为最近在研究公司一套新的框架,发现这套框架的底层是对Struts2,Spring 封装后的WEB应用框架,而我发现如果仅仅是利用这个框架开发,确实很容易快速上手,做业务来说是没有问题的,但我觉得如果只对上层如何去用熟悉是不行,必须要学习其底层是如何玩的,而任何一套WEB应用框架的开发,肯定都是基于Servlet 对象中各个方法的生命周期来进行的,因此对Servlet的研究是有必要,虽然以前学过,但是很多原理都遗忘了,为此决定重新学习一下. 本人的开发工具和环境是:Myeclipse + Tom

servlet回顾

servlet genericServlet抽象类 HttpServlet抽象类 ServletRequest接口 HttpServletRequest接口 ServletResponse接口 HttpServletResponse接口 ServletConfig接口 ServletContext接口 javaweb应用的生命周期 Servlet的生命周期 servletcontext共享数据 servletcontextlistener 监听器 防止页面被缓存 文件下载 上传文件 生成动态图像