首先说一下实现的功能:
用户打开注册页面,最下面有个记住用户名和密码的复选框,如果勾选上,则在登录页面会自动将用户名和密码赋值到文本框中,使用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){//遍历cookiefor(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获取的是值。
欢迎留言评论,公众号:雄雄的小课堂。