Q:

login page using session in jsp

I've created a login environment using jsp. index.jsp, login.jsp. I take username and password from database. If user name and password matches with the database it does login process perfectly. When user give wrong name or password it shows a error message invalid name or passwordand redirect to the login page. Nothing's wrong, but I am facing a problem when I login first. The place where the error message is shown after submitting wrong name or password that place is showing null.
Why null is showing?
Below is 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  action="login.jsp" method="post">
        <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>

            </tbody>
        </table>
        </center>
    </form>
    </body>
</html>

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);
    response.sendRedirect("success.jsp");
} else {

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

 }
%>

A:

You can use this in your index.jsp
<% if ((String) request.getAttribute("errorMessage") != null) {%>
            <h3 style="color: red;"><%=(String) request.getAttribute("errorMessage")%></h3>
            <%
                }
            %>

Instead of this
<%=request.getAttribute("errorMessage")%>

