📄 httpservlet.java
字号:
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); } } /** * Called by the server (via the <code>service</code> method) * to allow a servlet to handle a PUT request. * * The PUT operation allows a client to * place a file on the server and is similar to * sending a file by FTP. * * <p>When overriding this method, leave intact * any content headers sent with the request (including * Content-Length, Content-Type, Content-Transfer-Encoding, * Content-Encoding, Content-Base, Content-Language, Content-Location, * Content-MD5, and Content-Range). If your method cannot * handle a content header, it must issue an error message * (HTTP 501 - Not Implemented) and discard the request. * For more information on HTTP 1.1, see RFC 2616 * <a href="http://www.ietf.org/rfc/rfc2616.txt"></a>. * * <p>This method does not need to be either safe or idempotent. * Operations that <code>doPut</code> performs can have side * effects for which the user can be held accountable. When using * this method, it may be useful to save a copy of the * affected URL in temporary storage. * * <p>If the HTTP PUT request is incorrectly formatted, * <code>doPut</code> returns an HTTP "Bad Request" message. * * * @param req the {@link HttpServletRequest} object that * contains the request the client made of * the servlet * * @param resp the {@link HttpServletResponse} object that * contains the response the servlet returns * to the client * * @exception IOException if an input or output error occurs * while the servlet is handling the * PUT request * * @exception ServletException if the request for the PUT * cannot be handled * */ protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String protocol = req.getProtocol(); String msg = lStrings.getString("http.method_put_not_supported"); if (protocol.endsWith("1.1")) { resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, msg); } else { resp.sendError(HttpServletResponse.SC_BAD_REQUEST, msg); } } /** * * Called by the server (via the <code>service</code> method) * to allow a servlet to handle a DELETE request. * * The DELETE operation allows a client to remove a document * or Web page from the server. * * <p>This method does not need to be either safe * or idempotent. Operations requested through * DELETE can have side effects for which users * can be held accountable. When using * this method, it may be useful to save a copy of the * affected URL in temporary storage. * * <p>If the HTTP DELETE request is incorrectly formatted, * <code>doDelete</code> returns an HTTP "Bad Request" * message. * * * @param req the {@link HttpServletRequest} object that * contains the request the client made of * the servlet * * * @param resp the {@link HttpServletResponse} object that * contains the response the servlet returns * to the client * * * @exception IOException if an input or output error occurs * while the servlet is handling the * DELETE request * * @exception ServletException if the request for the * DELETE cannot be handled * */ protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String protocol = req.getProtocol(); String msg = lStrings.getString("http.method_delete_not_supported"); if (protocol.endsWith("1.1")) { resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, msg); } else { resp.sendError(HttpServletResponse.SC_BAD_REQUEST, msg); } } private static Method[] getAllDeclaredMethods(Class c) { if (c.equals(javax.servlet.http.HttpServlet.class)) { return null; } Method[] parentMethods = getAllDeclaredMethods(c.getSuperclass()); Method[] thisMethods = c.getDeclaredMethods(); if ((parentMethods != null) && (parentMethods.length > 0)) { Method[] allMethods = new Method[parentMethods.length + thisMethods.length]; System.arraycopy(parentMethods, 0, allMethods, 0, parentMethods.length); System.arraycopy(thisMethods, 0, allMethods, parentMethods.length, thisMethods.length); thisMethods = allMethods; } return thisMethods; } /** * Called by the server (via the <code>service</code> method) * to allow a servlet to handle a OPTIONS request. * * The OPTIONS request determines which HTTP methods * the server supports and * returns an appropriate header. For example, if a servlet * overrides <code>doGet</code>, this method returns the * following header: * * <p><code>Allow: GET, HEAD, TRACE, OPTIONS</code> * * <p>There's no need to override this method unless the * servlet implements new HTTP methods, beyond those * implemented by HTTP 1.1. * * @param req the {@link HttpServletRequest} object that * contains the request the client made of * the servlet * * * @param resp the {@link HttpServletResponse} object that * contains the response the servlet returns * to the client * * * @exception IOException if an input or output error occurs * while the servlet is handling the * OPTIONS request * * @exception ServletException if the request for the * OPTIONS cannot be handled * */ protected void doOptions(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { Method[] methods = getAllDeclaredMethods(this.getClass()); boolean ALLOW_GET = false; boolean ALLOW_HEAD = false; boolean ALLOW_POST = false; boolean ALLOW_PUT = false; boolean ALLOW_DELETE = false; boolean ALLOW_TRACE = true; boolean ALLOW_OPTIONS = true; for (int i=0; i<methods.length; i++) { Method m = methods[i]; if (m.getName().equals("doGet")) { ALLOW_GET = true; ALLOW_HEAD = true; } if (m.getName().equals("doPost")) ALLOW_POST = true; if (m.getName().equals("doPut")) ALLOW_PUT = true; if (m.getName().equals("doDelete")) ALLOW_DELETE = true; } String allow = null; if (ALLOW_GET) if (allow==null) allow=METHOD_GET; if (ALLOW_HEAD) if (allow==null) allow=METHOD_HEAD; else allow += ", " + METHOD_HEAD; if (ALLOW_POST) if (allow==null) allow=METHOD_POST; else allow += ", " + METHOD_POST; if (ALLOW_PUT) if (allow==null) allow=METHOD_PUT; else allow += ", " + METHOD_PUT; if (ALLOW_DELETE) if (allow==null) allow=METHOD_DELETE; else allow += ", " + METHOD_DELETE; if (ALLOW_TRACE) if (allow==null) allow=METHOD_TRACE; else allow += ", " + METHOD_TRACE; if (ALLOW_OPTIONS) if (allow==null) allow=METHOD_OPTIONS; else allow += ", " + METHOD_OPTIONS; resp.setHeader("Allow", allow); } /** * Called by the server (via the <code>service</code> method) * to allow a servlet to handle a TRACE request. * * A TRACE returns the headers sent with the TRACE * request to the client, so that they can be used in * debugging. There's no need to override this method. * * * * @param req the {@link HttpServletRequest} object that * contains the request the client made of * the servlet * * * @param resp the {@link HttpServletResponse} object that * contains the response the servlet returns * to the client * * * @exception IOException if an input or output error occurs * while the servlet is handling the * TRACE request * * @exception ServletException if the request for the * TRACE cannot be handled * */ protected void doTrace(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { int responseLength; String CRLF = "\r\n"; String responseString = "TRACE "+ req.getRequestURI()+ " " + req.getProtocol(); Enumeration reqHeaderEnum = req.getHeaderNames(); while( reqHeaderEnum.hasMoreElements() ) { String headerName = (String)reqHeaderEnum.nextElement(); responseString += CRLF + headerName + ": " + req.getHeader(headerName); } responseString += CRLF; responseLength = responseString.length(); resp.setContentType("message/http"); resp.setContentLength(responseLength); ServletOutputStream out = resp.getOutputStream(); out.print(responseString); out.close(); return; } /** * * Receives standard HTTP requests from the public * <code>service</code> method and dispatches * them to the <code>do</code><i>XXX</i> methods defined in * this class. This method is an HTTP-specific version of the * {@link javax.servlet.Servlet#service} method. There's no * need to override this method. * * * * @param req the {@link HttpServletRequest} object that * contains the request the client made of * the servlet * * * @param resp the {@link HttpServletResponse} object that * contains the response the servlet returns * to the client * * * @exception IOException if an input or output error occurs * while the servlet is handling the * HTTP request * * @exception ServletException if the HTTP request * cannot be handled * * @see javax.servlet.Servlet#service * */ protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -