您的位置:首页 > 运维架构

Servlet 中的Service(),doGet(),doPost()的关系

2014-10-21 10:13 127 查看
在默认的servlet中,所有的get和post请求都是通过service()处理,然后转到doGet()或者doPost()方法中的

service()方法中除了处理转向,还对get()方法进行了页面过期的判断

如果要使用该功能调用getLastModified(),默认返回-1(永远过期)

我们需要复写getLastModified()实现页面过期的逻辑.

protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
String method = req.getMethod();
if(method.equals("GET"))
{
long lastModified = getLastModified(req);
if(lastModified == -1L)
{
//如果没有复写getLastModified,则直接调用doGet()
doGet(req, resp);
} else
{
long ifModifiedSince = req.getDateHeader("If-Modified-Since");
if(ifModifiedSince < (lastModified / 1000L) * 1000L)
{
maybeSetLastModified(resp, lastModified);
doGet(req, resp);
} else
{
resp.setStatus(304);
}
}
}
...
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: