Web tier: servlets, JSP, Web frameworks: newbie: passing servlet data to a jsp

  1. newbie: passing servlet data to a jsp (7 messages)

    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:


    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>

    Threaded Messages (7)

  2. newbie: passing servlet data to a jsp[ Go to top ]

    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.
  3. 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?
  4. 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.
  5. newbie: passing servlet data to a jsp[ Go to top ]

    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.
  6. newbie: passing servlet data to a jsp[ Go to top ]

    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
  7. arg, why can't i read[ Go to top ]

    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) {
                // 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");

    // 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
  8. arg, why can't i code[ Go to top ]

    well, you get my point... sorry for the shoddy code hahah

    but the one liner was the point of my post.