即使在jsp页面的在线考试关闭窗口后,单选按钮也无法清除

时间:2017-10-03 15:12:47

标签: java jsp

I need your help. Actually I developed an online quiz in struts1.3.it is 
  working fine but when I finished my test and close the browser window and 

登录然后我发现所有单选按钮都标记为关闭窗口之前。     我正在使用java hashmap来存储用户响应(qno。,ans)。     当我在结果行之后清除了hashmap然后它显示了一个      空指针异常。     请告诉我应该怎么做才能清除用户响应      关上窗户后。     我正在使用会话将地图移动到多个页面以获得结果。     请帮忙

<%@page import="com.myapp.struts.Question"%>
<%@page import="java.util.*"%>
<%@page import="java.util.TreeMap"%>
<%@page import="java.util.Map"%>
<%@page import="com.myapp.struts.Db"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="com.myapp.struts.MyBean"%>
<%@page import="java.util.Iterator"%>
<%@page import="java.util.List"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@page import="com.myapp.struts.ActionQuestion"%>

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     <title>JSP Page</title>
         <link rel="stylesheet" type="text/css" 
           href="css/bootstrap.min.css">




         <style>

             <style type="text/css">
    .bs-example{
        margin: 20px;
    }
</style>
         </style>
 <script src="js/jquery-3.2.1.min.js"></script>     
    <script src="js/bootstrap.min.js"></script>



     <script> 

    <!--
    //edit this message to say what you want
    var message = "Right Click Not Allowed";

    function clickIE() {
        if (document.all) {
            alert(message);
            return false;
        }
    }
    function clickNS(e) {
        if (document.layers || (document.getElementById && !document.all)) {
            if (e.which == 2 || e.which == 3) {
                alert(message);
                return false;
            }
        }
    }
    if (document.layers) {
        document.captureEvents(Event.MOUSEDOWN);
        document.onmousedown = clickNS;
    }
    else {
        document.onmouseup = clickNS;
        document.oncontextmenu = clickIE;
    }

    document.oncontextmenu = new Function("return false")
    // -->
</script>
    <font>Banner</font>



        <%!
            static int qid=0;
            Map<Integer,Integer> userans=new LinkedHashMap<Integer,Integer>
                                              ();
 Map<Integer,Integer> dbans=new LinkedHashMap<Integer,Integer>();            


int count=0;List list; 
static int qcounter=1;        
 int qidsetbutton=0;
Question app=null;
 int useroption=0;
int userres=0;


    %>
        <%
                     String sess = (String)session.getAttribute("user");   

   out.println("User-Id:"+sess);
            if( sess==null || sess.equals(""))
        {
        response.sendRedirect("UnAuthorisedAccess.jsp");

        }  




       if(request.getAttribute("list")!=null)
       {
        list=(List)request.getAttribute("list");



             }

            String button= request.getParameter("button1");

       count=list.size(); 

        %>

        <script type="text/javascript">

        function saveanswer() // Save Answer when user click on any radio 
     button
        {
          var useroption = document.querySelector('input[name = 
        "my"]:checked').value;

            window.location.replace("TakeTest.jsp?useroption="+useroption);
        alert("hi"+useroption);
        }
           </script>

   <%
    if(request.getParameter("useroption")!=null)
    {
        try{
         useroption= Integer.parseInt(request.getParameter("useroption"));
          out.println("Rrrrrrrrrrrrrr"+useroption);
         out.println("app.qid="+app.qid());
          userans.put(app.qid(),useroption);
          dbans.put(app.qid(),app.sol());
 session.setAttribute("map",userans);
 session.setAttribute("dbans",dbans);


        }catch(Exception ex)
       {
out.println(" problem during question answer save "+ex);
}


 }
%>





        <%

            if(request.getParameter("q")!=null)
            {
                qid= Integer.parseInt(request.getParameter("q"));

       app=(Question)list.get(qid);


            }
//String str = "<script>document.writeln(name)</script>";
    //out.println("value: " + str);

%>





        <%


session.setAttribute("count",count);



           if(qid==0)
           {
      app=(Question)list.get(0);     
           }
        try{   
          if(button.equals("Next") && qid<count)
       {
        //map3.put(qid,request.getParameter("my"));
           //new Db().userResponse(qid,request.getParameter("my"));
           qcounter=qcounter+1;
           qid=qid+1;
       app=(Question)list.get(qid);

       userres=userans.get(app.qid());

       }
        }catch(Exception ex)
       {
//out.println("probleme in next section "+ex);
       }
       try{
  if(button.equals("Previous") && (qid>0 & qcounter>1))
       {

           qid=qid-1;
       qcounter=qcounter-1;  
      app=(Question)list.get(qid);  

      userres=userans.get(app.qid());


        %>



       <%


       }  

       }catch(Exception ex)
       {
//out.println("probleme in prevoius "+ex);
}%>
    <script type="text/javascript">      

function qidset()
            {
<%
 try{
    if(request.getParameter("qidsetbutton")!=null)


            {

              qidsetbutton = Integer.parseInt(request.getParameter("qidsetbutton"));
          qcounter=qidsetbutton; 
          qid=qidsetbutton-1;
          app=(Question)list.get(qid);

           userres=userans.get(app.qid());
          }
 }catch(Exception ex)
       {
out.println("probleme button refernec section "+ex);
}%>
            %>

            }


       </script>



        <script type="text/javascript">



function hide()
{



if(<%=qcounter%> == 1)
{

document.getElementById('myButton').disabled =true;

}

else if(<%=qcounter%>==<%=list.size()%>)
  {

document.getElementById('myButton1').disabled =true;

}


}





</script>






    <script type="text/javascript">
    function start()
    {
    //startTimer();
     hide();

    }


    </script>
    <style>
       #qid {
  padding: 10px 15px;
  -moz-border-radius: 50px;
  -webkit-border-radius: 50px;
  border-radius: 20px;
}
label.btn {
    padding: 18px 60px;
    white-space: normal;
    -webkit-transform: scale(1.0);
    -moz-transform: scale(1.0);
    -o-transform: scale(1.0);
    -webkit-transition-duration: .3s;
    -moz-transition-duration: .3s;
    -o-transition-duration: .3s
}

label.btn:hover {
    text-shadow: 0 3px 2px rgba(0,0,0,0.4);
    -webkit-transform: scale(1.1);
    -moz-transform: scale(1.1);
    -o-transform: scale(1.1)
}
label.btn-block {
    text-align: left;
    position: relative
}

label .btn-label {
    position: absolute;
    left: 0;
    top: 0;
    display: inline-block;
    padding: 0 10px;
    background: rgba(0,0,0,.15);
    height: 100%
}

label .glyphicon {
    top: 34%
}

        body {

          background-color: #;

        }
    </style>

    </head>










    <body onload="start()" >


        <% out.println("herererer"+userres); %>



        <br><br>
    <center>        <html:form action="/Result">

        <button type="submit" name="button1" class="btn btn-primary btn-danger">Finish</button>
        </html:form>
        </center>
</center>

        <br><br>



        <html:form action="/question">


<div class="container" >




    <div class="row">  <%-- paralle conntent--%>

        <div class="col-xs-6"> 

           <div class="span12">

          <table border="1"  align="center" height="100%" width="150%">        
<tr>        
<td height="100"><font  size="3">Q No.<%=qcounter%>&nbsp; <%=app.stmt()%></font> 
</td>
</tr>

<tr>

<div class="quiz" id="quiz" data-toggle="buttons">

    <td>
           <label class="element-animation1 btn btn-lg btn-primary btn-block "><span class="btn-label"><i class="glyphicon glyphicon-chevron-right"></i></span> <input type="radio"  id="opt1" name="my"  value="1" <%= (userres==1)?"checked":""%> onchange="saveanswer()" /><%=app.opt1()%></label>
     <br>     
    </td>

         </tr>


           <tr>
           <td>
           <label class="element-animation1 btn btn-lg btn-primary btn-block "><span class="btn-label"><i class="glyphicon glyphicon-chevron-right"></i></span> <input type="radio"  id="opt2" name="my"  value="2" <%= (userres==2)?"checked":""%> onchange="saveanswer()" /><%=app.opt2()%></label>
       <br>
       </td>     
    </tr>
    <tr>   
    <td>
       <label class="element-animation1 btn btn-lg btn-primary btn-block ">

onchange =“saveanswer()”/&gt;&lt;%= app.opt3()%&gt;

          <br>                  
          </td>  
    </tr>
    <tr>     
    <td>
          <label class="element-animation1 btn btn-lg btn-primary btn-block 
"><span class="btn-label"><i class="glyphicon glyphicon-chevron-right"></i>
  </span> <input type="radio"  id="opt4" name="my"  value="4" <%= 
  (userres==4)?"checked":""%> onchange="saveanswer()" /><%=app.opt4()%>
  </label>

    </td>
    </tr>
</div>  
</tr>
 <%
    userres=0;    

    %>







             <tr td height="60" align="center">
                 <td>  &nbsp;  
                     <button type="submit" id="myButton" name="button1" 
value="Previous"  class="btn btn-success">Previous</button>

            &nbsp;&nbsp;
            <button type="submit" id="myButton1" name="button1" value="Next"  
     class="btn btn-success">Next</button>

            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;





        </td>

    </tr>




</html:form>


   </table>

    </div>

                  </div>            

                  <div class="col-xs-6"> 


        <html:form action="/question">    <%--pallete code in which click on pallete button reach to particular questions--%>




<table border="1" width=50% height=40% align="right"  style="font-family: Arial;color:black;font-weight: bold" cellspacing=10 cellpadding=10 >
    <tr><td align="right">Pallete Preview</td></tr>
<%


int nq=count;

int col_lastrow=nq%3;

int rows;
int c=0;

if(col_lastrow==0)
rows=nq/3;
else
rows=(nq/3)+1;


for(int j=0;j<rows;j++)
{

%>

<tr align=center style="height:5px">
<%

if(j<(rows-1))
{
for(int i=0;i<3;i++)
{
c++;
%>

<td id="<%=c%>"style="width:50px;height:2px;">
    <button type="submit" id="setqid" name="qidsetbutton" value="<%=c%>"   class="btn btn-toolbar btn-block" onclick="qidset()"><%=c%></button> 


<%

}

%>

</tr>

<%
}

else
{
if(col_lastrow==0)
col_lastrow=3;
for(int i=0;i<col_lastrow;i++)
{
c++;
%>
<td id="<%=c%>"style="width:50px;height:2px;">

    <button type="submit" id="setqid" name="qidsetbutton" value="<%=c%>"  class="btn btn-toolbar btn-block" onclick="qidset()"><%=c%></button>
<%

}

}

%>

</tr>

<%
}
%>

</table>
          </div>         </div>
              </div>








</html:form>



 </body>
</html>


       **Result.jsp**




<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@page import="java.util.Collection"%>
<%@page import="java.util.Set"%>
<%@page import="java.util.Map"%>
<%@page import="com.myapp.struts.Db"%>
<%@page import="com.myapp.struts.ActionQuestion"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>


    <script>
    <!--
    //edit this message to say what you want
    var message = "Right Click Not Allowed";

    function clickIE() {
        if (document.all) {
            alert(message);
            return false;
        }
    }
    function clickNS(e) {
        if (document.layers || (document.getElementById && !document.all)) {
            if (e.which == 2 || e.which == 3) {
                alert(message);
                return false;
            }
        }
    }
    if (document.layers) {
        document.captureEvents(Event.MOUSEDOWN);
        document.onmousedown = clickNS;
    }
    else {
        document.onmouseup = clickNS;
        document.oncontextmenu = clickIE;
    }

    document.oncontextmenu = new Function("return false")
    // -->
</script>


    </head>
    <body>

        <%!

        Db db=null;
        int count=0;
        int correct=0;
        int incorrect=0;
        int attempt=0;
        int unattempt=0;
        double percentage;
        Map map1=null;
        Map map2=null;

%>


        <%
        String sess = (String)session.getAttribute("user");   

   out.println("User-Id:"+sess);
            if( sess==null || sess.equals(""))
        {
        response.sendRedirect("UnAuthorisedAccess.jsp");

        }  

            if(session.getAttribute("map")!=null)
            {
           map1=(Map)session.getAttribute("map");  //user response map


        map2=(Map)session.getAttribute("dbans");  //database answer map



       Integer count=(Integer)session.getAttribute("count");









    Set <Integer>s1=map1.keySet();
Set <Integer>s2=map2.keySet();

Collection cl=map1.values();
Collection c2=map2.values();



List map1value=new ArrayList();
map1value.addAll(cl);


List map2valu=new ArrayList();
map2valu.addAll(c2);

out.println(map1value.get(0));
out.println(map2valu.get(0));

List map1key=new ArrayList(s1);
List map2key=new ArrayList(s2);




for(int i=0;i<map1key.size();i++)
{



if(map1key.get(i).equals(map2key.get(i)))
{

    if(map1value.get(i).equals(map2valu.get(i)))

    {
out.println(map1value.get(i));
out.println(map2valu.get(i));        

    ++correct;
}
else
    {
++incorrect;        
    }

}

else
    {
++incorrect;        
    }

}


attempt=correct+incorrect;
unattempt=count-attempt;  
percentage=Math.round((correct*100.0)/count);




        %>




        <br><br><br><br>
    <center><font size="5" color="green">Result</font></center>
    <br><br><br>    
    <table border="1" height="100" width="500" align="center">

            <tbody align="center">

                <tr style=" color: ">
                    <td>Total Questions:<%=count%></td>

                </tr>
                <tr style=" color: ">
                    <td>Right Answer:<%=correct%></td>
                </tr>
                <tr style=" color:">
                <td>Wrong Answer:<%=incorrect%></td>
                </tr>

                <tr style=" color:">
                    <td>Attempted:<%=attempt%></td>
                </tr>
                <tr style=" color:">
                    <td>Not Attempted:<%=unattempt%></td>
                </tr>

                   <tr style=" color:">
                       <td>Percentage:<%=percentage%><i>%</i></td>
                </tr>

            </tbody>
        </table>


    <%

        }

else
{
   out.println("you couldn't attempt any question so your result wouldn't consider");
}

    %>

</body>
</html>

1 个答案:

答案 0 :(得分:0)

如果您将收音机保存为选择,它将永远不会消失。它将默认它。选择你的选择。