在同一 jsp 页面中显示错误消息(无效的名称或密码)无效

showing error message(invalid name or password) in same jsp page is not working

我有两个jsp文件index.jsplogin.jspprofile.jsp。 这里 index.jsp 有两个文本字段名称和密码。

当我输入正确的名称和密码时登录显示成功并转到 success.jsp 但是当我输入错误的密码或名称时我希望错误消息显示在"invalid name or pass" 相同的索引页。我的代码不工作 login.jsp 用于用户名和密码验证..请帮我解决这个问题。

我已经搜索过 RequestDispatcher 但它在 servlet 中使用 class.but 我想在我的 jsp file.my 代码中使用它

index.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Example</title>
</head>
<body>
   <form method="post" action="login.jsp">
        <center>
        <table border="1" width="30%" cellpadding="3">
            <thead>
                <tr>
                    <th colspan="2" align ="left">Login Here</th><h5><%=request.getAttribute("errorMessage") %></h5>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>User Name</td>
                    <td><input type="text" name="uname" value="" /></td>
                </tr>
                <tr>
                    <td>Password</td>
                    <td><input type="password" name="pass" value="" /></td>
                </tr>
                <tr>
                    <td><input type="submit" value="Login" /></td>
                    <td><input type="reset" value="Reset" /></td>
                </tr>
                <tr>
                    <td colspan="2">Yet Not Registered!! <a href="reg.jsp">Register Here</a></td>
                </tr>
            </tbody>
        </table>
        </center>
    </form>
</body>

login.jsp

<%@ page import ="java.sql.*" %>
    <%
    String userid = request.getParameter("uname");    
    String pwd = request.getParameter("pass");
    Class.forName("org.postgresql.Driver");
    Connection con = DriverManager.getConnection("jdbc:postgresql://localhost:5432/test", "postgres", "root");
    Statement st = con.createStatement();
    ResultSet rs;
    rs = st.executeQuery("select * from member where uname='" + userid + "' and pass='" + pwd + "'");
    if (rs.next()) {
        session.setAttribute("userid", userid);
        //out.println("welcome " + userid);
        //out.println("<a href='logout.jsp'>Log out</a>");
        response.sendRedirect("success.jsp");
    } else {
        //out.println("Invalid password <a href='index.jsp'>try again</a>");
        request.setAttribute("errorMessage", "Invalid user or password");
        response.sendRedirect("index.jsp");
             }
    %>

在你的 else 语句中,不要使用 response.sendRedirect() 因为所有保存的属性都将丢失,因为它是一个新请求,请改用 requestDispatcher.forward()

req.setAttribute("errorMessage", "Invalid user or password");
req.getRequestDispatcher("/index.jsp").forward(req, res);

答案就在您的代码中。为了成功,你使用了 session.setAttribute 所以它的工作,并且为了登录失败“你使用了 request.setAttribute 这就是它不起作用的原因。去会话而不是请求或用户 requestDispatcher 来转发你的请求。你有您使用 RequestDispatcher 但它不起作用的豪宅,除非您编写完整的语句,如调用 requestDispatcher 的转发方法,否则它不会起作用。

你可以在表单的索引页中做

  <div style="color:red">${errorMessage}</div>