码迷,mamicode.com
首页 > Web开发 > 详细

HttpServlet

时间:2019-05-26 18:03:32      阅读:108      评论:0      收藏:0      [点我收藏+]

标签:equal   开发   response   service   nbsp   script   ase   strong   直接   

HttpServlet

1)是一个Servlet,继承自GenericServlet。针对于HTTP协议所制定

2)在service()方法中直接把ServletRequest和ServletResponse转为HttpServletRequest和HttpServletResponse,并调用了重载的service(HttpServletRequest,HttpServletResponse)

3)在service(HttpServletRequest,HttpServletResponse)获取了请求方式:request.getMethod().根据请求方式创建了doXxx()方法(xxx为具体的请求方式,比如doGet,doPost)

4)实际开发中,直接继承HttpServlet,并根据请求方式复写doXxx()方法

5)优点:直接有针对性的覆盖doXxx()方法

 

public class MyHttpServlet extends GenericServlet{
    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        HttpServletRequest request;
        HttpServletResponse response;
        try {
            request = (HttpServletRequest) servletRequest;
            response = (HttpServletResponse) servletResponse;
        }catch (ClassCastException e){
            throw new ServletException("non-HTTP request or response");
        }
        service(request,response);
    }
    public void service(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse){
        String method = httpServletRequest.getMethod();
        if ("GET".equalsIgnoreCase(method)){
            doGet(httpServletRequest,httpServletResponse);
        }else if ("POST".equalsIgnoreCase(method)){
            doPost(httpServletRequest,httpServletResponse);
        }
    }

    public void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
    }

    public void doPost(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
    }
    
}

 

HttpServlet

标签:equal   开发   response   service   nbsp   script   ase   strong   直接   

原文地址:https://www.cnblogs.com/yangHS/p/10926605.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!