What is the difference between GenericServlet, HttpServlet and a Servlet?
2015-08-31 03:26
776 查看
Servletis an interface defining what a servlet must implement.
GenericServletis just that, a generic, protocol-independent servlet.
HttpServletis a servlet tied specifically to the HTTP protocol.
In general, you’d extend
HttpServletto implement an application’s web layer.
You might implement
Servletif you’re writing your own container or handling everything yourself. You might extend
GenericServletto handle a different protocol, but you might not.
javax.servlet.Servletis interface, it defines methods for all the implementations - that’s what interfaces usually do.
javax.servlet.GenericServletis 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.HttpServletis abstract class to be extended if you want to communicate over HTTP protocol. Most likely you only have to care about this one.
Servletis 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
GenericServletdefines a generic, protocol-independent servlet.
GenericServletgives a blueprint and makes writing servlet easier.
GenericServlet provides simple versions of the life-cycle methods
initand
destroyand of the methods in the
ServletConfiginterface.
GenericServlet implements the log method, declared in the
ServletContextinterface.
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.
HttpServletis 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.
GenericServletis the basic, protocol-neutral implementation of the Servlet interface. Often you’ll find similar basic implementations of interfaces in an API; in this case
GenericServletadds a bit of functionality to the Servlet API:
getServletName,
getServletInfo, and pass-through methods for the servlet
initparameters.
HttpServletbenefits from these additions by extending
GenericServlet.
Generally everyone coding against this API is using
HttpServletfor implementing HTTP web services, but one can also extend or use
GenericServletfor implementing server/service functionality using a custom protocol, or another extant protocol, for example, FTP.
Servlet:-
The
Servletsruns 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.
Servletis platform independent.
Servletis 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.
相关文章推荐
- HTTP网络协议(三)
- 记录一次MVC 3.0错误 HTTP 404您正在查找的资源(或者它的一个依赖项)可能已被移除,或其名称已更改,或暂时不可用。请检查以下 URL 并确保其拼写正确。
- TCP协议
- matlab 神经网络常用操作
- VIEWGOOD(远古)网络电视台的应用
- android通过httpClient请求获取JSON数据并且解析
- HtmlUnit 网络爬虫 菜鸟的学习笔记(一)
- Kail Linux渗透测试教程之网络扫描和嗅探工具Nmap
- 计算机网络面试整理【转】
- 网络部署记
- 外军网络空间作战简报
- 构建战略网络防御:网络曼哈顿计划蓝图
- 网络协议之访问服务端的三种方式
- 靠谱助手 http://www.kpzs.com/
- 关于c#visio 网络监控案例遇到的问题
- muduo网络库源码学习————日志滚动
- VMware虚拟机中nat网络模式的设置方法
- HTTP状态码
- 计算机网络之面试常考
- HLG 1494网络 (求的割点数目)可做模板