深入体验JavaWeb开发内幕——由创建Cookie到通过Cookie获取用户上次访问某站点时间的简单应用



        当客户端发出请求时,服务器在处理客户请求并将处理结果返回给客户的同时还会额外的创建一个Cookie返还给客户端,以便该客户端下次再访问服务器时服务器可以很快的知道是哪个用户在访问该网站,利用Cookie的这个特性,我们就可以实现一些现实生活中会应用到的应用了。如:购物网站、用户的登陆信息保存等。

一、模拟服务器创建Cookie到客户端

        下面,我们来通过程序模拟服务器创建一个Cookie发送给客户端:

     
MakeCookie .java
package net.csdn.cookie;

import java.io.IOException;
import java.io.PrintWriter;
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 MakeCookie extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		makeCookie(response);
		
	}


	private void makeCookie(HttpServletResponse response) throws IOException {
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		
		Cookie cookie = new Cookie("username","lijizh1013");
		cookie.setMaxAge(24*3600);
		cookie.setPath("/Cookie/MakeCookie");
		response.addCookie(cookie);
		out.println("Cookie已经写入到客户端!");
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}

效果如图:

在本地磁盘上的Cookies文件夹下便会产生一个相应的扩展名为.txt的文件,该文件就是服务器端返还给客户端的cookie。

 

二、通过Cookie获取客户端传过来的信息

package net.csdn.cookie;

import java.io.IOException;
import java.io.PrintWriter;
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 MakeCookie extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		getCookie(request, response);
	}

	private void getCookie(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		Cookie [] cookies = request.getCookies();
		for(int i=0;cookies!=null && i
如图:

     

三、通过Cookie获取用户上次浏览该网站的具体时间

package net.csdn.cookie;

import java.io.IOException;
import java.io.PrintWriter;
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 MakeCookie extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

	response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		out.println("你上次访问的时间是:");
		Cookie [] cookies = request.getCookies();
		for(int i=0;cookies!=null && i

效果如图:

当第一次访问时;

再次访问:


好了,赶快自己试一试吧!

你可能感兴趣的:(深入体验JavaWeb开发内幕——由创建Cookie到通过Cookie获取用户上次访问某站点时间的简单应用)