2012-07-31 43 views
0

這是我第一次嘗試交換兩個JSP頁面之間的值,index.jsp頁面具有登錄形式,並且login.jsp頁面將驗證此登錄,如果登錄失敗,它會重定向到index.jsp的一個叫有效參數一起,爲0的值,如果成功,值爲1不能使用從另一個JSP頁面帶來的參數

的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> 

這是登錄.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. 

這個錯誤消失,如果我在本節

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

回答

1

頁面可能拋出NullPointerException,因爲'valid'的值爲null。 總是要與文字字符串比較使用格式

<literal>.equals(<variable>) 

嘗試使用下面的代碼更新行:

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

非常感謝你的工作! – 2012-07-31 18:42:09

1

Redirect將被視爲全新的請求刪除了Java代碼,所以您將使用以前的請求參數。如果您想保持請求參數可用於下一頁,可能需要考慮使用forward

+0

可以請你告訴使用什麼代碼,而不是response.sendRedirect是(「index.jsp的?有效= 1" ); ? – 2012-07-31 18:40:36

+0

您可能需要使用請求調度程序。閱讀此鏈接http://java.boot.by/wcd-guide/ch03s05.html – kosa 2012-07-31 18:41:54

相關問題