如何增加request.getParameter的值?

时间:2013-10-01 11:08:33

标签: java jsp servlets

我正在尝试进行测验,我的查询是ORDER BY RAND()所以这意味着它将是随机的...我的JSP将它发送到servlet来处理流程但是我遇到了问题,我有在数据库中存储了4个带有选择(答案)的问题,我在下面的代码中进行了检索。问题是,当我检索受检者的答案时,唯一检索到的数据是第一个答案...请参阅我的servlet

<%@page contentType="text/html" pageEncoding="UTF-8"%>  
<%@page import="java.sql.*" %>  
<%@page import="database.Connect" %>  

<%   
    Connect conn = new Connect();   
    Statement stmt = conn.getDataConn().createStatement();  
    ResultSet rs = stmt.executeQuery("SELECT * from quiz WHERE category = 'secondary' ORDER BY RAND()");  

%>  

<html>  
    <head>  
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
        <link rel="stylesheet" type="text/css" href="../../css&js/des.css">  
        <script src="css&js/modernizr.custom.js"></script>  

        <title>Question 1</title>  
    </head>  

    <body>  

        <img alt="" src="../../pics/bgimg2.jpg" id="bgimg" />   

        <div class="md-modal md-effect-1 md-show" id="modal-1">  
            <form action="../../Check" method="POST">  
            <table border="1" class="c-form md-content">  
            <% while(rs.next()){   

            %>  
            <tr>  
                <td><h1><%=rs.getString("question")%>?</h1></td>  
                <td><select name="answer">  
                    <option value="<%=rs.getString("choice1")%>"><%=rs.getString("choice1")%></option>  
                    <option value="<%=rs.getString("choice2")%>"><%=rs.getString("choice2")%></option>  
                    <input type="hidden" name="correct" value="<%=rs.getString("correct")%>"/>  
                </select></td>  
            </tr>  

            <%}  
            %>  

            <tr>  
                <td align="center" colspan="2"><input type="submit" value="Next"/></td>  
            </tr>  

            </table>   

            </form>  
        </div>  



    </body>  

我正在尝试进行测验,我的查询是ORDER BY RAND()所以这意味着它将是随机的...我的JSP将它发送到servlet来处理流程但是我遇到了问题,我有在数据库中存储了4个带有选择(答案)的问题,我在下面的代码中进行了检索。问题是,当我检索受检者的答案时,唯一检索到的数据是第一个答案...请参阅我的servlet

<%@page contentType="text/html" pageEncoding="UTF-8"%>  
<%@page import="java.sql.*" %>  
<%@page import="database.Connect" %>  

<%   
    Connect conn = new Connect();   
    Statement stmt = conn.getDataConn().createStatement();  
    ResultSet rs = stmt.executeQuery("SELECT * from quiz WHERE category = 'secondary' ORDER BY RAND()");  

%>  

<html>  
    <head>  
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
        <link rel="stylesheet" type="text/css" href="../../css&js/des.css">  
        <script src="css&js/modernizr.custom.js"></script>  

        <title>Question 1</title>  
    </head>  

    <body>  

        <img alt="" src="../../pics/bgimg2.jpg" id="bgimg" />   

        <div class="md-modal md-effect-1 md-show" id="modal-1">  
            <form action="../../Check" method="POST">  
            <table border="1" class="c-form md-content">  
            <% while(rs.next()){   

            %>  
            <tr>  
                <td><h1><%=rs.getString("question")%>?</h1></td>  
                <td><select name="answer">  
                    <option value="<%=rs.getString("choice1")%>"><%=rs.getString("choice1")%></option>  
                    <option value="<%=rs.getString("choice2")%>"><%=rs.getString("choice2")%></option>  
                    <input type="hidden" name="correct" value="<%=rs.getString("correct")%>"/>  
                </select></td>  
            </tr>  

            <%}  
            %>  

            <tr>  
                <td align="center" colspan="2"><input type="submit" value="Next"/></td>  
            </tr>  

            </table>   

            </form>  
        </div>  



    </body>  

Servlet ...我只是想在这里试验,但我真的需要帮助..对于for和过去的代码输出为null也是失败的,因为第一个答案是olnly检索,我需要增加它。请帮忙

package Servlet;  

import java.io.IOException;  
import javax.servlet.ServletException;  
import javax.servlet.http.HttpServlet;  
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  

public class Check extends HttpServlet {  

    @Override  
    protected void doPost(HttpServletRequest request, HttpServletResponse response)  
            throws ServletException, IOException {  

        for(int i=0;i<10;i++) {    
            String Answer = request.getParameter("answer" + i);  
            System.out.println(Answer);  
        }   


        String Answer = request.getParameter("answer");  
        String Correct = request.getParameter("correct");  
        int score = 0;  


        if (Answer.equals(Correct)) {  
            score++;  
        }  
        System.out.println(score);  



    }  
}  

1 个答案:

答案 0 :(得分:0)

可能是因为您为每个问题选择的标签名称相同:

<select name="answer">  

我认为它应该是唯一的