Я создаю приложение для викторины. Здесь есть 5 страниц jsp с 5 различными викторинами. Если я использую 1 страницу jsp и страницу результатов, она работает нормально..i.e; Он перенаправляется из QuizPage5.jsp и показывает результат в ResultPage.jsp. Мне нужно показать все результаты от QuizPage1.jsp, QuizPage2.jsp, QuizPage3.jsp, QuizPage4.jsp и QuizPage5.jsp до ResultPage.jsp.Как получить значения с предыдущей страницы jsp на страницу результатов?
Я использовал
в jsp1 передать значение.
<form action="">
<input type="hidden" name="hidden" value="hidden">
<input type="submit" value="submit"></form>
в jsp2, чтобы получить значение
String value=request.getParameter("hidden");
Но я получаю java.lang.NullPointerException
Вот мой код ..
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
<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
<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
<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
<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
<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>
Ваша помощь очень ценится. Спасибо заранее
У вас есть 5 отдельных страниц с одинаковым кодом, где отличается только текстовое содержимое? У вас должна быть одна страница с массивом вопросов ... – mplungjan
Я использовал таким образом. Есть ли какое-нибудь решение? –
@ Dinesh Kumar Решение заключается в реализации его как 'mplungjan', упомянутого выше. – Alex