2

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
  • 31,391
  • 7
  • 56
  • 78
USERRR5
  • 430
  • 2
  • 10
  • 27
  • why you are using response.sendRedirect("index.jsp"); in else condition – SpringLearner Jul 09 '15 at 05:54
  • With `sendRedirect` you tell the browser to send a **new request**, i.e. the attributes you've stored into the **current**request are lost. – Jozef Chocholacek Jul 09 '15 at 05:56
  • for staying in index page after clicking submit button.and pass the values of set attribute to index page..is it wrong process ?because using same process i have passed the user id to my success.jsp page. how may i stay in my index page and show the error message..i have used RequestDispatcher dispatch=request.getRequestDispatcher("/index.jsp"); but not working – USERRR5 Jul 09 '15 at 05:58
  • this is not right way. At first you should not use functionalties in jsp page. Use jsp only for displaying purpose and use servlet for handling functionalities – SpringLearner Jul 09 '15 at 06:06

3 Answers3

0

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);
cнŝdk
  • 31,391
  • 7
  • 56
  • 78
-1

The answer is in your code itself. For success you have used session.setAttribute so its working, and for login failed" you have used request.setAttribute that's why its not working. Go for session rather than request or user requestDispatcher to forward your request. And You have mansion that you used RequestDispatcher but its not working, it will not work unless you write complete statement like calling forward method of requestDispatcher.

-1

you can do in index page in form

  <div style="color:red">${errorMessage}</div>
Deepak Singh
  • 460
  • 2
  • 7
  • 19