我的作业要求我们将信息从java servlet发送到jsp页面。
我的问题是关于调用函数。它有一个sql查询,它找到多个结果我遇到的问题是它只返回结果集中的最后一个到jsp页面,而不是所有的结果。
这是servlet:
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语句来逐步完成每个结果。
答案 0 :(得分:2)
你的逻辑错了
您迭代rs
的一行,并将其存储在请求属性data1
和data2
中。
然后你得到下一行,你反复覆盖相同的属性。
因此,您将获得rs
的最后一个值。
我认为最好将结果存储在Collection
中,然后将Collection
存储在request
对象中
<强> UPDATE1 强>
考虑这个示例代码
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");
<强> UPDATE2 强>
PS:最好删除do/while
循环,因为它容易出错并将其替换为while
这样的
while(res.next()){
obj.setData1("data1", res.getString(1));
obj.setData2("data2", res.getString(2));
list.add(obj);
}
<强> UPDATE3 强>
将它打印到像这样的jsp
<%@page import="java.util.*"%>
<%
List <MyObject> results = (List<MyObject>) request.getAttribute("results");
for(MyObject obj : results){
out.write(obj.getData1() + " "+ obj.getData2() + "<br/>";
}
%>