一直找不到错误,我是eclipse写的
<%@page import="java.util.*,java.net.*"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <h1>用户登录</h1> <% request.setCharacterEncoding("utf-8"); String username=""; String password = ""; Cookie[] cookies = request.getCookies(); if(cookies!=null&&cookies.length>0) { for(Cookie c:cookies) { if(c.getName().equals("username")) { username =URLDecoder.decode(c.getValue(), "utf-8") ; } if(c.getName().equals("password")) { password =URLDecoder.decode(c.getValue(), "utf-8"); } } } %> <form action="dologin.jsp" name="login" method="post"> <table> <tr> <td>姓名:</td> <td><input type="text" name="username " value="<%=username %>" /></td> </tr> <tr> <td>密码:</td> <td> <input type="password" name="password" value="<%=password %>" /> </td> </tr> <tr> <td colspan="2" ><input type="checkbox" name="useCookie" checked >十天内免登陆</td> </tr> <tr> <td colspan="2" ><input type="submit" value="提交" ></td> </tr> </table> </form> <br> <br> </body> </html>
这是log.jsp
<%@page import="java.util.*,java.net.*"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <h1>登录成功</h1> <hr> <% request.setCharacterEncoding("utf-8"); //判断用户是否选择记住密码 String[] useCookie=request.getParameterValues("useCookie"); if(useCookie!=null&&useCookie.length>0){ //用户名和密码保存在cookie里 String username = URLEncoder.encode(request.getParameter("username"),"utf-8"); //使用URLEncoder解决无法在Cookie当中保存中文字符串问题 String password=URLEncoder.encode(request.getParameter("password"), "utf-8"); Cookie usernameCookie=new Cookie("username",username); Cookie passwordCookie=new Cookie("password",password); usernameCookie.setMaxAge(864000); passwordCookie.setMaxAge(864000);//最大生存期限10天 response.addCookie(usernameCookie); response.addCookie(passwordCookie); }else{ Cookie[] cookies=request.getCookies(); if(cookies!=null&&cookies.length>0){ for(Cookie c:cookies){ if(c.getName().equals("username")||c.getName().equals("password")){ c.setMaxAge(0);//设置cookie失效 response.addCookie(c);//重新保存 } } } } %> <a href="users.jsp" target="_blank">查看用户信息</a> </body> </html>
这是dologin.jsp
<%@page import="java.util.*,java.net.*"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <h1>用户信息</h1> <br> <% request.setCharacterEncoding("utf-8"); String username=""; String password=""; Cookie[] cookies=request.getCookies(); if(cookies!=null&&cookies.length>0){ for(Cookie c:cookies){ if(c.getName().equals("username")){ username=URLDecoder.decode(c.getValue(), "utf-8") ; } if(c.getName().equals("password")){ password=URLDecoder.decode(c.getValue(), "utf-8"); } } } %> 用户名:<%=username %><br> 密码:<%=password %><br> </body> </html>
这是users.jsp