<%@ page import = "java.util.*"%>
<%@ page import = "java.lang.*"%>
<%@ page import="java.sql.*" %>
<%@ page language="java" import="java.sql.*" errorPage="" %>

<!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=UTF-8">
<title>JSP Page</title>
</head>
<body>
<% 
    String ans=" ";
if(request.getParameter("correctAns")!=null)
    {
        ans=request.getParameter("correctAns").toString();
    }
        Connection conn = null;
        String url = "jdbc:mysql://localhost:3306/";
        String db = "test";
        String driver = "com.mysql.jdbc.Driver";
        String userName ="root";
        String pass="password";
        Statement st = null;
        ResultSet qrst;
        ResultSet rs = null;



    String a,b;
   int count = 0;
    int items = 0 ;


try {

    Class.forName(driver);
    conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root", "password");

    st = conn.createStatement();
    rs = st.executeQuery("select * from questionnaires");

while(rs.next()) {
%>
<br>
<br/>
<center>

<table border="1" width="500px" bgcolor="white" cellspacing="0" cellpadding="0">
<tr>
<td width="100%">

<form name="form1" >

<h2 align="center"><font color="red">Online Quize Application</font></h2>

<b>Select Correct Answer</b>
<table border="0" width="500px" cellspacing="2" cellpadding="4">
<tr>
<td width="50%"> Question Number: <%= rs.getString("qid")%></td>


<input type="hidden" name="correctAns" value="<%=rs.getString("correctAns")%>" />
<tr>
<td><%= rs.getString("quest") %></td></tr>
<tr>
<td>

1: <input type="radio" name="a" value= "QA" /></td>
<td><%= rs.getString("QA") %></td></tr> 
<tr>
<td>
2: <input type="radio" name="a" value="QB" /></td>
<td><%= rs.getString("QB") %></td></tr>

<tr>
<td>
3: <input type="radio" name="a" value="QC" /></td>
<td><%= rs.getString("QC") %> </td></tr>

<tr>
<td>
4: <input type="radio" name="a" value="QD" /> </td>
<td> <%= rs.getString("QD") %> </td></tr>




<tr>
<td>
<center>
<input type="submit" value="Submit" name="submit"></center></td></tr>
</table>

</form>
</td>
</tr>
</table>
</center>
</body>


<%
}
     a=request.getParameter("a");

 if(a.equals(ans))
        {

        count++;
    items++;
        out.println("Correct");

         }
        else{
        out.println("wrong");
    items++;
    }

}

catch (Exception ex) {
ex.printStackTrace();

%>

<%
} finally {
if (rs != null) rs.close();
if (st != null) st.close();
if (conn != null) conn.close();
}
%>


    <% 







if(count <= items)
    {
        //String strcount = Integer.toString(count);
        //String stritems = Integer.toString(items);
        //out.println("Score :" . count . "/" .items);

        out.print("Score :");
        out.print(count);
        out.print("/");
        out.println(items);
    }
%>
</html>

Hello, I'm creating a simple Online Exam System but i'm having some problem with my codes. I want to count all the correct answers and wrong answers. How should I do it? I mean, what code should I use? I'm a beginner in jsp. Please help. Thank you so much.

Recommended Answers

All 3 Replies

my advice: you seem to know the basics of jsp, no head over to servlets.
that should get all of that java code out of the jsp file, and make it a lot easier to work with.

Oh thank you for that sir, But I just wanna ask what should I use because in that code that I'm doing it only checks 1 by 1. Maybe theres a code to check all at once but idk what should I use.

they're all different components, so I don't see how you would put a 'check all' in there, without writing a check all method that checks them one by one

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.