zoukankan      html  css  js  c++  java
  • jsp使用cookie实现记住用户名和密码

    首先说一下实现的功能:
    用户打开注册页面,最下面有个记住用户名和密码的复选框,如果勾选上,则在登录页面会自动将用户名和密码赋值到文本框中,使用java中的cookie实现,下面就是代码:
    注册页面代码(reg.jsp):

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">
        
        <title>My JSP 'reg.jsp' starting page</title>
        
    	<meta http-equiv="pragma" content="no-cache">
    	<meta http-equiv="cache-control" content="no-cache">
    	<meta http-equiv="expires" content="0">    
    	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    	<meta http-equiv="description" content="This is my page">
    	<!--
    	<link rel="stylesheet" type="text/css" href="styles.css">
    	-->
    
      </head>
      
      <body>
         <h1>注册页面</h1>
         <form action="doreg.jsp" method="post">
         	用户名:<input type="text" name="name"/><br/>
         	密码:<input type="text" name="pass"/><br/>
         	<input type="checkbox" name="jizhu"/>记住用户名和密码
         	<br/>     	
         	<input type="submit" value="注册"/>
         	<input type="reset" value="重置"/>
         </form>
      </body>
    </html>
    
    

    运行结果如图所示:
    在这里插入图片描述
    在这里插入图片描述

    点击注册的按钮时,将表单信息提交到doreg.jsp页面,下面是doreg.jsp页面的代码:

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%
    	//解决乱码
    	request.setCharacterEncoding("utf-8");
    	//获取记住密码的框是否选中
    	String jizhu = request.getParameter("jizhu");
    	if(jizhu!=null){
    		//获取值
    		String name = request.getParameter("name");
    		String pass = request.getParameter("pass");
    		//将值放在cookie里面
    		Cookie c1 = new Cookie("uname",name);
    		Cookie c2 = new Cookie("upass",pass);
    		response.addCookie(c1);
    		response.addCookie(c2);
    		//重定向到登陆页面
    		response.sendRedirect("login.jsp");
    	}
    	
     %>
    
    

    这个页面主要是处理业务,所有将jsph中的html代码都已去掉,全部以小脚本的方式写的。先判断注册时是否勾选记住用户名和密码的复选框,如果勾选则将用户名和密码放到cookie里,最后重定向到登录页面login.jsp里。

    下面是login页面的代码:

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">
        
        <title>My JSP 'login.jsp' starting page</title>
        
    	<meta http-equiv="pragma" content="no-cache">
    	<meta http-equiv="cache-control" content="no-cache">
    	<meta http-equiv="expires" content="0">    
    	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    	<meta http-equiv="description" content="This is my page">
    
      </head>
      
      <body>
      <%
      	String name="";
      	String pass="";
      	//获取cookie里面的值
      	Cookie [] cookies = request.getCookies();
         	 if(cookies!=null){
         	 	//遍历cookie
         	 	for(int i = 0;i<cookies.length;i++){
         	 		if(cookies[i].getName().equals("uname")){
         	 			//获取cookie里面的用户名
         	 			name = cookies[i].getValue();
         	 		}else if(cookies[i].getName().equals("upass")){
         	 			//获取密码
         	 			pass = cookies[i].getValue();
         	 		}
         	 	}
         	 }
       %>
         <h1>登录页面</h1>
           <form action="dologin.jsp" method="post">
         	用户名:<input type="text" name="name" value="<%=name%>"/><br/>
         	密码:<input type="text" name="pass" value="<%=pass%>"/><br/>
         	<input type="submit" value="登录"/>
         	<input type="reset" value="重置"/>
         </form>
      </body>
    </html>
    
    

    运行截图如下所示:
    在这里插入图片描述
    其中,Cookie的getName是获取存放的键,getValue获取的是值。
    欢迎留言评论,公众号:雄雄的小课堂。

  • 相关阅读:
    图片懒加载
    浅谈javascript的函数节流
    js字符串常用方法详解
    js数组详解
    thinkphp3.2 批量添加数据
    openssl证书及配置
    手机访问PC端
    mui框架(三)
    mui框架(二)
    mui框架(一)
  • 原文地址:https://www.cnblogs.com/a1111/p/12815848.html
Copyright © 2011-2022 走看看