模板方法模式(TemplateMethod) (四)

2014-11-24 11:19:58 · 作者: · 浏览: 3
Object[] errArgs = new Object[1];
errArgs[0] = method;
errMsg = MessageFormat.format(errMsg, errArgs);

resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED, errMsg);
}
}

}

public abstract class HttpServlet extends GenericServlet {


private static final String METHOD_DELETE = "DELETE";
private static final String METHOD_GET = "GET";
private static final String METHOD_POST = "POST";


/**
* Does nothing, because this is an abstract class.
*/
public HttpServlet() {
// NOOP
}

protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
String protocol = req.getProtocol();
String msg = lStrings.getString("http.method_get_not_supported");
if (protocol.endsWith("1.1")) {
resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, msg);
} else {
resp.sendError(HttpServletResponse.SC_BAD_REQUEST, msg);
}
}

protected void doHead(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {

NoBodyResponse response = new NoBodyResponse(resp);

doGet(req, response);
response.setContentLength();
}


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);
}
}



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);
}
}



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);
}
}

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);