HttpServlet
2015-08-30 16:26
323 查看
The
The
To respond to e.g. HTTP GET requests only, you will extend the
The
Most often you just want to respond to either HTTP GET or POST requests, so you just override these two methods.
If you want to handle both GET and POST request from a given servlet, you can override both methods, and have one call the other. Here is how:
I would recommend you to use the
javax.servlet.http.HttpServletclass is a slightly more advanced base class than the
GenericServletshown in the Simple Servlet example.
The
HttpServletclass reads the HTTP request, and determines if the request is an HTTP GET, POST, PUT, DELETE, HEAD etc. and calls one the corresponding method.
To respond to e.g. HTTP GET requests only, you will extend the
HttpServletclass, and override the
doGet()method only. Here is an example:
public class SimpleHttpServlet extends HttpServlet { protected void doGet( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.getWriter().write("<html><body>GET response</body></html>"); } }
The
HttpServletclass has methods you can override for each HTTP method (GET, POST etc.). Here is a list of the methods you can override:
doGet() doPost() doHead() doPut() doDelete() doOptions() doTrace()
Most often you just want to respond to either HTTP GET or POST requests, so you just override these two methods.
If you want to handle both GET and POST request from a given servlet, you can override both methods, and have one call the other. Here is how:
public class SimpleHttpServlet extends HttpServlet { protected void doGet( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } protected void doPost( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.getWriter().write("GET/POST response"); } }
I would recommend you to use the
HttpServletinstead of the
GenericServletwhenever possible. HttpServlet is easier to work with, and has more convenience methods than
GenericServlet.
相关文章推荐
- 使用GenyMotion模拟器+抓包工具SRSniffer分析网络请求
- HttpServletRequest和HttpServletResponse详解
- POJ1459-Power Network-网络流-最大流(EK模板题)
- Linux网络栈剖析—从socket到设备驱动程序
- hdu 4741 2013杭州赛区网络赛 dfs ***
- (转)HTTP协议详解
- ios开发进阶之网络04 数据解析 文件下载上传
- Android网络编程之使用post方式提交数据
- 个人IHttpHandler,IHttpModule认识
- 防止SYN泛洪攻击 开启路由器的TCP拦截
- openwrt linux portal 实现 支持 https 支持基于时长和流量控制
- HTTP状态码含义
- android之ConnectivityManager简介,网络连接状态
- TCP协议的一些问题
- HTTP协议--处理状态和返回状态码含义
- HTTP 错误 500.21 - Internal Server Error 处理程序“PageHandlerFactory-Integrated”在其模块列表中有一个错误模块“ManagedPipelineHandler”
- Android网络编程之使用get方式向服务端提交数据和乱码问题的解决
- (转)TCP协议那些事
- 【python】socket网络编程
- perl脚本实时监测重要网络设备以及服务器的连通性 推荐