doTrace()詳解
阿新 • • 發佈:2018-12-29
由伺服器(通過<code>service</code>方法)呼叫,以允許servlet處理TRACE請求。TRACE將隨TRACE請求一起傳送的報頭返回給客戶端,以便用於除錯。不需要重寫此方法。
原始碼:
/** * 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"; StringBuilder buffer = new StringBuilder("TRACE ").append(req.getRequestURI()) .append(" ").append(req.getProtocol()); Enumeration<String> reqHeaderEnum = req.getHeaderNames(); while( reqHeaderEnum.hasMoreElements() ) { String headerName = reqHeaderEnum.nextElement(); buffer.append(CRLF).append(headerName).append(": ") .append(req.getHeader(headerName)); } buffer.append(CRLF); responseLength = buffer.length(); resp.setContentType("message/http"); resp.setContentLength(responseLength); ServletOutputStream out = resp.getOutputStream(); out.print(buffer.toString()); out.close(); return; }
主要用於測試!