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

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.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ServletGenericServletHttpServlet是JavaWeb中的三个关键类,它们之间存在继承关系。 Servlet是JavaWeb的三大组件之一,它可以处理客户端的请求并生成动态的Web内容。Servlet可以通过实现javax.servlet.Servlet接口来创建,也可以通过继承javax.servlet.GenericServlet类或javax.servlet.http.HttpServlet类来创建。\[1\] GenericServlet是一个抽象类,它实现了javax.servlet.Servlet接口,并提供了一些通用的方法和功能。如果我们需要创建一个通用的Servlet,可以继承GenericServlet类并重写其中的方法。\[3\] HttpServletGenericServlet的子类,它进一步扩展了GenericServlet的功能,专门用于处理HTTP请求和响应。如果我们需要创建一个处理HTTP请求的Servlet,通常会继承HttpServlet类并重写其中的doGet()和doPost()方法。\[2\] 所以,可以说HttpServletGenericServlet的子类,而GenericServletServlet接口的实现类。这三者之间存在继承关系,同时也代表了Servlet在不同场景下的不同实现方式。 #### 引用[.reference_title] - *1* *2* *3* [ServletGenericServletHttpServlet之间的关系](https://blog.csdn.net/qq_40645822/article/details/101314753)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值