/** * Standard implementation of <code>ServletContext</code> that represents * a web application's execution environment. An instance of this class is * associated with each instance of <code>StandardContext</code>. * 代表web应用程序的执行环境。这个类的一个实例是 *与StandardContext的每个实例关联。 * @author Craig R. McClanahan * @author Remy Maucherat */ public class ApplicationContext implements ServletContext { protected static final boolean STRICT_SERVLET_COMPLIANCE;///翻译为是否严格遵守 protected static final boolean GET_RESOURCE_REQUIRE_SLASH;//我的蹩脚英语翻译为获取资源是否需要斜线。 static { STRICT_SERVLET_COMPLIANCE = Globals.STRICT_SERVLET_COMPLIANCE; String requireSlash = System.getProperty("org.apache.catalina.core.ApplicationContext.GET_RESOURCE_REQUIRE_SLASH"); if (requireSlash == null) { GET_RESOURCE_REQUIRE_SLASH = STRICT_SERVLET_COMPLIANCE; } else { GET_RESOURCE_REQUIRE_SLASH = Boolean.parseBoolean(requireSlash); } }
public static final boolean STRICT_SERVLET_COMPLIANCE =Boolean.parseBoolean(System.getProperty("org.apache.catalina.STRICT_SERVLET_COMPLIANCE", "false"));
public interface ServletRegistration extends Registration { /** * TODO * @param urlPatterns The URL patterns that this Servlet should be mapped to * @return TODO * @throws IllegalArgumentException if urlPattern is null or empty * @throws IllegalStateException if the associated ServletContext has * already been initialised */URL必须映射 public Set<String> addMapping(String... urlPatterns); public Collection<String> getMappings(); public String getRunAsRole(); public static interface Dynamic extends ServletRegistration, Registration.Dynamic { public void setLoadOnStartup(int loadOnStartup); public Set<String> setServletSecurity(ServletSecurityElement constraint); public void setMultipartConfig(MultipartConfigElement multipartConfig); public void setRunAsRole(String roleName); } }
private static final String METHOD_DELETE = "DELETE"; private static final String METHOD_HEAD = "HEAD"; private static final String METHOD_GET = "GET"; private static final String METHOD_OPTIONS = "OPTIONS"; private static final String METHOD_POST = "POST"; private static final String METHOD_PUT = "PUT"; private static final String METHOD_TRACE = "TRACE"; protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String method = req.getMethod(); //GET if (method.equals(METHOD_GET)) { long lastModified = getLastModified(req); if (lastModified == -1) { // servlet doesn't support if-modified-since, no reason // to go through further expensive logic doGet(req, resp); } else { long ifModifiedSince = req.getDateHeader(HEADER_IFMODSINCE); if (ifModifiedSince < lastModified) { // If the servlet mod time is later, call doGet() // Round down to the nearest second for a proper compare // A ifModifiedSince of -1 will always be less maybeSetLastModified(resp, lastModified); doGet(req, resp); } else { resp.setStatus(HttpServletResponse.SC_NOT_MODIFIED); } } } else if (method.equals(METHOD_HEAD)) { long lastModified = getLastModified(req); maybeSetLastModified(resp, lastModified); doHead(req, resp); } else if (method.equals(METHOD_POST)) { doPost(req, resp); } else if (method.equals(METHOD_PUT)) { doPut(req, resp); } else if (method.equals(METHOD_DELETE)) { doDelete(req, resp); } else if (method.equals(METHOD_OPTIONS)) { doOptions(req,resp); } else if (method.equals(METHOD_TRACE)) { doTrace(req,resp); //There's no need to override this method. 没有必要~ } else { // // Note that this means NO servlet supports whatever // method was requested, anywhere on this server. // String errMsg = lStrings.getString("http.method_not_implemented"); Object[] errArgs = new Object[1]; errArgs[0] = method; errMsg = MessageFormat.format(errMsg, errArgs); resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED, errMsg); } } 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(HttpServletResponse.SC_METHOD_NOT_ALLOWED, msg); } else { resp.sendError(HttpServletResponse.SC_BAD_REQUEST, msg); } }