I am accessing a db in a servlet and want to display all the rows of a table in a jsp, to divide the presentation from the logic. Essentially I want to pass an object (ResultSet rs) from the servlet to the jsp, so within the jsp I can call something like:
<tr>
while(rs.next())
<td><%=Record.firstfield%></td>
<td><%=Record.secondfield></td>
</tr>
I am unclear on the use of a directive (syntax <@ page...%>
to import the needed packages as well as the classes and objects (like the ResultSet) instantiated in a separate servlet held in the same container>
-
newbie: passing servlet data to a jsp (7 messages)
- Posted by: John Kilbourne
- Posted on: December 10 2001 11:45 EST
Threaded Messages (7)
- newbie: passing servlet data to a jsp by joseph yi on December 10 2001 13:01 EST
- newbie: passing servlet data to a jsp by John Kilbourne on December 10 2001 14:53 EST
- newbie: passing servlet data to a jsp by Weston Aiken on December 13 2001 11:38 EST
-
newbie: passing servlet data to a jsp by Weston Aiken on December 13 2001 01:16 EST
-
newbie: passing servlet data to a jsp by joseph yi on December 14 2001 01:22 EST
-
arg, why can't i read by joseph yi on December 14 2001 01:36 EST
- arg, why can't i code by joseph yi on December 14 2001 01:38 EST
-
arg, why can't i read by joseph yi on December 14 2001 01:36 EST
-
newbie: passing servlet data to a jsp by joseph yi on December 14 2001 01:22 EST
-
newbie: passing servlet data to a jsp by Weston Aiken on December 13 2001 01:16 EST
- newbie: passing servlet data to a jsp by Weston Aiken on December 13 2001 11:38 EST
-
newbie: passing servlet data to a jsp[ Go to top ]
- Posted by: joseph yi
- Posted on: December 10 2001 13:01 EST
- in response to John Kilbourne
You want to use the <jsp:forward> tag.
First store your result set into the request object:
<%
// first I store the result set...
request.setAttribute("results", rs);
%>
<!-- path to servlet or jsp -->
<jsp:forward page="/servlet/ResultSetHandler" />
Then in your servlet you can extract the result set:
public void doGet(HttpServletRequest request, HttpServletResponse) {
// cast object to ResultSet
ResultSet rs = (ResultSet)request.getAttribute("results");
}
Hope this helps. -
newbie: passing servlet data to a jsp[ Go to top ]
- Posted by: John Kilbourne
- Posted on: December 10 2001 14:53 EST
- in response to John Kilbourne
After more searching, it seems that the two ways to do this are with beans or by setting system attributes, and either using a Collection or using a direct connection to the database. Does anybody have any suggestions on which approach is preferable for a newbie? -
newbie: passing servlet data to a jsp[ Go to top ]
- Posted by: Weston Aiken
- Posted on: December 13 2001 11:38 EST
- in response to John Kilbourne
The response you got from Jospeh Yi is the preferred way. That way, you can extend the life of the request long enough for it to give the ResultSet to your JSP, and then it will be cleaned up with the request it was attached to.
If you need the ResultSet longer than the life of the current request, you can stick it in the HttpSession or the ServletContext. If you do that, you will probably want to use a disconnected ResultSet, and give up the connection. -
newbie: passing servlet data to a jsp[ Go to top ]
- Posted by: Weston Aiken
- Posted on: December 13 2001 13:16 EST
- in response to Weston Aiken
although I think Jospeh Yi, solution is backwards. You need to go from the servlet to the JSP, not JSP to servlet. Just reverse his steps. -
newbie: passing servlet data to a jsp[ Go to top ]
- Posted by: joseph yi
- Posted on: December 14 2001 13:22 EST
- in response to Weston Aiken
hahha well, even i wouldn't do it my way! =p
i gave the solution in context to his question though, since he was getting the result set (it seemed) within a JSP. =p -
arg, why can't i read[ Go to top ]
- Posted by: joseph yi
- Posted on: December 14 2001 13:36 EST
- in response to joseph yi
okay, I was wrong, he wanted to go from servlet to JSP.
To forward the request within a servlet, I usually do it in a pretty inelegant one-liner within the servlet.
public void doPost(HttpServletRequest req, HttpServletResponse) throws IOException, ServletException {
ResultSet rs = null;
StringBuffer output = null;
String sql = "SELECT * FROM tableName"; // your sql
try {
output = new StringBuffer(1024);
// get a result set from stmt, pstmt, whatever
rs = stmt.executeQuery(sql);
while(rs.next) {
output.append(rs.getString(1));
output.append("<hr>");
output.append(rs.getString(2));
// etc.
}
// store object in request object
req.setAttribute("output", output.toString());
} catch (SQLException ex) {
// handle error
}
// here's the one liner
// be careful using RequestDispatcher method
// the slash is relative to your webapp home
getServletConfig().getServletContext().getRequestDispatcher("/resultviewer.jsp").forward(req, res);
}
// in your JSP
<%
String output = (String)request.getAttribute("output");
out.println(output);
%>
// or even
<%= (String)request.getAttribute("output") %>
Storing it in a session is also an alternative, or even the servlet context object, but it all depends on your application's flow and how dynamic it's supposed to be
Hope this helps... Sorry for the bad explanation on the previous post; thanks for pointing it out Weston -
arg, why can't i code[ Go to top ]
- Posted by: joseph yi
- Posted on: December 14 2001 13:38 EST
- in response to joseph yi
well, you get my point... sorry for the shoddy code hahah
but the one liner was the point of my post.