如何在JSP中的表中显示多行?

时间:2015-02-04 13:31:48

标签: java html jsp

这是我的Servlet代码:

while(rs.next()){
    int questionId = rs.getInt("question_id");
    String questions = rs.getString("questions_name");
    String option1 = rs.getString("option1");
    String option2 = rs.getString("option2");
    String option3 = rs.getString("option3");
    String option4 = rs.getString("option4");
    String correctAns = rs.getString("correct_ans");

    request.setAttribute("questionId", questionId);
    request.setAttribute("questions", questions);
    request.setAttribute("option1", option1);
    request.setAttribute("option2", option2);
    request.setAttribute("option3", option3);
    request.setAttribute("option4", option4);
    request.setAttribute("correctAns", correctAns);
}

这是我的DAO代码:

 public ResultSet StartTest(Test passData) throws SQLException{
        Statement myStatement = getConnection();
        String query;
        query = "SELECT question_id, questions_name, option1, option2, option3, option4, correct_ans "
                + "FROM question WHERE courses_codes = '"+passData.getCourseCode()+"'"
                + "ORDER BY RAND()"
                + "LIMIT 5";
        rs = myStatement.executeQuery(query);
        return rs;
    }

JSP代码:

<table id="TTquestionDiv">
    <tr>
        <td>${questions}${b}</td>
    </tr>
    <tr>
        <td><input name="${questionId}" type="radio" value="${option1}" />${option1}${b}</td>
    </tr>
    <tr>
        <td><input name="${questionId}" type="radio" value="${option2}" />${option2}${b}</td>
    </tr>
    <tr>
        <td><input name="${questionId}" type="radio" value="${option3}" />${option3}${b}</td>
    </tr>
    <tr>
        <td><input name="${questionId}" type="radio" value="${option4}" />${option4}${b}</td>
    </tr>
    <tr>
        <td><input name="${questionId}" type="radio" value="${correctAns}" />${correctAns}${b}</td>
    </tr>
</table>

1 个答案:

答案 0 :(得分:0)

在servlet中使用此代码

HttpSession session=request.getSession();  
Arraylist<Person>listPerson=daoPerson.getLisPersons();
        session.setAttribute("lsperson",listPerson);  

在jsp页面中导入jstl标记库

 <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

并输入此代码

   <c:forEach items="${lsperson}" var="person">
        <tr>      
            <td>${person.name}</td>
            <td>${person.lastName}</td>
            <td>${person.age}</td>
        </tr>
    </c:forEach>