密码错误,未提示错误信息

密码错误,未提示错误信息

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录</title>
<link rel="stylesheet" href="${pageContext.request.contextPath}/css/login.css">
</head>
<body>

<div class="login">
<div class="header">
<h1>
<a href="/login.do">登录</a>
</h1>
<button></button>
</div>

<form action="/login.do" method="post">
<div class="name">
<input type="text" id="name" name="username">
<p></p>
</div>
<div class="pwd">
<input type="password" id="pwd" name="password">
<p></p>
</div>
<div class="btn-red">
<input type="submit" value="登录" id="login-btn">
</div>
<div class="error">
<c:if test="${USER.username}==null">
<span class="error"> ${MSG}</span>
</c:if>
</div>
</form>
</div>
</body>
</html>
//    /toLogin.do           /login.jsp
public void toLogin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.getRequestDispatcher("login.jsp").forward(request,response);
}
//      /login.do
public void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username =request.getParameter("username");
String password = request.getParameter("password");
User user = userService.get(username,password);
if(user==null){
response.sendRedirect("toLogin.do");
request.setAttribute("MSG","密码或用户名错误,请重新登录");
}else{
HttpSession session = request.getSession();
session.setAttribute("USER",user);
response.sendRedirect("../category/list.do");
}
}


正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

3回答
好帮手慕小脸 2021-04-28 18:36:47

同学你好,这里同学访问的是login.do吧?如果是,出现这种情况是正常的,这里应先访问jsp,然后输入用户名和密码,点击登录跳转至表单对应的路径上(login.do),举例如下:

http://img1.sycdn.imooc.com//climg/60892fab09549b6f08280707.jpg

http://img1.sycdn.imooc.com//climg/60892fc409b3f36e10220414.jpg

祝学习愉快~

好帮手慕小脸 2021-04-27 18:59:49

同学你好,如下代码位置处,将其修改为请求转发再试试:

http://img1.sycdn.imooc.com//climg/6087ee2409920b0404900127.jpg

祝学习愉快~

  • 提问者 复java_n #1

    还是没有输出输入错误的提示呀

    2021-04-28 14:06:15
  • 好帮手慕小脸 回复 提问者 复java_n #2

    同学你好,先进行setAttribute然后再进行请求转发:

    if(user==null){
    request.setAttribute("MSG","密码或用户名错误,请重新登录");
    //请求转发
    }

    祝学习愉快~​

    2021-04-28 15:17:56
  • 提问者 复java_n 回复 好帮手慕小脸 #3

    按照老师的说法修改了,还没有输入账号和密码的时候就显示了

    http://img1.sycdn.imooc.com//climg/60891eeb095fefaa10260907.jpg

    //    /toLogin.do           /login.jsp
    public void toLogin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    request.getRequestDispatcher("login.jsp").forward(request, response);
    }

    //      /login.do
    public void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String username = request.getParameter("username");
    String password = request.getParameter("password");
    User user = userService.get(username, password);
    if (user == null) {
    request.setAttribute("MSG", "密码或用户名错误,请重新登录");
    request.getRequestDispatcher("toLogin.do").forward(request, response);

    } else {
    HttpSession session = request.getSession();
    session.setAttribute("USER", user);
    response.sendRedirect("../category/list.do");
    }
    }
    <form action="/login.do" method="post">
    <div class="name">
    <input type="text" id="name" name="username">
    <p></p>
    </div>
    <div class="pwd">
    <input type="password" id="pwd" name="password">
    <p></p>
    </div>
    <div class="btn-red">
    <input type="submit" value="登录" id="login-btn">
    </div>
    ${MSG}
    </form>


    2021-04-28 16:41:43
好帮手慕小脸 2021-04-27 17:50:52

同学你好,如下这里无需加判断,直接书写el表达式即可

http://img1.sycdn.imooc.com//climg/6087de74095fe72604310129.jpg

祝学习愉快~


  • 提问者 复java_n #1

    按照老师的方法更改了,依旧没有跳出输入错误的信息

    2021-04-27 18:54:49
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师