Question about JSP and JavaBeans
Hey All,
I was thinking of doing some basic jsp and css page projects. I thought to create a questionaire page where users will have an option to choose the correct answer from a radio button. Lets say there are total of 10 questions with 4 choices given for each question and one should be correct.
After the user select the answers for each question a new page will be opened displaying the question, if the choice is correct then the answer is highlights in green if the answer is wrong then red.
Whats the best way to do it? i thought to create a javaBean class with some getter and setter method or do everything in jsp with some variable names.
Secondly how can i make the right answer in green highlight and wrong answer in red?
Any clues will be appreciated so i can get started
[824 byte] By [
lrngjavaa] at [2007-11-27 10:27:47]

[nobr]why in the world my jsp page cannot resolve request.getParamter("");?
these are the errors
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: Unable to compile class for JSP
An error occurred at line: 32 in the jsp file: /QuestionForm.jsp
Generated servlet error:
[javac] Compiling 1 source file
C:\JBuilder2007\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\work\Catalina\localhost\JSPEXAMPLE\org\apache\jsp\QuestionForm_jsp.java:75: cannot find symbol
symbol : variable score
location: class org.apache.jsp.QuestionForm_jsp
score = 0;
^
An error occurred at line: 32 in the jsp file: /QuestionForm.jsp
Generated servlet error:
C:\JBuilder2007\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\work\Catalina\localhost\JSPEXAMPLE\org\apache\jsp\QuestionForm_jsp.java:76: cannot find symbol
symbol : variable correctAnswers
location: class org.apache.jsp.QuestionForm_jsp
correctAnswers = new int[7];
^
An error occurred at line: 32 in the jsp file: /QuestionForm.jsp
Generated servlet error:
C:\JBuilder2007\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\work\Catalina\localhost\JSPEXAMPLE\org\apache\jsp\QuestionForm_jsp.java:77: cannot find symbol
symbol : variable usersAnswers
location: class org.apache.jsp.QuestionForm_jsp
usersAnswers = new int[7];
^
An error occurred at line: 32 in the jsp file: /QuestionForm.jsp
Generated servlet error:
C:\JBuilder2007\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\work\Catalina\localhost\JSPEXAMPLE\org\apache\jsp\QuestionForm_jsp.java:78: cannot find symbol
symbol : variable correctAnswers
location: class org.apache.jsp.QuestionForm_jsp
correctAnswers[0] = 1;
^
An error occurred at line: 32 in the jsp file: /QuestionForm.jsp
Generated servlet error:
C:\JBuilder2007\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\work\Catalina\localhost\JSPEXAMPLE\org\apache\jsp\QuestionForm_jsp.java:79: cannot find symbol
symbol : variable correctAnswers
location: class org.apache.jsp.QuestionForm_jsp
correctAnswers[1] = 1;
^
.....
and here is my simple incompleted code.
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import = "java.util.Calendar" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<b>Displaying The time</b>
<%
Calendar now =Calendar.getInstance();
int hour = now.get(Calendar.HOUR_OF_DAY);
int minute = now.get(Calendar.MINUTE);
if(hour<10)
out.println("0" + hour);
else
out.println(hour);
out.println(":");
if(minute<0)
out.println("0" + minute);
else
out.println(minute);
%>
<h2>Here are your answers</h2>
<br>
<><%
score = 0;
correctAnswers = new int[7];
usersAnswers = new int[7];
correctAnswers[0] = 1;
correctAnswers[1] = 1;
correctAnswers[2] = 1;
correctAnswers[3] = 1;
correctAnswers[4] = 3;
correctAnswers[5] = 2;
correctAnswers[6] = 1;
usersAnswers[0] = Integer.parseInt(request.getParamter("q1"));
userAnswers[1] = Integer.parseInt(request.getParamter("q2"));
userAnswers[2] = Integer.parseInt(request.getParamter("q3"));
userAnswers[3] = Integer.parseInt(request.getParamter("q4"));
userAnswers[4] = Integer.parseInt(request.getParamter("q5"));
userAnswers[5] = Integer.parseInt(request.getParamter("q6"));
userAnswers[6] = Integer.parseInt(request.getParamter("q7"));
for(int i=0; i<correctAnswers.length; i++) {
if(correctAnswers[i]==userAnswers[i]) {
out.println(correctAnswers[i] + " is the correct answer ");
score++;
}
else {
out.println(userAnswers[i] + "is not the correct answer");
}
}
%>
</body>
</html>
+ i was trying to highlight the answer with red if the user answer wrong and green if the user answer it right. but for some reason jsp does not display the color if i use this command
out.println("<span style="background-color: #FF0000>");>[/nobr]
You have to declare those variables in the JSP. Your error message tells you that.
ignore
Message was edited by:
fastmike
<% int[] correctAnswers = new int[7]
%>
[nobr]i did that already
<h2>Here are your answers</h2>
<br>
<><%
score = 0;
correctAnswers = new int[7];
usersAnswers = new int[7];
correctAnswers[0] = 1;
correctAnswers[1] = 1;
correctAnswers[2] = 1;
correctAnswers[3] = 1;
correctAnswers[4] = 3;
correctAnswers[5] = 2;
correctAnswers[6] = 1;
usersAnswers[0] = Integer.parseInt(request.getParamter("q1"));
userAnswers[1] = Integer.parseInt(request.getParamter("q2"));
userAnswers[2] = Integer.parseInt(request.getParamter("q3"));
userAnswers[3] = Integer.parseInt(request.getParamter("q4"));
userAnswers[4] = Integer.parseInt(request.getParamter("q5"));
userAnswers[5] = Integer.parseInt(request.getParamter("q6"));
userAnswers[6] = Integer.parseInt(request.getParamter("q7"));
for(int i=0; i<correctAnswers.length; i++) {
if(correctAnswers[i]==userAnswers[i]) {
out.println(correctAnswers[i] + " is the correct answer ");
score++;
}
else {
out.println(userAnswers[i] + "is not the correct answer");
}
}
%>
[/nobr]
ah forum is not displaying my variables
<><%
i got it what a day. Paramter is not equal to Parameter :--)
[nobr]i got 2 questions
1) i tried this statement
if(correctAnswers[i]!=usersAnswers[i] || usersAnswers[i]==null)
Just incase if a user does not select any option i still wanted to display the correct result but for some reason i get a jasper error
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: null
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:358)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
root cause
java.lang.NumberFormatException: null
java.lang.Integer.parseInt(Integer.java:415)
java.lang.Integer.parseInt(Integer.java:497)
org.apache.jsp.QuestionForm_jsp._jspService(QuestionForm_jsp.java:88)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:133)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:311)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
note The full stack trace of the root cause is available in the Tomcat logs.
2) i am trying to highlight the wrong answer with red and right answer with green i tried this code but not working
for(int i=0; i<correctAnswers.length; i++) {
if(correctAnswers[i]==usersAnswers[i]) {
out.println(">"
);
out.println("option " + correctAnswers[i] + " is rite");
score = score + 1;
}
else {
out.println(""
);
out.println("<span style=background-color: #FF0000>");
out.println(usersAnswers[i] + " is wrong correct answer is option " + correctAnswers[i]);
//out.println("</span>");
}
}
and here is my full code
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import = "java.util.Calendar" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<b>Displaying The time</b>
<%
Calendar now =Calendar.getInstance();
int hour = now.get(Calendar.HOUR_OF_DAY);
int minute = now.get(Calendar.MINUTE);
if(hour<10)
out.println("0" + hour);
else
out.println(hour);
out.println(":");
if(minute<0)
out.println("0" + minute);
else
out.println(minute);
%>
<h2>Here are your answers</h2>
<br>
<%
int[] correctAnswers;
int[] usersAnswers;
int score;
correctAnswers = new int[7];
usersAnswers = new int[7];
score=0;
correctAnswers[0] = 2;
correctAnswers[1] = 1;
correctAnswers[2] = 1;
correctAnswers[3] = 1;
correctAnswers[4] = 3;
correctAnswers[5] = 2;
correctAnswers[6] = 1;
usersAnswers[0] = Integer.parseInt(request.getParameter("q1"));
usersAnswers[1] = Integer.parseInt(request.getParameter("q2"));
usersAnswers[2] = Integer.parseInt(request.getParameter("q3"));
usersAnswers[3] = Integer.parseInt(request.getParameter("q4"));
usersAnswers[4] = Integer.parseInt(request.getParameter("q5"));
usersAnswers[5] = Integer.parseInt(request.getParameter("q6"));
usersAnswers[6] = Integer.parseInt(request.getParameter("q7"));
for(int i=0; i<correctAnswers.length; i++) {
if(correctAnswers[i]==usersAnswers[i]) {
out.println(""
);
out.println("option " + correctAnswers[i] + " is rite");
score = score + 1;
}
else {
out.println(""
);
out.println("<span style=background-color: #FF0000>");
out.println(usersAnswers[i] + " is wrong correct answer is option " + correctAnswers[i]);
//out.println("</span>");
}
}
%>
You got <%=score %> correct out of <%=correctAnswers.length %>
</body>
</html>
Thanks for any help[/nobr]
