0

hi all i have created a jsp page for online exam here i have created a database to store all the question and answers ,used the database to fill all the radio button values,but whenever i retrieve the radio button value and compare it with the database value it is not comparing it...pls find the code below and give me some suggestions about it..

Index.jsp

<%@ page import="java.sql.*"    %>
<%@ page import="java.lang.String.*"    %>

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h2>Online Exam</h2>
        <form action="process.jsp" method="post">
            <%
                        Connection con = null;
                        try {
                            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                            con = DriverManager.getConnection("jdbc:derby://localhost:1527/student");
                            Statement st = con.createStatement();
                            ResultSet rs = null;
                            String sql = "select * from qtable";
                            st = con.createStatement();
                            rs = st.executeQuery(sql);
                            int i = 1;
         while (rs.next()) {
String nn="p";






            %>

            <%out.println(rs.getString("qid") + ".");
                                            out.println(rs.getString("question"));

            %><br>
            <input type="radio" name="<%= i%>"  value="<%out.println(rs.getString("op1"));%>" /><%out.println(rs.getString("op1"));%>
            <input type="radio" name="<%= i%>" value="<%out.println(rs.getString("op2"));%>" /><%out.println(rs.getString("op2"));%>
            <input type="radio" name="<%= i%>" value="<%out.println(rs.getString("op3"));%>"/><%out.println(rs.getString("op3"));%><br><br>
<%out.println(nn); %>

            <%
                                            i++;

                                        }
            %>
            <br><input type="submit" name="sub" value="submit"/>
        </form>
        <%

                    } catch (Exception e) {

                        out.println("TRY AGIN FAST........");

                    }


        %>




    </body>
</html>

Below contains the backend...
Quiz.JSP

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<%@ page import="java.lang.String.*"    %>
<!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>

        <%
                    Connection con = null;
                    try {

                        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                        con = DriverManager.getConnection("jdbc:derby://localhost:1527/student");
                        Statement st = con.createStatement();

                        String sql = "select * from qtable";
                        ResultSet rs = st.executeQuery(sql);
                        int i = 1, mark = 0;
                        while (rs.next()) {
                            String ans = request.getParameter("" + i);
                            String cans = rs.getString("cans");
                            out.println(cans);
                            out.println(ans);
                            if (cans.equals(ans)) {

                                out.println("works");

                            }



                            i++;
                        }



                    } catch (Exception e) {

                        out.println("GOT an Error Joe Please try again....." + e.getMessage());

                    }


        %>





    </body>
</html>
3
Contributors
8
Replies
27
Views
3 Years
Discussion Span
Last Post by joelanandraj
2

whenever i retrieve the radio button value and compare it with the database value it is not comparing it

either you are comparing it, or you aren't. which is it? and what code is it that isn't working?

I assume it is this part?

if (cans.equals(ans)) {
                                out.println("works");
                            }

why do you assume it doesn't work? have you debugged it?

you may want to refactor your code a bit. it's bad design to have Java code inside your .jsp file, use servlets for that.

Votes + Comments
I hate, absolutely HATE, scriptlets.
0

In the file quiz.jsp in line number 28 and 29 i have printed the value of cans and ans and it got printed both are same values only ,but when i am using it inside the if condition to check their similarity the control is not going inside the If block. any idea

2

Change your println calls to

out.println("<pre>==="+ans+"===</pre>");
out.println("<pre>==="+cans+"===</pre>");

to ensure no whitespace is messing up your comparison.

But you REALLY need to learn how to use beans and the varying jstl tag libraries as all this scriptlet stuff is BAD. Scriptlets are JSP 1.0. The ONLY reason they still exist is backwards compatability (you do not want to be backwards, do you?). They are extremely bad for readability, maintainability, and scalability. There is NOTHING you can do with a scriptlet that you cannot do with Beans and jstl tags, and those two things eliminate the problems with scriptlets (not to mention how hard scriptlets are to properly debug).

Edited by masijade

Votes + Comments
trim was what I had in mind too.
0

ya i understood the problem of using scriplets ,but i need it in this way actually so can u help...

1

Did you try what was already posted (other than the beans and jstl advice, I have no wish to "speak to the wall", although I cannot imagine a case where it would be "need in this way", unless it is course work and required by the instructor, in which case I would look for a new course).

0

ya i am instructor ,just need it done for my lab work...i have tried out

This was the result....
===SimpleObjectAccessProtocol===

===SimpleObjectAccessProtocol

===browser===

===browser

The second line in each answer is getting breaked up

Edited by joelanandraj

Attachments Capture.PNG 2.92 KB
0

Then call trim on the one that is getting broken up.

Edit: Better would be to call it on both (but you should probably do null checks before doing that).

Edited by masijade

0

wow...Thanks a lot its working....as you said i have used the trim() in both the variables its working ....thanks a lot....

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.