HttpServlet

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
上一篇GenericServlet
下一篇JSP
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭