Islam Ismayilov
Islam Ismayilov

Reputation: 113

temporary variable save in jsp

i have created quiz project. i want to save show user answerlist when click option radio button. at last exam i want with ajax insert mysql all answerlist.

in parse answerlist i write such code. but in every new page click option previos answer table shows null.

please advise me how temporary save all answer in jsp then insert mysql :

<html>
<head>
    <%        Logger log =   Logger.getLogger("Test");%>

    <% Long qnum =     (Long) session.getAttribute("qnumer"); %>
    <% String answer = (String) session.getAttribute("answer");%>
    <%log.info(qnum + answer);%>
    <% String [] answerqlist = new String[6] ; %>

     <%switch (Integer.parseInt(String.valueOf(qnum))){
            case  1:
                String a1 = answer;
                answerqlist[0]=a1;
                break;
            case  2:
                String a2 = answer;
                answerqlist[1]=a2;
                break;
            case  3:
                String a3 = answer;
                answerqlist[2]=a3;
                break;
            case  4:
                String a4 = answer;
                answerqlist[3]=a4;
                break;
            case  5:
                String a5 = answer;
                answerqlist[4]=a5;
                break;
            case  6:
                String a6 = answer;
                answerqlist[5]=a6;
                break;
    }%>
</head>
<body>

<table   cellspacing="0" width="100%"  style="border: solid 1px;" >
    <thead>
    <tr>
        <th>1</th>
        <th>2</th>
        <th>3</th>
        <th>4</th>
        <th>5</th>
        <th>6</th>
    </tr>
    </thead>
    <tbody>
    <tr align="center">
        <td><%=answerqlist[0]%></td>
        <td><%=answerqlist[1]%></td>
        <td><%=answerqlist[2]%></td>
        <td><%=answerqlist[3]%></td>
        <td><%=answerqlist[4]%></td>
        <td><%=answerqlist[5]%></td>
    </tr>
    </tbody>
</table>
</body>
</html>

Upvotes: 0

Views: 1198

Answers (1)

AxelH
AxelH

Reputation: 14572

You are getting from the session :

<% Long qnum =     (Long) session.getAttribute("qnumer"); %>
<% String answer = (String) session.getAttribute("answer");%>

But I don't see anywhere the place where you set those values in the session.

The same will be for the array answerqlist, you are building a new one every page, you should save it into the session (and load it too).

PS : You should really take a look to How to avoid Java code in JSP files?

PS 2: The switch is not necessary, since this could be reduce as :

int index = Integer.parseInt(String.valueOf(qnum));
answerqlist[index - 1] = answer;

You are doing the same over and over but with the index depending on the qnum. Of course, you can add a check to prevent an exception.

Upvotes: 1

Related Questions