user1393064
user1393064

Reputation: 411

Sending information from servlets to jsp pages

It is a requirement of my assignment that we send information from the java servlet to the jsp page.

My question is regarding calling a function. It has a sql query in it that finds multiple results the problem I'm having is that it is only returning the very last in the result set to the jsp page, NOT ALL THE RESULTS.

Here is the 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); 
}

and here is the 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>

I believe I need some sort of do statement in the jsp page to step through each result.

Upvotes: 0

Views: 2767

Answers (1)

MaVRoSCy
MaVRoSCy

Reputation: 17849

Your logic is wrong

You iterate a row of the rs and store it in the request attributes data1 and data2. Then you get the next row and you overwrite the same attributes over and over again.

And therefore you are left with the last value of the rs.

I think it would be better to store your results in a Collection, and then store your Collection in your request object

UPDATE1

Consider this sample code

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;
    }
}

This is your Object.

Then Do the changes in your code above like this:

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);
}

This will add the List in a request attribute named results

Then you can retrieve the list using

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

UPDATE2

PS: It is a good practice to remove the do/while loop because it is error prone and replace it with a while like this

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

UPDATE3

Print it to jsp like this

<%@page import="java.util.*"%>
<%
  List <MyObject> results = (List<MyObject>) request.getAttribute("results");
  for(MyObject obj : results){
    out.write(obj.getData1() + " "+ obj.getData2() + "<br/>";
  }
%>

Upvotes: 2

Related Questions