javaweb 在页面中通过setheader请求头设置动态时钟

  • Post author:
  • Post category:java


package com.my.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

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 ResponseRefreshHeadServlet
 */
@WebServlet("/ResponseRefreshHeadServlet")
public class ResponseRefreshHeadServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ResponseRefreshHeadServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//利用设置响应消息头设置动态时钟的效果
		response.setContentType("text/html;charset=UTF-8");
		response.setHeader("refresh", "1");
		
		PrintWriter out = response.getWriter();
		out.println("现在时间是:");
		SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-DD- HH:mm:ss");
		out.println(sdf.format(new Date()));
		System.out.println("yes");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}



版权声明:本文为xizi_ghq原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。