为了账号安全,请及时绑定邮箱和手机立即绑定

为什么我保存不了账户和密码呢?

和老师的有些不同,我的dologin是servlet写的

<%@ page import="java.net.URLDecoder" %><%--
  Created by IntelliJ IDEA.
  User: lenovo
  Date: 2020/5/19
  Time: 20:18
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>欢迎登录</title>
</head>
<body>
<h1>用户登录</h1>
<hr>
<%
    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 name="loginForm" action="servlet/Login" method="post">
    <table>
        <tr>
            <td class="label">用户名:</td>
            <td><input type="text" name="username" value="<%=username %>"/></td>
        </tr>
        <tr>
            <td class="label">密码:</td>
            <td><input type="password" name="password" value="<%=password %>" /></td>
        </tr>
        <tr>
            <td colspan="2"><input type="checkbox" name="isUseCookie" checked="checked"/>十天内记住我的登录状态</td>
        </tr>
        <tr>
            <td colspan="2" align="center"><input type="submit" value="登录"/><input type="reset" value="取消"/></td>
        </tr>
    </table>
</form>
</body>
</html>

这是jsp页面代码

package servlet;

import db.Dubtil;
import model.Users;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class Login extends HttpServlet {
    Dubtil dubtil=new Dubtil();
    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
           request.setCharacterEncoding("utf-8");
           response.setContentType("text/html;charset=utf-8");
           Connection coon=dubtil.getConn();
           String username=request.getParameter("username");
           String password=request.getParameter("password");
        //首先判断用户是否选择了记住登录状态
        String[] isUseCookies = request.getParameterValues("isUseCookie");
        if(isUseCookies!=null&&isUseCookies.length>0)
        {
            //把用户名和密码保存在Cookie对象里面
            String username1 = URLEncoder.encode(request.getParameter("username"),"utf-8");
            //使用URLEncoder解决无法在Cookie当中保存中文字符串问题
            String password1 = URLEncoder.encode(request.getParameter("password"),"utf-8");

            Cookie usernameCookie = new Cookie("username",username1);
            Cookie passwordCookie = new Cookie("password",password1);
            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); //重新保存。
                    }
                }
            }
        }

下面代码和数据库有关系,我觉得和cookie应该没有关系

  try{
               PreparedStatement p=coon.prepareStatement
                       ("select *from teacherpassword where username=? and password=?");
               p.setString(1,username);
               p.setString(2,password);
               ResultSet re=p.executeQuery();
               if(re.next())
               {
                   request.getRequestDispatcher("/teacher.jsp").forward(request,response);
               }
               else
               {
                   PreparedStatement p1=coon.prepareStatement("select *from studentpassword where username=? and password=?");
                   p1.setString(1,username);
                   p1.setString(2,password);
                   ResultSet re1=p1.executeQuery();
                   if(re1.next())
                   {
                       PreparedStatement p2=coon.prepareStatement("select *from student where number=?");
                       p2.setString(1,username);
                       ResultSet re2=p2.executeQuery();
                       re2.next();
                       Users user=new Users();
                       user.setName(re2.getString("name"));
                       user.setSex(re2.getString("sex"));
                       user.setNumber(re2.getString("number"));
                       user.setEmail(re2.getString("email"));
                       user.setAddress(re2.getString("address"));
                       user.setPhonenumber(re2.getString("phonenumber"));
                       user.setMathscore(re2.getDouble("mathscore"));
                       user.setChinesescore( re2.getDouble("chinesescore"));
                       user.setEnglishscore(re2.getDouble("englishscore"));
                       request.getSession().setAttribute("studentinformation",user);
                       request.getRequestDispatcher("/student.jsp").forward(request,response);
                   }
                   else
                   {
                       request.getRequestDispatcher("/faillogin.jsp").forward(request,response);
                   }
               }
           }catch (Exception e)
           {
               e.printStackTrace();
           }

    }
}

求大佬指点为什么cookie保存不了密码和账户呢?

正在回答

1 回答

没看出来问题。。。你搞定了没?啥问题?


0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

为什么我保存不了账户和密码呢?

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信