cookie实现记住密码(jsp+servlet)初学JavaEE
2021-02-09 22:17
YPE html>
标签: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;icookies.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="">
br>
密 码:input type="password" name="password" value="">
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(""); // out.println(""); // out.println(""); // out.println("Servlet GetPostDate "); // out.println(""); // out.println(""); // out.println("Servlet GetPostDate at " + request.getContextPath() + "
"); // out.println(""); // out.println(""); // } // } @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("
"); out.println("用户名:"+username); out.println("
"); out.println("密码:"+password); out.println("
"); out.println("查看密码是否保存成功!!!"); } @Override public String getServletInfo() { return "Short description"; } }
截图:
说明:输入用户名和密码,点击记住密码然后登录;
点击超链接跳转到登录页面,查看是否保存成功
问题:解决用户名为中文时乱码
servlet代码:
Cookie nameCookie = new Cookie("username", URLEncoder.encode(username, "utf-8"));
jsp代码:
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
上一篇:并发编程之java内存模型
下一篇:R语言:日薪和应发工资
文章标题:cookie实现记住密码(jsp+servlet)初学JavaEE
文章链接:http://soscw.com/index.php/essay/53283.html