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:-
- 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.
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 requestsdoPost, for HTTP POST requestsdoPut, for HTTP PUT requestsdoDelete, for HTTP DELETE requestsinitanddestroy, to manage resources that are held for the life of the servletgetServletInfo, 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 Servlet, ServletConfig, 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().
|
This is how
|
GenericServlet vs HttpServlet的更多相关文章
- servlet实现的三种方式对比(servlet 和GenericServlet和HttpServlet)
第一种: 实现Servlet 接口 第二种: 继承GenericServlet 第三种 继承HttpServlet (开发中使用) 通过查看api文档发现他们三个(servlet 和GenericSe ...
- Servlet的 GenericServlet 和 HttpServlet
一.GenericServlet? servlet 是一个接口 下面有两个抽象类 GenericServlet 和 HttpServlet 需要被 继承并重写其中的方法. package javawe ...
- servlet、genericservlet、httpservlet之间的区别
转自:http://blog.csdn.net/rat9912345/article/details/5161789 当编写一个servlet时,必须直接或间接实现servlet接口,最可能实现的方法 ...
- Servlet,GenericServlet和HttpServlet的继承关系
HttpServlet是GenericServlet的子类. GenericServlet是个抽象类,必须给出子类才能实例化.它给出了设计servlet的一些骨架,定义了servlet生命周期,还有一 ...
- servlet、genericservlet、httpservlet之间的区别(转)
当编写一个servlet时,必须直接或间接实现servlet接口,最可能实现的方法就是扩展javax.servlet.genericservlet或javax.servlet.http.httpser ...
- Javaweb学习笔记——(九)——————Servlet的进入,及ServletConfig、GenericServlet、HttpServlet、ServletContext、获取类路径资源
Servlet1.什么是Servlet? *Servlet是Javaweb三大组件之一(Servlet,Filter,Listener) *Servlet是用来处理客户端请求的动态资源 *Servle ...
- Servlet】(2)有关Servlet实现的几个类:GenericServlet、HttpServlet、ServletConfig、ServletContext
一.GenericServlet 1.所有的成员方法: 1.在javaWeb项目中: 2.web.xml <?xml version="1.0" encoding=" ...
- GenericServlet与HttpServlet
1.HttpServlet 1). 是一个 Servlet, 继承自 GenericServlet. 针对于 HTTP 协议所定制. 2). 在 service() 方法中直接把 ServletReu ...
- GenericServlet和HttpServlet有什么区别?
1.HttpServlet 1). 是一个 Servlet, 继承自 GenericServlet. 针对于 HTTP 协议所定制. 2). 在 service() 方法中直接把 ServletReu ...
- Servlet , GenericServlet和HttpServlet
Servlet是一套规范,表现为一套接口,留给开发人员去实现,Servlet接口定义如下(附加servlet-api source来查看源码) 其中init方法被Servlet容器调用,servlet ...
随机推荐
- Django项目:CRM(客户关系管理系统)--79--69PerfectCRM实现CRM业务流程(bpm)学生讲师分页
# student_views.py # ————————60PerfectCRM实现CRM学生上课记录———————— from django.shortcuts import render #页面 ...
- vue.js_08_vue-组件的定义
1.vue组件常用定义方式 <body> <div id="app"> <!--1.3使用组件--> <mycom1></my ...
- 【agc013d】AtCoder Grand Contest 013 D - Piling Up
题意 盒子里有n块砖,每块的颜色可能为蓝色或红色. 执行m次三步操作: 1.从盒子里随便拿走一块砖 2.放入一块蓝砖和红砖到盒子里 3.从盒子里随便拿走一块砖 给定n,m 问拿出来的砖,可能有多少种不 ...
- PHP 缓存详解
为什么要使用缓存 一个网站或者一个应用的标准流程是浏览器向应用服务器发出请求,应用服务器做一些计算和逻辑判断之后再请求数据库,数据库收到请求后在经过计算将数据返回给应用服务器,应用服务器再次计算后把数 ...
- 使用powerDesigner绘制类图
使用powerDesigner绘制类图 因为后面要理清楚spring中类与类之间关系,我们来看看如何使用powerDesigner绘制类图 你要去下载powerDesiger,一路下一步安装好 打开, ...
- 如何实现一个HTTP请求库——axios源码阅读与分析 JavaScript
概述 在前端开发过程中,我们经常会遇到需要发送异步请求的情况.而使用一个功能齐全,接口完善的HTTP请求库,能够在很大程度上减少我们的开发成本,提高我们的开发效率. axios是一个在近些年来非常火的 ...
- JAVA面试常见问题之开源框架和容器篇
1.Servlet的生命周期 加载:加载到虚拟机 初始化:init() 一个生命周期中只会被调用一次. 服务:service() 销毁:destroy() 2.转发与重定向的区别 转发在服务器端完成的 ...
- 洛谷P3298 泉
时空限制 1000ms / 128MB 题目描述 作为光荣的济南泉历史研究小组中的一员,铭铭收集了历史上x个不同年份时不同泉区的水流指数,这个指数是一个小于. 2^30的非负整数.第i个年份时六个泉区 ...
- git与github建立链接(学习笔记)
总结步骤: 1.将所有文件添加到本库 git add . 2. git commit -m "提示信息随便写" 3.查看git修改状态 git status 4.获取远程库与本地同 ...
- freopen() 函数的使用
当我们求解acm题目时,通常在设计好算法和程序后,要在调试环境(例如VC等)中运行程序,输入测试数据,当能得到正确运行结果后,才将程序提交到oj中.但由于调试往往不能一次成功,每次运行时,都要重新输入 ...