0

I have encountered a strange behaviour which i cannot explain. Here is my JSP pages code

<%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*,java.util.*" errorPage="" %>   
<%
String qid = request.getParameter("qid");
int qno;
int choice = 0;
if(qid==null)
qno = 1;
else
qno = Integer.parseInt(qid);
%>
<script>
    function f1()
    {
      var check;
    if(document.getElementById('r1').checked||document.getElementById('r2').checked||document.getElementById('r3').checked||document.getElementById('r4').checked)
    {   
        check = true;
        if(document.getElementById('r1').checked){
        <% choice = 1; %>
        }
        else if(document.getElementById('r2').checked){
        <% choice = 2; %>
        }
        else if(document.getElementById('r3').checked){
            <% choice = 3; %>
        }
        else {
          <% choice = 4; %>
        }
    }
    else{
        alert("Select an answer");
        check = false;
    }
     if(check){

    <%
    out.println("document.form1.action=\"starttest.jsp?qid="+qno+"&choice="+choice+"\"");
    %>
    document.form1.method="post";
    document.form1.submit();
    }
    }
</script>
<form id ="form1" name="form1" method="post" action="">
<%
try{
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost/examination?" +"user=root&password=nawed");
PreparedStatement ps=conn.prepareStatement("select question_text from question_master where question_id=?");
ps.setInt(1,qno);
ResultSet rs=ps.executeQuery();
while(rs.next()){
out.println("1><B>"+rs.getString(1)+"</B><br/>");
}
ps=conn.prepareStatement("select choice_1,choice_2,choice_3,choice_4,answer from multichoice_question where ques_id=?");
ps.setInt(1,qno);
rs= ps.executeQuery();
while(rs.next()){
  session.setAttribute("pans",rs.getString(5));
%>
<input type="Radio" name="radio" id="r1"/><%=rs.getString(1)%><br/>
<input type="Radio" name="radio" id="r2"/><%=rs.getString(2)%><br/>
<input type="Radio" name="radio" id="r3"/><%=rs.getString(3)%><br/>
<input type="Radio" name="radio" id="r4"/><%=rs.getString(4)%><br/>
<input type="button" name="button" value="Submit" onclick="f1()"/>
<%
}
ps.close();
rs.close();
}catch(Exception e){}
%>
</form>
</body>
</html>

The problem is that i am getting choice value 4 everytime in query string but intrestingly problems get solved when i use javaScript variable instead like:

<script>
    function f1()
    {
      var check;
    if(document.getElementById('r1').checked||document.getElementById('r2').checked||document.getElementById('r3').checked||document.getElementById('r4').checked)
    {   var choice;
        check = true;
        if(document.getElementById('r1').checked){
         choice = 1;
        }
        else if(document.getElementById('r2').checked){
         choice = 2;
        }
        else if(document.getElementById('r3').checked){
             choice = 3;
        }
        else {
           choice = 4; 
        }
    }
    else{
        alert("Select an answer");
        check = false;
    }
     if(check){

    <%
    out.println("document.form1.action=\"starttest.jsp?qid="+qno+"&choice=\"+choice");
    %>
    document.form1.method="post";
    document.form1.submit();
    }
    }
</script>

I cant explain this Strange behaviour.It will be very helpful if anyone can explain this in simple way :)

1
  • 3
    Its not strage, you're mixing up JavaScript and Java. The Java lines get executed when rendering the page, way before JavaScript gets executed. Commented Nov 23, 2013 at 15:49

1 Answer 1

1

JavaScript runs on the client side. Java runs on the application / web server.

Think of a JSP as a big Java method which outputs the client's document (HTML, JavaScript, CSS, etc.).

Therefore, in the below snippet:

    if(document.getElementById('r1').checked){
    <% choice = 1; %>
    }
    else if(document.getElementById('r2').checked){
    <% choice = 2; %>
    }
    else if(document.getElementById('r3').checked){
        <% choice = 3; %>
    }
    else {
      <% choice = 4; %>
    }

These 4 lines execute sequentially. You can look at the compiled JSP code to verify this.

choice = 1;
choice = 2;
choice = 3;
choice = 4;

Therefore, choice will end up being 4. The if statements are JavaScript, which the browser will execute and have nothing to do with the Java logic.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.