首页 新闻 搜索 专区 学院

如何将Java中数据显示到jsp页面之中

0
悬赏园豆:20 [已解决问题] 解决于 2015-01-27 14:09

废话不说,先上代码!问题在最后

package com.chk.doubleball.setvlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.TreeSet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.chk.doubleball.model.Bonus;
import com.chk.doubleball.model.DdwhOne;
import com.chk.doubleball.util.DBUtil;

public class SearchDdwhoneServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 1、查询数据
        ArrayList<DdwhOne> ddwhoneList = new ArrayList<DdwhOne>();

        String sql = "select * from t_bonus where _id > (select count(*) from t_bonus) - 100;";
        Connection conn = DBUtil.getConn();
        PreparedStatement pstmt = null;
        PreparedStatement pstmt1 = null;
        ResultSet rs = null;
        ResultSet rs1 = null;

        try {
            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery();

            while (rs.next()) {
                DdwhOne d = new DdwhOne();
                d.set_id(rs.getInt("_id"));
                d.setIssue(rs.getInt("issue"));
                d.setOrder_red_one(rs.getString("order_red_one"));
                d.setOrder_red_two(rs.getString("order_red_two"));
                d.setOrder_red_three(rs.getString("order_red_three"));
                d.setOrder_red_four(rs.getString("order_red_four"));
                d.setOrder_red_five(rs.getString("order_red_five"));
                d.setOrder_red_six(rs.getString("order_red_six"));
                d.setBlue(rs.getString("blue"));
                // 调用前一期的数据
                ArrayList<Object> danArrayList = new ArrayList<Object>();
                
                int idNum = d.get_id() - 1;
                pstmt1 = conn.prepareStatement("select * from t_bonus where _id = ? ");
                pstmt1.setInt(1, idNum);
                rs1 = pstmt1.executeQuery();
                Bonus b = new Bonus();
                while (rs1.next()) {
                    b.setIssue(rs1.getInt("issue"));
                    b.setOrder_red_one(rs1.getString("order_red_one"));
                    b.setOrder_red_two(rs1.getString("order_red_two"));
                    b.setOrder_red_three(rs1.getString("order_red_three"));
                    b.setOrder_red_four(rs1.getString("order_red_four"));
                    b.setOrder_red_five(rs1.getString("order_red_five"));
                    b.setOrder_red_six(rs1.getString("order_red_six"));
                    b.setBlue(rs1.getString("blue"));

                    // 第一组胆码:红一位 + 蓝
                    int dan_one = Integer.parseInt(b.getOrder_red_one()) + Integer.parseInt(b.getBlue());
                    if (dan_one > 33) {
                        dan_one = dan_one - 33;
                    }

                    int dan_one_a = dan_one - 1;
                    int dan_one_b = dan_one + 1;

                    // 第二组胆码:红三位拆分 + 篮球拆分
                    int a_ch1 = Integer.parseInt(String.valueOf(b.getOrder_red_three().charAt(0)));
                    int a_ch2 = Integer.parseInt(String.valueOf(b.getOrder_red_three().charAt(1)));
                    int b_ch1 = Integer.parseInt(String.valueOf(b.getBlue().charAt(0)));
                    int b_ch2 = Integer.parseInt(String.valueOf(b.getBlue().charAt(1)));

                    int dan_two = a_ch1 + a_ch2 + b_ch1 + b_ch2;
                    int dan_two_a = dan_two + 1;
                    int dan_two_b = dan_two - 1;

                    // 第三组胆码:红二位 + 篮球
                    int dan_three = Integer.parseInt(b.getOrder_red_two()) + Integer.parseInt(b.getBlue());
                    if (dan_three > 33) {
                        dan_three = dan_three - 33;
                    }

                    int dan_three_a = dan_three - 1;
                    int dan_three_b = dan_three + 1;

                    danArrayList.add(dan_one);
                    danArrayList.add(dan_one_a);
                    danArrayList.add(dan_one_b);
                    danArrayList.add(dan_two);
                    danArrayList.add(dan_two_a);
                    danArrayList.add(dan_two_b);
                    danArrayList.add(dan_three);
                    danArrayList.add(dan_three_a);
                    danArrayList.add(dan_three_b);
                    
                    //剔除重复并排序
                    TreeSet<Object> ts = new TreeSet<Object>(danArrayList);
                    danArrayList.clear();
                    danArrayList.addAll(ts);
                    
                    /*
                     * 计算出来的所有值都保存在danArrayList中,现在我想要把danArrayList中的数据都显示在jsp页面中,请问我该怎么办?
                     * 
                     */
                }
                ddwhoneList.add(d);
            }

        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            DBUtil.close(rs);
            DBUtil.close(pstmt);
            DBUtil.close(conn);
        }
        request.setAttribute("ddwhoneList", ddwhoneList);
        // 2、跳转
        request.getRequestDispatcher("/table/table_dadiweihong.jsp").forward(request, response);

    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}

jsp页面如下

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>综合图表 -- 大底围红(一)</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">

<link rel="stylesheet" type="text/css" href="js/ddwh.css">

<script type="text/javascript" src="js/jquery-1.10.1.js"></script>

</head>

<body>
    <!-- title -->
    <jsp:include page="/global/title.jsp"></jsp:include>
    <!-- logo -->
    <jsp:include page="/global/logo.jsp"></jsp:include>
    <!-- navigation -->
    <jsp:include page="/global/navigation.jsp"></jsp:include>
    <!-- roll -->
    <jsp:include page="/global/roll.jsp"></jsp:include>

    <br>

    <table border="1">
        <caption>大底围红</caption>
        <tr>
            <td rowspan="2" class="issue">期号</td>
            <td colspan="6" class="red">红球</td>
            <td rowspan="2">蓝球</td>
            <td colspan="33" class="weihong">围红</td>
            <td rowspan="2">验证</td>
        </tr>
        <tr>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td>01</td>
            <td>02</td>
            <td>03</td>
            <td>04</td>
            <td>05</td>
            <td>06</td>
            <td>07</td>
            <td>08</td>
            <td>09</td>
            <td>10</td>
            <td>11</td>
            <td>12</td>
            <td>13</td>
            <td>14</td>
            <td>15</td>
            <td>16</td>
            <td>17</td>
            <td>18</td>
            <td>19</td>
            <td>20</td>
            <td>21</td>
            <td>22</td>
            <td>23</td>
            <td>24</td>
            <td>25</td>
            <td>26</td>
            <td>27</td>
            <td>28</td>
            <td>29</td>
            <td>30</td>
            <td>31</td>
            <td>32</td>
            <td>33</td>
        </tr>
        <c:if test="${not empty ddwhoneList}">
            <c:forEach items="${ddwhoneList }" var="a">
                <tr>
                    <td>${a.issue }</td>
                    <td>${a.order_red_one }</td>
                    <td>${a.order_red_two }</td>
                    <td>${a.order_red_three }</td>
                    <td>${a.order_red_four }</td>
                    <td>${a.order_red_five }</td>
                    <td>${a.order_red_six }</td>
                    <td>${a.blue }</td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                </tr>
            </c:forEach>
        </c:if>
    </table>
    <br>
    <!-- footer -->
    <jsp:include page="/global/footer.jsp"></jsp:include>
</body>
</html>


问题:

  将计算出来的数据显示在页面的指定位置(如下图:01~33下边对应的位置),为了便于说明问题,我上一个示例图给大家。

图中,围红下边对应的01~33下边的数,就是计算出来的数据应该显示的位置!!

horse_leo的主页 horse_leo | 初学一级 | 园豆:17
提问于:2013-09-16 16:00
< >
分享
最佳答案
0

setvlet+jsp?现在web项目都是用框架

收获园豆:10
二十四号同学 | 菜鸟二级 |园豆:216 | 2013-09-16 17:29

我现在还没学到框架呢!请你按我的思路帮我看看

horse_leo | 园豆:17 (初学一级) | 2013-09-16 18:00

@horse_leo:

 servlet用的
request.setAttribute("list", list)
request.getRequestDispatcher("").forward(request, response)
方法传递的这个list

然后用jstl遍历就行了

<c:forEach items="${}" var="arrayListI">
${arrayListI}
</c:forEach> 

好久不用了servlet,不知道能不能帮到你。

二十四号同学 | 园豆:216 (菜鸟二级) | 2013-09-16 18:15
其他回答(2)
0

学习了

收获园豆:5
铭心之路 | 园豆:209 (菜鸟二级) | 2013-09-16 16:18
0

学下jsp+servlet,要不然你是做不出来的

收获园豆:5
小小树 | 园豆:213 (菜鸟二级) | 2015-01-16 16:44
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册