标签: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) { } }
标签:equal 开发 response service nbsp script ase strong 直接
原文地址:https://www.cnblogs.com/yangHS/p/10926605.html