0

我的任务要求我们将信息从 java servlet 发送到 jsp 页面。

我的问题是关于调用函数。它有一个sql查询,可以找到多个结果我遇到的问题是它只将结果集中的最后一个返回到jsp页面,而不是所有结果。

这是小服务程序:

private void sendBack(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession(true);

    //Set data you want to send back to the request (will be forwarded to the page)
//Can set string, int, list, array etc.
String sql = "SELECT id, user_id" +
          " FROM lab" +
        " WHERE user_id=" + (Integer)session.getAttribute("id");

  try{
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/wae","root","");
    System.out.println("got boobs");
    System.out.println(session.getAttribute("id"));

  Statement stmt = con.createStatement();
  ResultSet res = stmt.executeQuery(sql);
  ArrayList<String> list1 = new ArrayList<String>();
  if (res.next()){
      do{
           list1.add(res.getString(1));
           list1.add(res.getString(2));
      }while(res.next());

      request.setAttribute("res", res);
      }
      for(int i=0;i<list1.size();i++){
          System.out.println(list1.get(i));
      }
  }catch (SQLException e) {
    } 
    catch (Exception e) {
    } 


    //Decides what page to send the request data to
    RequestDispatcher view = request.getRequestDispatcher("Student_manage.jsp");
    //Forward to the page and pass the request and response information
    view.forward(request, response); 
}

这是jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Mars University Lab System</title>
    <link rel="stylesheet" href="style.css" type="text/css" media="screen">
</head>

<body>
<jsp:include page="headerStudent.jsp"/>


<tr>
<td>
</td>
</tr>

<tr>
<td>
<div id = "centrecontent">
<br>
<h3>Manage Timetable</h3>

  TESTING THE NO FORM REDIRECTION HERE:<br>

<% String data1 = String.valueOf(request.getAttribute("data1")); %>
 <% String data2 = String.valueOf(request.getAttribute("data2")); %>
<%=data1 %>
<%=data2 %>


</div>

<jsp:include page="footer.jsp"/>


</body>

</html>

我相信我需要在 jsp 页面中使用某种 do 语句来逐步检查每个结果。

4

1 回答 1

2

你的逻辑是错误的

您迭代一行rs并将其存储在请求属性data1data2中。然后你得到下一行,你一遍又一遍地覆盖相同的属性。

因此,您只剩下 . 的最后一个值rs

我认为最好将您的结果存储在 aCollection中,然后将您的结果存储Collection在您的request对象中

更新1

考虑这个示例代码

public class MyObject{
  private String data1;
  private String data2;
    public void setData1(String data1) {
        this.data1 = data1;
    }

    public String getData1() {
        return data1;
    }

    public void setData2(String data2) {
        this.data2 = data2;
    }

    public String getData2() {
        return data2;
    }
}

这是你的对象。

然后像这样在上面的代码中进行更改:

List <MyObject> list = new List<MyObject>();
if (res.next()){
    MyObject obj = new MyObject();
    do{
          obj.setData1(res.getString(1));
          obj.setData2(res.getString(2));
          list.add(obj);
    }while(res.next());

request.setAttribute("results", list);
}

这将List在名为的请求属性中添加results

然后您可以使用检索列表

List <MyObject> results = (List<MyObject>) request.getAttribute("results");

更新2

PS:删除do/while循环是一个很好的做法,因为它容易出错并用这样的替换while

    while(res.next()){
          obj.setData1("data1", res.getString(1));
          obj.setData2("data2", res.getString(2));
          list.add(obj);
    }

更新3

像这样打印到jsp

<%@page import="java.util.*"%>
<%
  List <MyObject> results = (List<MyObject>) request.getAttribute("results");
  for(MyObject obj : results){
    out.write(obj.getData1() + " "+ obj.getData2() + "<br/>";
  }
%>
于 2012-10-04T07:51:23.930 回答