您的位置:首页 > 理论基础 > 计算机网络

What is the difference between GenericServlet, HttpServlet and a Servlet?

2015-08-31 03:26 776 查看
Servlet
is an interface defining what a servlet must implement.

GenericServlet
is just that, a generic, protocol-independent servlet.

HttpServlet
is a servlet tied specifically to the HTTP protocol.

In general, you’d extend
HttpServlet
to implement an application’s web layer.

You might implement
Servlet
if you’re writing your own container or handling everything yourself. You might extend
GenericServlet
to handle a different protocol, but you might not.

javax.servlet.Servlet
is interface, it defines methods for all the implementations - that’s what interfaces usually do.

javax.servlet.GenericServlet
is protocol independent. It is abstract, so it is not to be directly instantiated. It is 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
is a server-side web technology. As the name implies, it serves a client request and receives a response from the server. You have to implement
javax.Servlet
(Interface) to handle a servlet work.

javax.servlet.GenericServlet

Signature:

public abstract class GenericServlet extends java.lang.Object implements Servlet, ServletConfig, java.io.Serializable


GenericServlet
defines a generic, protocol-independent servlet.

GenericServlet
gives a blueprint and makes writing servlet easier.

GenericServlet provides simple versions of the life-cycle methods
init
and
destroy
and of the methods in the
ServletConfig
interface.

GenericServlet implements the log method, declared in the
ServletContext
interface.

To write a generic servlet, it is sufficient to override the abstract
service()
method.

javax.servlet.http.HttpServlet

Signature:

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


HttpServlet defines a HTTP protocol specific servlet.

HttpServlet gives a blueprint for Http servlet and makes writing them easier.

HttpServlet extends the GenericServlet and hence inherits the properties GenericServlet.

HttpServlet
is specific to the HTTP protocol and hence it supplies methods for the HTTP verbs:
doGet
,
doPost
, etc, and a version of the generic service method that takes HTTP-specific request and response objects. It is a special type of Servlet which is actually a pretty minimal interface.

GenericServlet
is the basic, protocol-neutral implementation of the Servlet interface. Often you’ll find similar basic implementations of interfaces in an API; in this case
GenericServlet
adds a bit of functionality to the Servlet API:
getServletName
,
getServletInfo
, and pass-through methods for the servlet
init
parameters.
HttpServlet
benefits from these additions by extending
GenericServlet
.

Generally everyone coding against this API is using
HttpServlet
for implementing HTTP web services, but one can also extend or use
GenericServlet
for implementing server/service functionality using a custom protocol, or another extant protocol, for example, FTP.

Servlet
:-

The
Servlets
runs as a thread in a web-container instead of in a seperate OS process.

Only one object is created first time when first request comes, other request share the same object.

Servlet
is platform independent.

Servlet
is fast.

GenericServlet:-

General for all protocol.

Implements Servlet Interface.

Use Service method.

HttpServlet:-

Only for HTTP Protocol.

Inherit GenericServlet class.

Use doPost, doGet method instead of service method.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: