码迷,mamicode.com
首页 > 其他好文 > 详细

覆盖servlet getLastModified()方法

时间:2016-01-23 21:14:57      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:

package com.helloweenvsfei.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* Servlet implementation class FirstServlet
*/
@WebServlet("/firstServlet")
public class FirstServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
/*
* 以get方式访问页面时执行getLastModified()函数,执行doGet(request,response)前会先执行getLastModified,如果浏览器发现其返回值与上次文章地返回值相同,则认为该文档没有更新,浏览器估用缓存而不执行doGet()若返回-1,则认为是时刻更新,总是执行该函数
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.log("执行doGet()方法");//调用 Servlet自带的日志输出信息到控制台
this.execute(request,response);
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.log("执行doPost()方法");
this.execute(request,response);
}

private void execute(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException{
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html");
PrintWriter out=response.getWriter();
String method=request.getMethod();//访问方式
String param=request.getParameter("param");
String requestURI=request.getRequestURI();//访问SERVLET的URI
out.println(request.getRequestURI());//输出到客户端servlet URI路径
out.println("<!DOCTYPE html PUBLIC ‘-//W3C//DTD HTML 4.01 Transitional//EN‘ ‘http://www.w3.org/TR/html4/loose.dtd‘><html><head><meta http-equiv=‘Content-Type‘ content=‘text/html; charset=UTF-8‘><title>firstWeb</title></head><body>");
out.println("以"+request.getMethod()+"方式访问该页面。取到的param参数为:"+request.getParameter("param")+"<br/>");
out.println("<form action=‘"+request.getRequestURI()+"‘ method=‘get‘ ><input type=‘text‘ name=‘param‘ value=‘param string‘/><input type=‘submit‘ value=‘以GET 方式访问"+request.getRequestURI()+"‘></form>");
out.println("<form action=‘"+request.getRequestURI()+"‘ method=‘post‘ ><input type=‘text‘ name=‘param‘ value=‘param string‘/><input type=‘submit‘ value=‘以POST 方式访问"+request.getRequestURI()+"‘></form>");
//由客户端浏览器读取该文档的时间
out.println("<script>document.write(‘本页面最后更新时间:‘+document.lastModified);</script>");
out.println("</body></html>");
out.flush();
out.close();
}
/*
* 返回Servlet生成文档的更新时间,对get方式访问有效,返回时间相对于1970-1-1 8点的毫秒数如果为-1则为实时更新本例将其设为-1
*/
@Override//覆盖父类方法
public long getLastModified(HttpServletRequest request){
this.log("执行getLastModified方法!");
return -1;
}



}

覆盖servlet getLastModified()方法

标签:

原文地址:http://www.cnblogs.com/xiaona19841010/p/5153828.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!