📄 httpservlet.java
字号:
* @param resp an {@link HttpServletResponse} object that
* contains the response the servlet sends
* to the object
*
* @exception IOException if an input or output error is
* detected when the servlet handles
* the request
*
* @exception ServletException if the request for the POST
* could not be handled
*
*
* @see javax.servlet.ServletOutputStream
* @see javax.servlet.ServletResponse#setContentType
*
*
*/
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);
}
}
/**
* Receives an HTTP PUT request from the protected
* <code>service</code> method and handles the
* request. The PUT operation allows a client to
* place a file on the server and is similar to
* sending a file by FTP.
*
* <p>If you override this method, your method must 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 2068
* <a href="http://info.internet.isi.edu:80/in-notes/rfc/files/rfc2068.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. If you
* override this method, you may want 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);
}
}
/**
*
* Receives an HTTP DELETE request from the protected
* <code>service</code> method and handles the
* 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. If you override this
* method, you may want to save a copy of the affected
* page 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 Method [] getAllDeclaredMethods (Class c) {
if (c.getName().equals("javax.servlet.http.HttpServlet"))
return null;
int j=0;
Method [] parentMethods = getAllDeclaredMethods(c.getSuperclass());
Method [] thisMethods = c.getDeclaredMethods();
if (parentMethods!=null) {
Method [] allMethods =
new Method [parentMethods.length + thisMethods.length];
for (int i=0; i<parentMethods.length; i++) {
allMethods[i]=parentMethods[i];
j=i;
}
j++;
for (int i=j; i<thisMethods.length+j; i++) {
allMethods[i] = thisMethods[i-j];
}
return allMethods;
}
return thisMethods;
}
/**
*
* Receives an HTTP OPTIONS request from the protected
* <code>service method and handles the 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>You do not need to override this method unless your
* 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);
}
/**
*
* Receives an HTTP TRACE request from the protected
* <code>service</code> method and handles the request.
* HTTP TRACE returns the headers sent with the TRACE
* request to the client, so that they can be used in
* debugging. You do not usually 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. You will probably
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -