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

Java 在同一jsp页面中显示错误消息(无效名称或密码)无效,java,jsp,jdbc,requestdispatcher,Java,Jsp,Jdbc,Requestdispatcher,我有两个jsp文件index.jsp和login.jsp和profile.jsp。 这里index.jsp有两个文本字段名和密码 当我给出正确的名称和密码时,登录显示成功并转到success.jsp,但当我给出错误的密码或名称时,我希望在同一索引页中显示一条错误消息,即“无效名称或通过”。我的代码无法登录。jsp用于用户名和密码身份验证。请帮助我解决此问题 我已经搜索了RequestDispatcher,但它在servlet类中使用。但我想在jsp文件中使用它。我的代码 index.jsp &l

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

当我给出正确的名称和密码时,登录显示成功并转到success.jsp,但当我给出错误的密码或名称时,我希望在同一索引页中显示一条错误消息,即“无效名称或通过”。我的代码无法登录。jsp用于用户名和密码身份验证。请帮助我解决此问题

我已经搜索了RequestDispatcher,但它在servlet类中使用。但我想在jsp文件中使用它。我的代码

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>
<%@ 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");
             }
    %>

JSP示例
在这里登录
用户名
密码
还没有注册!!

login.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>
<%@ 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);

在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的前向方法,否则它将不起作用

答案就在代码本身中。为了成功,您使用了session.setAttribute使其正常工作,而登录失败“您使用了request.setAttribute,这就是它不起作用的原因。转到会话而不是请求或用户requestDispatcher来转发您的请求。您使用了RequestDispatcher,但它不起作用,除非您编写完整的语句,比如调用RequestDispatcher的前向方法,否则它将不起作用

您可以在表单的索引页中执行

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

您可以在表单的索引页中执行

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

为什么要使用response.sendRedirect(“index.jsp”);在else condition with
sendRedirect
中,告诉浏览器发送新请求,也就是说,您存储在currentrequest中的属性丢失。请在单击“提交”按钮后保留在索引页中。并将设置属性的值传递到索引页。这是错误的过程吗?因为使用相同的过程,我已将用户id传递到我的success.jsp页。如何在索引页面中显示错误消息..我使用了RequestDispatcher dispatch=request.getRequestDispatcher(“/index.jsp”);但不起作用这不是正确的方法。首先,您不应该在jsp页面中使用函数。使用jsp仅用于显示目的,使用servlet处理功能为什么使用response.sendRedirect(“index.jsp”);在else condition with
sendRedirect
中,告诉浏览器发送新请求,也就是说,您存储在currentrequest中的属性丢失。请在单击“提交”按钮后保留在索引页中。并将设置属性的值传递到索引页。这是错误的过程吗?因为使用相同的过程,我已将用户id传递到我的success.jsp页。如何在索引页面中显示错误消息..我使用了RequestDispatcher dispatch=request.getRequestDispatcher(“/index.jsp”);但不起作用这不是正确的方法。首先,您不应该在jsp页面中使用函数。使用jsp仅用于显示目的,使用servlet处理功能很好,很高兴它有帮助。很好,很高兴它有帮助。