简体   繁体   中英

Strange Behaviour java variable

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 :)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM