言落578 2022-03-17 09:49 采纳率: 83.3%
浏览 85
已结题

idea登录界面输入账户会突然清空

用idea设计的一个学生管理系统,为什么在登录界面时候能注册成功,但是用这个账户登录就是突然账户密码清空,反正就是进不去里面

img

img


以下是loginservlet代码

package com.lili.servlet;

import com.alibaba.fastjson.JSON;
import com.lili.dao.impl.UserDaoImpl;
import com.lili.entity.User;
import com.lili.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json");
        PrintWriter out = response.getWriter();
        String user = request.getParameter("username");
        String passwd = request.getParameter("password");
        List<User> userList = new ArrayList<>();
        List<User> List = new UserServiceImpl(new UserDaoImpl()).findUser(user, passwd);
        if (userList.size() > 0) {
            // 添加进session
            HttpSession session = request.getSession(true);
            session.setAttribute("user", userList.get(0));
            // 设置过期时间5分钟
            session.setMaxInactiveInterval(60 * 5);
        }
        String str = JSON.toJSONString(userList);
        out.write(str);
        out.flush();
        out.close();
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}

  • 写回答

4条回答 默认 最新

  • CSDN专家-sinJack 2022-03-17 10:05
    关注

    代码写错了。
    List修改为userList

    package com.lili.servlet;
     
    import com.alibaba.fastjson.JSON;
    import com.lili.dao.impl.UserDaoImpl;
    import com.lili.entity.User;
    import com.lili.service.impl.UserServiceImpl;
     
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.ArrayList;
    import java.util.List;
     
    @WebServlet("/login")
    public class LoginServlet extends HttpServlet {
        @Override
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            request.setCharacterEncoding("utf-8");
            response.setCharacterEncoding("utf-8");
            response.setContentType("application/json");
            PrintWriter out = response.getWriter();
            String user = request.getParameter("username");
            String passwd = request.getParameter("password");
            List<User> userList = new UserServiceImpl(new UserDaoImpl()).findUser(user, passwd);
            if (userList.size() > 0) {
                // 添加进session
                HttpSession session = request.getSession(true);
                session.setAttribute("user", userList.get(0));
                // 设置过期时间5分钟
                session.setMaxInactiveInterval(60 * 5);
            }
            String str = JSON.toJSONString(userList);
            out.write(str);
            out.flush();
            out.close();
        }
     
        @Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            this.doPost(request, response);
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 3月25日
  • 已采纳回答 3月17日
  • 创建了问题 3月17日

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀