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

10,497

In your else statement, don't use response.sendRedirect() because all the saved attributes will be lost as it's a new request, use requestDispatcher.forward() instead:

req.setAttribute("errorMessage", "Invalid user or password");
req.getRequestDispatcher("/index.jsp").forward(req, res);
Share:
10,497
USERRR5
Author by

USERRR5

Updated on June 04, 2022

Comments

  • USERRR5
    USERRR5 about 2 years

    i have two jsp file index.jsp and login.jsp and profile.jsp. here index.jsp has two text field name and password.

    When I give right name and password login shows successful and go to success.jsp but when i give wrong password or name i want that an error message will show in same index page that is "invalid name or pass". my code is not working login.jsp is used for username and password authentication..please help me to solve this .

    I have searched about RequestDispatcher but it is used in servlet class.but i want to do it in my jsp file.my code

    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");
                 }
        %>
    
  • cнŝdk
    cнŝdk almost 9 years
    Great, glad it helps.