标签:get lse header equals servet response 抽象 support exce
进行HTTP服务操作时,Servet所使用的类
Serlvet
Servlet对象的根接口,声明了提供Servlet服务所需要用到方法
ServletConfig
Serlvet配置对象接口,声明了获取Servlet配置参数的方法
ServletRequest
请求的接口,声明了请求操作的常用方法
ServletResponse
响应的接口,声明了响应的常用方法
GenericServlet
实现了Servlet和ServletConfig接口,重载了init方法,新增对application对象的访问方法
public ServletContext getServletContext() { return getServletConfig().getServletContext(); } public void init() throws ServletException {}
HttpServlet
继承了GenericServlet,重载了使用HttpServletRequst、HttpServletResponse接口使用参数的service方法,扩展了doGet和doPost方法实现业务功能
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String protocol = req.getProtocol(); String msg = lStrings.getString("http.method_get_not_supported"); if (protocol.endsWith("1.1")) { resp.sendError(405, msg); } else { resp.sendError(400, msg); } } dopost protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String protocol = req.getProtocol(); String msg = lStrings.getString("http.method_post_not_supported"); if (protocol.endsWith("1.1")) { resp.sendError(405, msg); } else { resp.sendError(400, msg); } } protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String method = req.getMethod(); if (method.equals("GET")) { long lastModified = getLastModified(req); if (lastModified == -1L) { doGet(req, resp); } else { long ifModifiedSince; try { ifModifiedSince = req.getDateHeader("If-Modified-Since"); } catch (IllegalArgumentException iae) { long ifModifiedSince; ifModifiedSince = -1L; } if (ifModifiedSince < lastModified / 1000L * 1000L) { maybeSetLastModified(resp, lastModified); doGet(req, resp); } else { resp.setStatus(304); } } } else if (method.equals("HEAD")) { long lastModified = getLastModified(req); maybeSetLastModified(resp, lastModified); doHead(req, resp); } else if (method.equals("POST")) { doPost(req, resp); } else if (method.equals("PUT")) { doPut(req, resp); } else if (method.equals("DELETE")) { doDelete(req, resp); } else if (method.equals("OPTIONS")) { doOptions(req, resp); } else if (method.equals("TRACE")) { doTrace(req, resp); } else { String errMsg = lStrings.getString("http.method_not_implemented"); Object[] errArgs = new Object[1]; errArgs[0] = method; errMsg = MessageFormat.format(errMsg, errArgs); resp.sendError(501, errMsg); } }
标签:get lse header equals servet response 抽象 support exce
原文地址:https://www.cnblogs.com/MrWuNotebook/p/10121613.html