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  voiddoDelete(HttpServletRequest req, HttpServletResponse resp) 
          Called by the server (via the service method) to allow a servlet to handle a DELETE request.
protected  voiddoGet(HttpServletRequest req, HttpServletResponse resp) 
          Called by the server (via the service method) to allow a servlet to handle a GET request.
protected  voiddoHead(HttpServletRequest req, HttpServletResponse resp) 
          Receives an HTTP HEAD request from the protected service method and handles the request.
protected  voiddoOptions(HttpServletRequest req, HttpServletResponse resp) 
          Called by the server (via the service method) to allow a servlet to handle a OPTIONS request.
protected  voiddoPost(HttpServletRequest req, HttpServletResponse resp) 
          Called by the server (via the service method) to allow a servlet to handle a POST request.
protected  voiddoPut(HttpServletRequest req, HttpServletResponse resp) 
          Called by the server (via the service method) to allow a servlet to handle a PUT request.
protected  voiddoTrace(HttpServletRequest req, HttpServletResponse resp) 
          Called by the server (via the service method) to allow a servlet to handle a TRACE request.
protected  longgetLastModified(HttpServletRequest req) 
          Returns the time the HttpServletRequest object was last modified, in milliseconds since midnight January 1, 1970 GMT.
protected  voidservice(HttpServletRequest req, HttpServletResponse resp) 
          Receives standard HTTP requests from the public service method and dispatches them to the doXXX methods defined in this class.
 voidservice(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
 voiddestroy() 
          Called by the servlet container to indicate to a servlet that the servlet is being taken out of service.
 StringgetInitParameter(String name) 
          Returns a String containing the value of the named initialization parameter, or null if the parameter does not exist.
 EnumerationgetInitParameterNames() 
          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.
 ServletConfiggetServletConfig() 
          Returns this servlet's ServletConfig object.
 ServletContextgetServletContext() 
          Returns a reference to the ServletContext in which this servlet is running.
 StringgetServletInfo() 
          Returns information about the servlet, such as author, version, and copyright.
 StringgetServletName() 
          Returns the name of this servlet instance.
 voidinit() 
          A convenience method which can be overridden so that there's no need to call super.init(config).
 voidinit(ServletConfig config) 
          Called by the servlet container to indicate to a servlet that the servlet is being placed into service.
 voidlog(String msg) 
          Writes the specified message to a servlet log file, prepended by the servlet's name.
 voidlog(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  voidservice(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().

 

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); } }

转载于:https://www.cnblogs.com/yunxiblog/p/5188533.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值