Ali Bassam
Ali Bassam

Reputation: 9969

Cannot use parameter brought from another JSP page

This is my first attempt in exchanging values between 2 JSP pages, the index.jsp page has a log in form, and the login.jsp page will validate this login, if login is unsuccessful, it will redirect to the index.jsp along with a parameter called valid, with a value of 0, if its successful, the value will be 1.

index.jsp

<%@page import="javax.enterprise.inject.spi.Bean"%>
<%@page import="myDatabase.Login"%> //this is class that I created
<%@page import="myDatabase.JavaDB"%> //this is a class that I created
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" type="text/css" href="indexCSS.css" />
        <title>Chat</title>

    </head>
    <body>
        <jsp:include page="login.jsp">
        <jsp:param name="valid" value="1"/>
        </jsp:include>  
        <% String valid = request.getParameter("valid");%>
        <div class="mainPage">
            <div id="header">
                <div id="pageTitle">
                    Chat
                </div>
            </div>
            <div id="loginBox">
                <form name="login" action="login.jsp" method="POST">

                    <div id="loginItems">
                        <div id="loginTitle">
                            Log in
                        </div>
                        <hr style="color:green;">
                        <div style="margin-top:17px; overflow:hidden;">
                            <label for="id">
                                ID
                            </label>
                            <span id="idError" class="error">
                                    <% if(valid.equals("0")) { %>is not valid<% } %>

                            </span>
                            <br>
                            <input class="inputText" type="text" name="id" value="" maxlength="10" autocomplete="off"/>
                        </div>
                        <div style="margin-top:17px; overflow:hidden;">
                            <label for="password">
                                Password
                            </label>
                            <span id="passwordError" class="error">

                            </span>
                            <br>
                            <input class="inputText" type="password" name="password" value="" maxlength="32" autocomplete="off"/>
                        </div>
                        <div style="margin-top:17px; overflow:hidden;">
                            <div>
                                Forgot your Password?
                            </div>
                        </div>
                        <div style="margin-top:17px; position:relative; overflow:hidden">
                            <input class="inputButton" type="submit" value="Log in" name="loginButton" />
                        </div>
                    </div>
                </form>
            </div>
        </div> 
    </body>
</html>

this is the login.jsp

<head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <jsp:useBean id="loginBean" scope="session" class="myDatabase.Login" />
        <jsp:setProperty name="loginBean" property="id" />
        <jsp:setProperty name="loginBean" property="password" />
        <%
            JavaDB myJavaDB=new JavaDB();
            myJavaDB.Connect("IULChat","iul","iul");
           if(myJavaDB.isConnected()==true){
                    //response.sendRedirect("index.jsp?a=2");
                    Login myLogin = new Login(loginBean.getId(),loginBean.getPassword());
                    myLogin.setConn(myJavaDB.getMyConnection());
                    myLogin.login(); loginBean.setId(0); loginBean.setPassword("");
                    if(myLogin.isValid()==true)
                                               {
                        response.sendRedirect("index.jsp?valid=1");
                    }
                    else
                                               {
                        response.sendRedirect("index.jsp?valid=0");
                    }
                                       }
            else
                out.println("no");
        %>

    </body>
</html>

When I run the project I get this error.

type Exception report

message

descriptionThe server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: java.lang.NullPointerException

root cause

java.lang.NullPointerException

note The full stack traces of the exception and its root causes are available in the GlassFish Server Open Source Edition 3.1.2 logs.

This error goes away if I delete the java codes in this section

                    <span id="idError" class="error">
                            <% if(valid.equals("0")) { %>is not valid<% } %>
                    </span>

Upvotes: 0

Views: 706

Answers (2)

AshwinN
AshwinN

Reputation: 301

The page is probably throwing NullPointerException because value of 'valid' is null. Always make string comparisons with literals using format

<literal>.equals(<variable>)

Try using the following updated lines of code:

<span id="idError" class="error">
    <% if("0".equals(valid)) { %>is not valid<% } %>
</span>

Upvotes: 1

kosa
kosa

Reputation: 66677

Redirect will be treated as brand new request, so you will use previous request parameters. If you want to keep the request parameters available for next page, may be you need to consider using forward.

Upvotes: 1

Related Questions