标签:type post writer data png int doctype check password
login.jsp
<%@page import="java.net.URLDecoder"%> <%@page contentType="text/html" pageEncoding="UTF-8"%> <% //用户名和密码 String userName = ""; String password = ""; //获取请求相关的cookie Cookie[] cookies = request.getCookies(); //判断cookie是否存在 if(cookies != null){ for(int i=0;i<cookies.length;i++){ if(cookies[i].getName().equals("username")){ //userName = cookies[i].getValue(); userName = URLDecoder.decode(cookies[i].getValue(), "utf-8"); } if(cookies[i].getName().equals("password")){ password = cookies[i].getValue(); } } } %> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>登录</title> </head> <body> <form action="GetPostData" method="post"> 用户名:<input type="text" name="username" value="<%=userName%>"> <br> 密 码:<input type="password" name="password" value="<%=password%>"> <br> 记住密码<input type="checkbox" name="remember"> <br> <input type="submit" name="Submit" value="提交"> <input type="reset" name="Reset" value="重置"> </form> </body> </html>
GetPostData.java
import java.io.IOException; import java.io.PrintWriter; import java.net.URLEncoder; import javax.servlet.ServletException; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class GetPostData extends HttpServlet { // protected void processRequest(HttpServletRequest request, HttpServletResponse response) // throws ServletException, IOException { // response.setContentType("text/html;charset=UTF-8"); // try (PrintWriter out = response.getWriter()) { // out.println("<!DOCTYPE html>"); // out.println("<html>"); // out.println("<head>"); // out.println("<title>Servlet GetPostDate</title>"); // out.println("</head>"); // out.println("<body>"); // out.println("<h1>Servlet GetPostDate at " + request.getContextPath() + "</h1>"); // out.println("</body>"); // out.println("</html>"); // } // } @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //processRequest(request, response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); //获取用户名和密码及是否记住密码 String username = request.getParameter("username"); String password = request.getParameter("password"); String remember = request.getParameter("remember"); //生成cookie if("on".equals(remember)){ Cookie nameCookie = new Cookie("username", URLEncoder.encode(username, "utf-8")); //Cookie nameCookie=new Cookie("username",username); Cookie passwordCookie=new Cookie("password",URLEncoder.encode(password, "utf-8")); passwordCookie.setMaxAge(30*24*60*60); nameCookie.setMaxAge(30*24*60*60); response.addCookie(nameCookie); response.addCookie(passwordCookie); } PrintWriter out = response.getWriter(); out.println("登录成功"); out.println("<br>"); out.println("用户名:"+username); out.println("<br>"); out.println("密码:"+password); out.println("<br>"); out.println("<a href=‘login.jsp‘>查看密码是否保存成功!!!</a>"); } @Override public String getServletInfo() { return "Short description"; } }
截图:
说明:输入用户名和密码,点击记住密码然后登录;
点击超链接跳转到登录页面,查看是否保存成功
问题:解决用户名为中文时乱码
servlet代码:
Cookie nameCookie = new Cookie("username", URLEncoder.encode(username, "utf-8"));
jsp代码:
<%@page import="java.net.URLDecoder"%>
userName = URLDecoder.decode(cookies[i].getValue(), "utf-8");
cookie实现记住密码(jsp+servlet)初学JavaEE
标签:type post writer data png int doctype check password
原文地址:https://www.cnblogs.com/ZhangStudy/p/12732109.html