HttpServlet

原创 2018年04月16日 22:59:44
1)是一个Servlet,继承自GenericServlet,针对于HTTP协议所定制
2)在service()方法中直接把ServletRequest和ServletResponse转为HTTPServletRequest和HTTPServletResponse
并调用了重载的service(HttpServletRequest , HttpServletResponse)
在service(HttpServletRequest , HttpServletResponse)获取了请求方式:request.getMethod().根据请求方式创建了
doXxx()方法(Xxx为具体的请求方式,比如doGet和doPost)
public class MyHttpServlet extends MyGenericServlet {
3)实际开发中,直接继承HTTPServlet,并根据请求方式复写doXxx()方法即可
4)好处:直接有针对性的覆盖doXxx()方法,直接 使用HttpServletRequest 和 HttpServletResponse,不再需要强转


@Override
public void service(ServletRequest arg0, ServletResponse arg1) throws ServletException, IOException {
  
if (arg0  instanceof HttpServletRequest) {
HttpServletRequest request=(HttpServletRequest) arg0;

if (arg1 instanceof HttpServletResponse) {
HttpServletResponse response=(HttpServletResponse) arg1;
  
service(request, response);

}
}


}

public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1获取请求方式
String method=request.getMethod();
//2根据请求方式再调用对应的处理方法
if ("GET".equalsIgnoreCase(method)) {
doGet(request,response);

}else if ("POST".equalsIgnoreCase(method)) {
doPost(request,response);

}

}


public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
// TODO Auto-generated method stub

}


public  void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub

}
}

HttpServlet 详解(基础)

HttpServlet详解大家都知道Servlet,但是不一定很清楚servlet框架,这个框架是由两个Java包组成:javax.servlet和javax.servlet.http. 在javax...
  • FANGAOHUA200
  • FANGAOHUA200
  • 2016-11-04 12:26:28
  • 930

【Servlet学习】继承HttpServlet简单示例

package com.sprying; import java.io.IOException; import java.io.PrintWriter; import javax.servl...
  • sprying
  • sprying
  • 2012-03-31 18:27:31
  • 3200

HttpServlet.init 的两种版本

public class SomeClass extends HttpServlet { public void init() throws ServletException { super.i...
  • yasi_xi
  • yasi_xi
  • 2014-05-22 10:21:35
  • 2061

Servlet系列之第三种创建方式(三)--实现HttpServlet类

package servlet;import javax.servlet.ServletContext; import javax.servlet.ServletException; import j...
  • dapyandxpy
  • dapyandxpy
  • 2017-06-26 08:57:40
  • 338

重温模糊知识点之继承httpServlet为什么不重写service方法

存在这个一个jichengu
  • zhouren1314
  • zhouren1314
  • 2014-04-12 16:54:11
  • 1237

为什么eclipse不识别HttpServlet

为什么eclipse不识别HttpServlet
  • GyaoD
  • GyaoD
  • 2016-11-15 21:12:15
  • 1178

servlet.jar,httpservlet架包

  • 2018年02月01日 15:46
  • 68KB
  • 下载

Servlet—HttpServlet、Servlet线程安全

1. HttpServlet Servlet接口SUN公司定义了两个默认实现类,分别为:GenericServlet、HttpServlet。HttpServlet指能够处理HTTP请求的servle...
  • Xw_Classmate
  • Xw_Classmate
  • 2016-02-21 17:36:19
  • 723

Servlet入门----子类HttpServlet类

HttpServlet类该类是GenericServlet的子类,只用于接收Http请求和响应,一般情况下,web开发中,需要使用的Servlet均只需要实现该类即可,并且实现时无特殊情况只需要覆写该...
  • Hangele
  • Hangele
  • 2016-10-26 10:42:42
  • 1472

揭秘HttpServlet抽象类并不抽象!

         揭秘HttpServlet抽象类并不抽象!       extends javax.servlet.http.HttpServlet的类就是Servlet。       API文档显...
  • shendl
  • shendl
  • 2005-06-14 15:52:00
  • 3385
收藏助手
不良信息举报
您举报文章:HttpServlet
举报原因:
原因补充:

(最多只允许输入30个字)