不能使用从另一个JSP页面带来的参数

时间:2012-07-31 18:14:47

标签: java jsp url-parameters

这是我第一次尝试在两个JSP页面之间交换值,index.jsp页面有登录形式,login.jsp页面将验证此登录,如果登录失败,它将重定向到索引。 jsp以及一个名为valid的参数,值为0,如果成功,则该值为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>

这是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>

当我运行项目时,我收到此错误。

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.

如果我删除本节中的java代码

,则此错误消失
                    <span id="idError" class="error">
                            <% if(valid.equals("0")) { %>is not valid<% } %>
                    </span>

2 个答案:

答案 0 :(得分:1)

Redirect将被视为全新请求,因此您将使用以前的请求参数。如果您希望将请求参数保留为下一页,则可能需要考虑使用forward

答案 1 :(得分:1)

页面可能抛出NullPointerException,因为'valid'的值为null。 始终使用格式

进行字符串比较
<literal>.equals(<variable>)

尝试使用以下更新的代码行:

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