简体   繁体   English

如何从以前的jsp页面到结果页面检索值?

[英]How to retrieve the values from previous jsp page to result page?

I am creating a quiz application. 我正在创建测验应用程序。 Here, there are 5 jsp pages with 5 different quizes. 在这里,有5个具有5个不同测验的jsp页面。 If I use 1 jsp page and result page, it is working fine..ie; 如果我使用1个jsp页面和结果页面,则可以正常工作。 It is redirecting from QuizPage5.jsp and show the result in ResultPage.jsp. 它从QuizPage5.jsp重定向并在ResultPage.jsp中显示结果。 I need to show all the results from QuizPage1.jsp, QuizPage2.jsp, QuizPage3.jsp, QuizPage4.jsp and QuizPage5.jsp to ResultPage.jsp. 我需要显示从QuizPage1.jsp,QuizPage2.jsp,QuizPage3.jsp,QuizPage4.jsp和QuizPage5.jsp到ResultPage.jsp的所有结果。

I used 我用了

in jsp1 to pass value. 在jsp1中传递值。

<form action="">
<input type="hidden" name="hidden" value="hidden">
<input type="submit" value="submit"></form>

in jsp2 to get value 在jsp2中获取价值

String value=request.getParameter("hidden");

But, I am getting java.lang.NullPointerException 但是,我正在获取java.lang.NullPointerException

Here is my code.. 这是我的代码。

QuizPage1.jsp QuizPage1.jsp

<html>  
    <head>  
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
            <script language="JavaScript">  

            function check()  
               {  
                   var radio_choice = false;  
                   for (counter = 0; counter < myform.grp.length; counter++)  
                    {  
                        if (myform.grp[counter].checked)  
                        radio_choice = true;  
                    }  
                        if (!radio_choice)  
                         {  
                            alert("Please select one.")  
                            return (false);  
                         }  
                         return(true);  
                     }  
</script>  
    </head>  
   <body> 
        <h3>Quiz No.1</h3>

        Q1.Which one of the following is the Capital of India?<br><br>  
        <form action="QuizPage2.jsp" method="post" name="myform" onsubmit="return check();">  
            <input type="radio" name="grp" value="New Delhi" >New Delhi<br>  
             <input type="radio" name="grp" value="Kolkata" >Kolkata<br>  
                 <input type="radio" name="grp" value="Chennai" >Chennai<br>  
                     <input type="radio" name="grp" value="Mumbai" >Mumbai<br>  
                     <br><br><input type="submit" value="Next Question"> 
                     <input type="hidden" name="hidden" value="grp">
                     <input type="submit" value ="Back" onclick="window.history.back();"/> 

        </form>  
      </body>



</html> 

QuizPage2.jsp QuizPage2.jsp

<html>  
    <head>  
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
            <script language="JavaScript">  

            function check()  
               {  
                   var radio_choice = false;  
                   for (counter = 0; counter < myform2.grp2.length; counter++)  
                    {  
                        if (myform2.grp2[counter].checked)  
                        radio_choice = true;  
                    }  
                        if (!radio_choice)  
                         {  
                            alert("Please select any one Answer");  
                            return (false);  
                         }  
                         return(true);  
                     }  
</script>  
    </head>  
    <body>  
        <h3>Quiz No.2</h3>

        Q2. Where is Chennai Located?<br><br>  
        <form action="QuizPage3.jsp"method="post" name="myform2" onsubmit="return check();">  
            <input type="radio" name="grp2" value="Karnataka" >Karnataka<br>  
             <input type="radio" name="grp2" value="Tamilnadu" >Tamilnadu<br>  
                 <input type="radio" name="grp2" value="Andhra Pradesh" >Andhra Pradesh<br>  
                     <input type="radio" name="grp2" value="Maharashtra" >Maharashtra<br>  
                     <br><br><input type="submit" value="Next Question">  
                      <input type="hidden" name="hidden" value="grp2">
                     <input type="submit" value ="Back" onclick="window.history.back();"/> 


        </form>  
        <%
            String s1=request.getParameter("grp");
         %>
      </body>  
</html> 

QuizPage3.jsp QuizPage3.jsp

<html>  
    <head>  
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
            <script language="JavaScript">  

            function check()  
               {  
                   var radio_choice = false;  
                   for (counter = 0; counter < myform3.grp3.length; counter++)  
                    {  
                        if (myform3.grp3[counter].checked)  
                        radio_choice = true;  
                    }  
                        if (!radio_choice)  
                         {  
                            alert("Please select any one Answer");  
                            return (false);  
                         }  
                         return(true);  
                     }  
</script>  
    </head>  
    <body>  
        <h3>Quiz No.3</h3>
        Q3. What is the largest state in the USA<br><br>  
        <form action="QuizPage4.jsp"method="post" name="myform" onsubmit="return check();">  
            <input type="radio" name="grp3" value="New York" >New York<br>  
             <input type="radio" name="grp3" value="Alaska" >Alaska<br>  
                 <input type="radio" name="grp3" value="California" >California<br>  
                     <input type="radio" name="grp3" value="Melbourne" >Melbourne<br>  
                     <br><br><input type="submit" value="Next Question">  
                     <input type="hidden" name="hidden" value="grp3">
                     <input type="submit" value ="Back" onclick="window.history.back();"/> 

        </form>  
        <%
            String s2=request.getParameter("grp2");
         %>
      </body>  
</html> 

QuizPage4.jsp QuizPage4.jsp

<html>  
    <head>  
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
            <script language="JavaScript">  

            function check()  
               {  
                   var radio_choice = false;  
                   for (counter = 0; counter < myform4.grp4.length; counter++)  
                    {  
                        if (myform4.grp4[counter].checked)  
                        radio_choice = true;  
                    }  
                        if (!radio_choice)  
                         {  
                            alert("Please select any one Answer");  
                            return (false);  
                         }  
                         return(true);  
                     }  
</script>  
    </head>  
    <body>  
        <h3>Quiz No.4</h3>
        Q4. The famous Dilwara Temples are situated in<br><br>  
        <form action="QuizPage5.jsp"method="post" name="myform" onsubmit="return check();">  
            <input type="radio" name="grp4" value="Uttar Pradesh" >Uttar Pradesh<br>  
             <input type="radio" name="grp4" value="Maharashtra" >Maharashtra<br>  
                 <input type="radio" name="grp4" value="Andhra Pradesh" >Andhra Pradesh<br>  
                     <input type="radio" name="grp4" value="Rajasthan" >Rajasthan<br>  
                     <br><br><input type="submit" value="Next Question"> 
                     <input type="hidden" name="hidden" value="grp4">
                     <input type="submit" value ="Back" onclick="window.history.back();"/> 

        </form> 
        <%
            String s3=request.getParameter("grp3");
         %>
      </body>  
</html> 

QuizPage5.jsp QuizPage5.jsp

<html>  
    <head>  
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
            <script language="JavaScript">  

            function check()  
               {  
                   var radio_choice = false;  
                   for (counter = 0; counter < myform5.grp5.length; counter++)  
                    {  
                        if (myform5.grp5[counter].checked)  
                        radio_choice = true;  
                    }  
                        if (!radio_choice)  
                         {  
                            alert("Please select any one Answer");  
                            return (false);  
                         }  
                         return(true);  
                     }  
</script>  
    </head>  
    <body>  
        <h3>Quiz No.5</h3>
        Q5. The Indian Institute of Science is located at<br><br>  
        <form action="ResultPage.jsp"method="post" name="myform" onsubmit="return check();">  
            <input type="radio" name="grp5" value="Chennai" >Chennai<br>  
             <input type="radio" name="grp5" value="Bangalore" >Bangalore<br>  
                 <input type="radio" name="grp5" value="Mumbai" >Mumbai<br>  
                     <input type="radio" name="grp5" value="Hyderabad" >Hyderabad<br>  
                     <br><br><input type="submit" value="Show Result"> 
                     <input type="hidden" name="hidden" value="grp5"/>


        </form> 
        <%
            String s4=request.getParameter("grp4");
         %>
      </body>  
</html> 

ResultPage.jsp ResultPage.jsp

<html>  
    <head>  
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
        <title>Quiz Result</title>  
    </head>  
    <body>  
          <%  
     String answer="New Delhi";  
     String s1=request.getParameter("grp");  
     if(s1.equals(answer))  
         {  
        out.println("Correct Answer");  
         }  
     else  
         {out.println("Wrong Answer! The Correct Answer is :"+answer);  
     }  
    %> 
    <%  
     String answer2="Tamilnadu";  
     String s2=request.getParameter("grp2");  
     if(s2.equals(answer2))  
         {  
        out.println("Correct Answer");  
         }  
     else  
         {out.println("Wrong Answer! The Correct Answer is :"+answer2);  
     }  
    %> 
    <%  
     String answer3="Alaska";  
     String s3=request.getParameter("grp3");  
     if(s3.equals(answer3))  
         {  
        out.println("Correct Answer");  
         }  
     else  
         {out.println("Wrong Answer! The Correct Answer is :"+answer3);  
     }  
    %> 
    <%  
     String answer4="Rajasthan";  
     String s4=request.getParameter("grp4");  
     if(s4.equals(answer4))  
         {  
        out.println("Correct Answer");  
         }  
     else  
         {out.println("Wrong Answer! The Correct Answer is :"+answer4);  
     }  
    %> 
        <%  
         String answer5="Bangalore";  
         String s5=request.getParameter("grp5");  
         if(s5.equals(answer5))  
             {  
            out.println("Correct Answer");  
             }  
         else  
             {out.println("Wrong Answer! The Correct Answer is :"+answer5);  
         }  
        %> 


    </body> 
</html> 

Your help is highly appreciated. 非常感谢您的帮助。 Thanks in Advance 提前致谢

If you want data that can be access across multiple pages, consider using the session to store data. 如果您希望可以跨多个页面访问的数据,请考虑使用会话存储数据。 For example, if page1.jsp submits a form parameter, "page1Result" to page2.jsp, then in page2.jsp, do the following: 例如,如果page1.jsp向page2.jsp提交了表单参数“ page1Result”,则在page2.jsp中,执行以下操作:

String page1Result = request.getParameter("page1Result");
request.getSession().setAttribute("page1Result", page1Result);

Then at some point down the road (say on page 5), you can access the page1 value as follows: 然后,在以后的某个时刻(例如第5页),您可以按以下方式访问page1值:

String page1Value = (String)request.getSession().getAttribute("page1Result);

================================================================================ So applying this to your code: ================================================== =============================将其应用于您的代码:

In page2 jsp, change the scriptlet at the end of the file to: 在page2 jsp中,将文件末尾的scriptlet更改为:

    <%
     String s1=request.getParameter("grp");
     request.getSession().setAttribute("s1", s1);
     %>

And in page 3 JSP, change the script let to: 在第3页JSP中,将脚本更改为:

    <%
     String s2=request.getParameter("grp2");
     request.getSession().setAttribute("s2", s2);
     %>

And so on. 等等。

And finally on ResultPage.jsp (just showing example one line for answer 2) 最后在Re​​sultPage.jsp上(仅显示示例2)

out.println("Wrong Answer! The Correct Answer for question 2 is :"+session.getAttribute("s2"));

on jsp1 在jsp1上

String passThis = "pass this value";
request.setAttribute("param1",passThis);

dispatch to jsp 2 调度到jsp 2

in jsp2 在jsp2中

String getParam = request.getAttribute("param1");

通过JavaScript切换问题并立即提交所有内容。

I have not detailed look on the code. 我没有详细看代码。 But the question requires a multiple request paradigm. 但是这个问题需要多重请求范式。 But result.jsp though takes all the answer from request object. 但是result.jsp虽然从请求对象获取所有答案。 Whereas it should be the session object. 而它应该是会话对象。 And answer to each question should be stored in session. 每个问题的答案都应该存储在会话中。 So it is accessible and doesn't gets descoped. 因此它是可访问的,并且不会受到影响。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM