/*
 * HitCounter.java
 *
 * Created on 2007年10月16日, 下午8:21
 */

package com.servlet;

import java.io.*;
import java.text.DateFormat;
import java.util.Date;

import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;

/**
 *
 * @author hyl
 * @version
 */
@WebServlet(name = "HitCounter", urlPatterns = {"/hitcounter"})
public class HitCounter extends HttpServlet {
    static  final  String COUNTER_KEY = "Counter";
    /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        //response.setContentType("text/html;charset=UTF-8");
        //PrintWriter out = response.getWriter();
        //TODO Method stub generated by Lomboz
//		获取会话对象
        HttpSession session = request.getSession(true);
        response.setContentType("text/html;charset=gb2312");
// Get the PrintWriter to write the response
        java.io.PrintWriter out = response.getWriter();
// 从会话中获取属性
        int count = 1;
        Integer i = (Integer) session.getAttribute(COUNTER_KEY);
// If a previous count exists, set it
        if (i != null) {
            count = i.intValue() + 1;
        }
// 将属性信息存入会话
        session.setAttribute(COUNTER_KEY, new Integer(count));
        Date lastAccessed = new Date(session.getLastAccessedTime( ));
        Date sessionCreated=new Date(session.getCreationTime());
        DateFormat formatter =
                DateFormat.getDateTimeInstance(DateFormat.MEDIUM,
                DateFormat.MEDIUM);
// Print a standard header
        out.println("<html>");
        out.println("<head>");
        out.println("<title>会话计数器</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("你的会话ID: <b>" +session.getId()+ "<br>");
        out.println("会话创建时间："+formatter.format(sessionCreated) + "<br>");
        out.println("会话上次访问时间："+formatter.format(lastAccessed) + "<br>");
        out.println("</b> 会话期间你对页面发起 <b>" +
                count +	"</b> 次请求");
        out.println("<form method=GET action=\"" +
                request.getRequestURI() + "\">");
        out.println("<input type=submit " +
                "value=\"再次单击...\">");
        out.println("</form>");
        out.println("</body>");
        out.println("</html>");
        out.flush();
        
        out.close();
    }
    
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
    
    /** Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
    
    /** Returns a short description of the servlet.
     */
    public String getServletInfo() {
        return "Short description";
    }
    // </editor-fold>
}
