cookie记录用户名和密码

时间:2022-10-22 09:21:20

getAttribute和getParameter的区别:

request.getAttribute():是request时设置的变量的值,用request.setAttribute("name","您自己的值");来设置值,
request.getParameter():提取发送过来的参数如:本网页

登录页面 login.java

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
//判断是否是错误
String error = (String)request.getAttribute("error");
System.out.println("---error---"+ error); //拿到客户端携带的所有的cookie
String username = "";
String password = "";
//拿到所有的cookie
Cookie[] cs = request.getCookies();
for (int i = 0;cs != null && i < cs.length; i++) {
Cookie c = cs[i];
if(c.getName().equals("name")){
username = c.getValue();
System.out.println("拿到了cookie的用户名" + username);
}
if(c.getName().equals("password")){
password = c.getValue();
System.out.println("拿到了cookie的密码" + password);
} }
//创建页面
out.write("登录页面<br>");
if(error != null){
out.write("<font color=red>" + error +"</font><br>");
}
out.write("<form action='" + request.getContextPath() + "/servlet/rember' method='post' >");
out.write("姓名:<input type='text' name='username' value='"+ username +"'><br><br> ");
out.write("密码:<input type='password' name='password' value='"+ password +"'><br> ");
out.write("<input type='checkbox' name='remember' value='on'>记住用户名和密码两周<br> ");
out.write("<input type='submit' value='登录'><br> ");
out.write("</form>"); }

处理数据页面:

rember.java

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
//拿到浏览器传递的数据
String name = request.getParameter("username");
String password = request.getParameter("password");
String remember = request.getParameter("remember");
System.out.println("----username------" + name);
System.out.println("----password------" + password);
System.out.println("----remember------" + remember); //判断数据,用户名=密码的逆序
String repassword = new StringBuffer(password).reverse().toString();
if(name.equals(repassword)){
//合法用户
//判断是否需要记住
if("on".equals(remember)){
Cookie c1 = new Cookie("name",name);
Cookie c2 = new Cookie("password",password);
c1.setMaxAge(Integer.MAX_VALUE);
c2.setMaxAge(Integer.MAX_VALUE);
//设定访问路径
c1.setPath(request.getContextPath());
c2.setPath(request.getContextPath());
//向客户端发送Cookie
response.addCookie(c1);
response.addCookie(c2); } request.setAttribute("name", name);
request.getRequestDispatcher("main").forward(request, response);
}else{
//非法用户
request.setAttribute("error", "用户名或者密码错误");
request.getRequestDispatcher("login").forward(request, response);
}
}

登录后的主页面:

    public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String name = (String)request.getAttribute("name");
System.out.println("---main----name----"+name);
out.write("欢迎回来 " + name);
}

登录,输入用户名和密码,如果勾选了记住用户名和密码选项,则将用户名和密码放到cookie中,下次登录则获取cookie中的用户名和密码;

如果用户名和密码错误,则将错误信息通过request对象发送回客户端。