• 覆盖servlet getLastModified()方法


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



    }

  • 相关阅读:
    Ubuntu 系统装机指南
    java读取配置文件属性
    反转单链表 递归与非递归
    迟到的2013年终总结
    2014年阅读资料总结
    程序人生的四个象限和两条主线
    查找单链表中倒数第k个结点
    技术人员应真正学会的第二课程
    Linux“七大蠢”收录
    postman测试方法,出现400错误码
  • 原文地址:https://www.cnblogs.com/xiaona19841010/p/5153828.html
Copyright © 2020-2023  润新知