📄 httpservlet.java
字号:
* not 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
*
* @see javax.servlet.Servlet#service
*
*/
protected void service (HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
String method = req.getMethod ();
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 == -1) {
// if the client didn't ask for a if-modifed-since
// no need to go further -- just do it.
doGet(req, resp);
} else {
// this is the most expensive path through
// but we know we need to do it at this point
maybeSetLastModified (resp, lastModified);
long now = System.currentTimeMillis();
if (now < ifModifiedSince ||
ifModifiedSince < lastModified) {
doGet(req, resp);
} else {
// XXX
// this is more of a message than an error, but
// sendError does the job just fine. Maybe we
// should have a response.sendMessage or some
// such in a future api rev...
resp.sendError(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);
} 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);
}
}
/*
* Sets the Last-Modified entity header field, if it has not
* already been set and if the value is meaningful. Called before
* doGet, to ensure that headers are set before response data is
* written. A subclass might have set this header already, so we
* check.
*/
private void maybeSetLastModified (HttpServletResponse resp,
long lastModified) {
if (resp.containsHeader(HEADER_LASTMOD))
return;
if (lastModified >= 0)
resp.setDateHeader(HEADER_LASTMOD, lastModified);
}
/**
*
* Dispatches client requests to the protected
* <code>service</code> method. You will usually not
* 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
*
*
* @see javax.servlet.Servlet#service
*
*/
public void service(ServletRequest req, ServletResponse res)
throws ServletException, IOException
{
HttpServletRequest request;
HttpServletResponse response;
try {
request = (HttpServletRequest) req;
response = (HttpServletResponse) res;
} catch (ClassCastException e) {
throw new ServletException ("non-HTTP request or response");
}
service (request, response);
}
}
/*
* A response that includes no body, for use in (dumb) "HEAD" support.
* This just swallows that body, counting the bytes in order to set
* the content length appropriately. All other methods delegate directly
* to the HTTP Servlet Response object used to construct this one.
*/
// file private
class NoBodyResponse implements HttpServletResponse {
private HttpServletResponse resp;
private NoBodyOutputStream noBody;
private PrintWriter writer;
private boolean didSetContentLength;
// file private
NoBodyResponse (HttpServletResponse r) {
resp = r;
noBody = new NoBodyOutputStream ();
}
// file private
void setContentLength () {
if (!didSetContentLength)
resp.setContentLength (noBody.getContentLength ());
}
// SERVLET RESPONSE interface methods
public void setContentLength (int len) {
resp.setContentLength (len);
didSetContentLength = true;
}
public void setContentType (String type)
{ resp.setContentType (type); }
public ServletOutputStream getOutputStream () throws IOException
{ return noBody; }
public String getCharacterEncoding ()
{ return resp.getCharacterEncoding (); }
public PrintWriter getWriter () throws UnsupportedEncodingException
{
if (writer == null) {
OutputStreamWriter w;
w = new OutputStreamWriter (noBody, getCharacterEncoding ());
writer = new PrintWriter (w);
}
return writer;
}
// HTTP SERVLET RESPONSE interface methods
public void addCookie(Cookie cookie)
{ resp.addCookie(cookie); }
public boolean containsHeader (String name)
{ return resp.containsHeader (name); }
public void setStatus (int sc, String sm)
{ resp.setStatus (sc, sm); }
public void setStatus (int sc)
{ resp.setStatus (sc); }
public void setHeader (String name, String value)
{ resp.setHeader (name, value); }
public void setIntHeader (String name, int value)
{ resp.setIntHeader (name, value); }
public void setDateHeader (String name, long date)
{ resp.setDateHeader (name, date); }
public void sendError (int sc, String msg) throws IOException
{ resp.sendError (sc, msg); }
public void sendError (int sc) throws IOException
{ resp.sendError (sc); }
public void sendRedirect (String location) throws IOException
{ resp.sendRedirect (location); }
public String encodeURL (String url)
{ return resp.encodeURL(url); }
public String encodeRedirectURL (String url)
{ return resp.encodeRedirectURL(url); }
/**
* @deprecated As of Version 2.0, replaced by
* {@link HttpServletResponse#encodeURL}.
*
*/
public String encodeUrl (String url)
{ return this.encodeURL(url); }
/**
* @deprecated As of Version 2.0, replaced by
* {@link HttpServletResponse#encodeRedirectURL}.
*
*/
public String encodeRedirectUrl (String url)
{ return this.encodeRedirectURL(url); }
}
/*
* Servlet output stream that gobbles up all its data.
*/
// file private
class NoBodyOutputStream extends ServletOutputStream {
private static final String LSTRING_FILE =
"javax.servlet.http.LocalStrings";
private static ResourceBundle lStrings =
ResourceBundle.getBundle(LSTRING_FILE);
private int contentLength = 0;
// file private
NoBodyOutputStream () {}
// file private
int getContentLength () {
return contentLength;
}
public void write (int b) {
contentLength++;
}
public void write (byte buf [], int offset, int len)
throws IOException
{
if (len >= 0) {
contentLength += len;
} else {
// XXX
// isn't this really an IllegalArgumentException?
String msg = lStrings.getString("err.io.negativelength");
throw new IOException ("negative length");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -