临时变量保存在jsp中 [英] temporary variable save in jsp

查看:115
本文介绍了临时变量保存在jsp中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了测验项目.单击选项单选按钮时,我想保存显示用户答案列表.在最后一次考试中,我想用Ajax插入mysql所有答案列表.

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.

在解析答案列表中,我编写了这样的代码.但在每个新页面中,单击选项previos答案表均显示为空.

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

请告诉我如何将所有答案临时保存在jsp中,然后插入mysql:

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>

推荐答案

您正在从会话中获取信息:

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.

对于数组answerqlist也是一样,您要在每个页面上构建一个新页面,应将其保存到会话中(并也加载它).

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:您真的应该看看如何避免JSP文件中的Java代码?

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

PS 2:不需要切换,因为可以将其减少为:

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

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

您一遍又一遍地执行相同的操作,但是索引取决于qnum.当然,您可以添加检查以防止异常.

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.

这篇关于临时变量保存在jsp中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆