zoukankan      html  css  js  c++  java
  • HttpServlet

    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) {
        }
        
    }
  • 相关阅读:
    spring boot 2 统一异常处理
    spring boot 2.0.4 Redis缓存配置
    windows下consul利用json文件注册服务
    docker安装portainer
    Docker 清理命令
    git command line 提交代码
    java应用健康检查
    springboot @Value获取值为空,解决办法
    解决You have new mail in /var/spool/mail/root提示
    kafka之kafka的伪分布式安装
  • 原文地址:https://www.cnblogs.com/yangHS/p/10926605.html
Copyright © 2011-2022 走看看