2012-07-31 3 views
0

이것은 JSP 페이지 2 개 사이에서 값을 교환하는 첫 번째 시도이며 index.jsp 페이지에는 로그인 양식이 있으며 로그인에 실패하면 login.jsp 페이지는이 로그인의 유효성을 검사합니다 ,는 0의 값으로, 유효라는 매개 변수와 함께 index.jsp를로 리디렉션됩니다, 성공적인 경우, 값이 로그인입니다 index.jsp를 1다른 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> 

프로젝트를 실행할 때이 오류가 발생합니다. 내가

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

답변

1

'valid'값이 null이므로 페이지에 NullPointerException이 발생합니다.

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

정말 고마워요! –

1

Redirect가 새로운 요청으로 처리됩니다이 섹션의 자바 코드를 삭제, 그래서 당신은 이전 요청 매개 변수를 사용할 경우

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. 

이 오류는 사라집니다. 다음 페이지에서 요청 매개 변수를 계속 사용하려면 forward을 고려해야 할 수도 있습니다.

+0

가 대신 위해 response.sendRedirect ("index.jsp에서의 사용에 어떤 코드를 알려 주시기 바랍니다 수 있습니다 항상 리터럴 문자열 비교는 다음 코드 업데이트 라인을 사용해보십시오 형식

<literal>.equals(<variable>) 

를 사용하여 만들어? 유효 = 1 "); ? –

+0

요청 발송자를 사용해야 할 수도 있습니다. 이 링크 읽기 http://java.boot.by/wcd-guide/ch03s05.html – kosa

관련 문제