java web基础 --- session


SessionTrackServlet.java

package com.web1.hello;

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

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

/**
 * Servlet implementation class SessionTrackServlet
 */
@WebServlet("/SessionTrackServlet")
public class SessionTrackServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    public static final String USER_KEY = "userName";
    public static final String VISITOR_KEY = "visitorCount";
    
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SessionTrackServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		HttpSession session = request.getSession(true);
		String sessionId = session.getId();
		//获取创建时间
		Date createDate = new Date(session.getCreationTime());
		//上次访问时间
		Date lastAccessDate = new Date(session.getLastAccessedTime());
		String userName = "gloomyfish";
		Integer visitCount = 1;
		if (session.isNew()) {
			session.setMaxInactiveInterval(120);
			session.setAttribute(USER_KEY, userName);
			session.setAttribute(VISITOR_KEY, visitCount);
		}else{
			userName = (String) session.getAttribute(USER_KEY);
			visitCount = (Integer) session.getAttribute(VISITOR_KEY);
			visitCount++;
			session.setAttribute(VISITOR_KEY, visitCount);
		}
		// start to response
		response.setHeader("Content-type", "text/html;charset=UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		
		Cookie cookie = new Cookie("JSESSIONID", sessionId);
		cookie.setHttpOnly(true);
		response.addCookie(cookie);
		
		PrintWriter  writer = response.getWriter();
		startWebPage(writer);
		
		writer.print("

"); writer.print("Session ID:" + sessionId); writer.print("

"); writer.print("

"); writer.print("创建时间:" + createDate.toString()); writer.print("

"); writer.print("

"); writer.print("上次访问时间:" + lastAccessDate.toString()); writer.print("

"); writer.print("

"); writer.print("访问次数:" + visitCount); writer.print("

"); writer.print("

"); writer.print("访问用户:" + userName); writer.print("

"); endWebPage(writer); } /** * @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); } private void startWebPage(PrintWriter writer){ writer.print(""); writer.print(""); writer.print(""); writer.print("Session Tracking Demo..."); writer.print(""); writer.print(""); writer.print(""); writer.print("

Session Tracking Example

"); } private void endWebPage(PrintWriter writer){ writer.print(""); writer.print(""); } }


web.xml



  
		 Hello World Servlet
	
  Hello World Servlet
  
    Encode Filter
    com.web1.util.RequestEncodeFilter
  
  
    Encode Filter
    /*
  
  
    URLOverFilter Filter
    com.web1.util.URLOverWriteFilter
  
  
    URLOverFilter Filter
    /home.html
  
  
  
    helloWorld
    com.web1.hello.HelloWorldServlet
  
  
    helloWorld
    /helloWorld
  
  
  
    helloImage
    com.web1.hello.ImageServlet
  
  
    helloImage
    /helloImage
  
  
  
    Session Track
    com.web1.hello.SessionTrackServlet
  
  
    Session Track
    /sessionTrack
  


java web基础 --- session_第1张图片

你可能感兴趣的:(JavaWeb)