程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长


+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

Cookie示例(会话跟踪)

发布于2021-05-29 19:12     阅读(575)     评论(0)     点赞(27)     收藏(5)


Cookie示例

示例使用Cookie保存用户名和密码,当用户再次登录时,在相应的文本栏显示上次登录时输入的信息。

(1) 编写用于接收用户输入的文件,在该例中,没有使用HTML文件是用一个Servlet来完成此功能,这是因为要通过Servlet去读取客户端的Cookie,HTML文件无法完成此功能。Servlet 命名为LoginServlet,代码如下:

LoginServlet.java

public class LoginServlet extends HttpServlet {
     
	/**
	 * 
	 */
	private static final long serialVersionUID = -3054516715115982849L;


	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String cookieName="userName";
		String cookiePwd="pwd";
		//获得所有cookie
		Cookie[] cookies=request.getCookies();
		String userName="";
		String pwd="";
	    String isChecked="";
	    //如果cookie数组不为null,说明曾经设置过
	    //也就是曾经登录过,那么取出上次登录的用户名,密码
	    if(cookies!=null&&cookies.length>0){
	    	//如果曾经设置过cookie,checkbox状态应该是checked
	    	isChecked="cheched";
	    	for(int i=0;i<cookies.length;i++){
	    		if(cookies[i].getName().equals(cookieName)){
	    			userName=cookies[i].getValue();
	    		}
	    		if(cookies[i].getName().equals(cookiePwd)){
	    			pwd=cookies[i].getValue();
	    		}
	    	}
	    }
	    response.setContentType("text/html;charset=GBK");
	    PrintWriter out=response.getWriter();
	    out.println("<html>\n");
	    out.println("<head><title>登录</title></head>\n");
	    out.println("<body>\n");
	    out.println("<center>\n");
	    out.println("<form action='CookieTest'"+"method='post'>\n");
	    out.print("姓名:<input type='text'"+"name='UserName'value='"+userName+"'><br/>\n");
	    out.print("密码:<input type='password'"+"name='Pwd'value='"+pwd+"'><br/>\n");
	    out.print("保存用户名和密码<input type='checkbox'"+"name='SaveCookie'value='Yes'"+isChecked+">\n");
	    out.print("<br/>\n");
	    out.print("<input type=\"submit\">\n");
	    out.print("</form>\n");
	    out.println("</center>\n");
	    out.println("</body>\n");
	    out.println("</html>\n");
	    
	    }


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

}

上述代码中,首先使用request.getCookies()获取客户端Cookie数组;再遍历该数组,找到对应的Cookie,取出用户名和密码:最后将信息显示在相应的表单控件中。

(2)新建CookieTest.java类,代码如下:

public class CookieTest extends HttpServlet {
	
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -4464988342865948406L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Cookie userCookie=new Cookie("userName",request.getParameter("UserName"));
		Cookie pwdCookie=new Cookie("pwd",request.getParameter("Pwd"));
		if(request.getParameter("SaveCookie")!=null&&request.getParameter("SaveCookie").equals("Yes")){
			userCookie.setMaxAge(7*25*60*60);
			pwdCookie.setMaxAge(7*25*60*60);
		}else{
			userCookie.setMaxAge(0);
			pwdCookie.setMaxAge(0);
			
		}
		response.addCookie(userCookie);
		response.addCookie(pwdCookie);
		PrintWriter out=response.getWriter();
		out.print("Welcome,"+request.getParameter("UserName"));
	
	}

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

}

上述代码中,首先创建两个Cookie 对象,分别用来储存表单中传递过来的登录名和密码,然后根据客户端的“SaveCookie” 元素的,决定是否向客户端发送Cookie, 或者删除以前存储的Cookie.

(3) 启动Tomcat, 在IE中访问http://localhost:8080/myapp/LoginServlet,运行结果如图示。

在这里插入图片描述

原文链接:https://blog.csdn.net/qq_50777680/article/details/117340261



所属网站分类: 技术文章 > 博客

作者:听说你没有见过我

链接:http://www.javaheidong.com/blog/article/207097/43bea9f66163d22d7ce4/

来源:java黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

27 0
收藏该文
已收藏

评论内容:(最多支持255个字符)