Session Technology-Displays the last access time for a user (17)

Source: Internet
Author: User
Tags current time
Package Com.ken.cookie;

Import java.io.IOException;
Import javax.servlet.ServletException;
Import Javax.servlet.http.Cookie;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;

Import Javax.servlet.http.HttpServletResponse;

	public class Getcookieservlet extends HttpServlet {private static final long serialversionuid = 1L;

		protected void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {

		Obtaining data for cookies carried by the client cookie[] cookies = request.getcookies ();
			Obtain the desired cookie by the cookie name for (cookie cookie:cookies) {//Get the name of the cookie String cookiename = Cookie.getname ();
				if (cookiename.equals ("name")) {//Gets the value of the cookie String cookievalue = Cookie.getvalue ();
			System.out.println (Cookievalue); }}} protected void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOE
	xception {doget (request, response); }
}

Operation Result:




Show user's last access time:


Package com.ken.lastAccessTime;
Import java.io.IOException;
Import Java.text.SimpleDateFormat;

Import Java.util.Date;
Import javax.servlet.ServletException;
Import Javax.servlet.http.Cookie;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;

Import Javax.servlet.http.HttpServletResponse;

	public class Lastaccesstimeservlet extends HttpServlet {private static final long serialversionuid = 1L;

		protected void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {
		Gets the current time date date = new Date ();
		SimpleDateFormat format = new SimpleDateFormat ("Yyyy-mm-dd hh:mm:ss");

		String currenttime = Format.format (date);
		1. Create a cookie that records the current latest access time cookie cookie = new Cookie ("LastAccessTime", currenttime);
		Cookie.setmaxage (60 * 10 * 500);

		Response.addcookie (cookie);
		2. Obtain the cookie that the client carries-----lastaccesstime String lastaccesstime = null;
		cookie[] cookies = request.getcookies (); if (cookies!)= null) {for (Cookie coo:cookies) {if (Coo.getname (). Equals ("LastAccessTime")) {LastAccessTime = Coo.getv
				Alue ();
		}}} response.setcontenttype ("Text/html;charset=utf-8");
		if (LastAccessTime = = null) {Response.getwriter (). Write ("You are the first to visit");
		} else {response.getwriter (). Write ("Your last access Time was:" + currenttime); }} protected void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, Ioexcep
	tion {doget (request, response); }
}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.