pass an object from servlet to jsp

Discussions

Web tier: servlets, JSP, Web frameworks: pass an object from servlet to jsp

  1. pass an object from servlet to jsp (6 messages)

    hi all,
    i've an object (like this for example :

    public class ResponseObj {
    public String string1;
    public String string2;
    public String string3;
    }

    how can i send this object to the jsp ? i do have a doGet method that send some parameters to the servlet and then i need to send the ResponseObj to the jsp.

    regards,
    elise

    Threaded Messages (6)

  2. pass an object from servlet to jsp[ Go to top ]

    Use the HttpServletRequest object in the servlet - set an attribute on the request object containing your object.

    Then on the JSP simply read that attribute into an object and use.

  3. pass an object from servlet to jsp[ Go to top ]

    You can also send it via the sesison...

    RequestObj requestObj = new RequestObj();

    HttpSession session = request.getSession(true);
    /*
    getSession with a true parameter means get me the client
       session, if it doesn't exist create a new one.
    */
    session.setAttribute("reqObj", requestObj);

    // then forward to jsp
  4. pass an object from servlet to jsp[ Go to top ]

    You can also send it via the sesison...

    RequestObj requestObj = new RequestObj();

    HttpSession session = request.getSession(true);
    /*
    getSession with a true parameter means get me the client
       session, if it doesn't exist create a new one.
    */
    session.setAttribute("reqObj", requestObj);

    // then forward to jsp
  5. pass an object from servlet to jsp[ Go to top ]

    thanks, that's what i'm trying to use.
    sorry to ask you again one more question but is the request of request.setAttribute the same one that the
    public void doGet(HttpServletRequest request, HttpServletResponse
    response) throws ServletException, IOException {} ?
    i don't think so because i got a error like this "the method setAttributes
    is not defined.

    i fact my question is : why can't i do this :

    public void doGet(HttpServletRequest request, HttpServletResponse
    response) throws ServletException, IOException {

            //...... blah blah
            // many code
            SearchResult mySearchResult = new SearchResult();

             if ( submitType.equals("apply") ) {
                    Myconnectionclass.updateRecord(sYear,sCurrency, myResp);
    //the query that updates the record in the database

                    request.setAttribute("responseObj",myResp); // here is
    the error
     
    getServletContext().getRequestDispatch("/myWebApp/ConnClass2.jsp").forward(request,response);
     // and here too
            }

    }

    as I've seen in java specifications, setAttribute method is part of
    javax.servlet.ServletRequest and my request here in the code is part of
    javax.servlet.http.HttpServletRequest.

    so what's the mess ? should i "transform" my request ?
  6. pass an object from servlet to jsp[ Go to top ]

    I got an error on the RequestDispatcher line in your code where getRequestDispatch should be getRequestDispatcher.
    If request.setAttribute() doesn't work for you, I'd check the servlet version you're compiling against...setAttribute was added in Servlet 2.1.

    // This code compiles OK, but wasn't written to do anything :)
    import javax.servlet.http.*;
    import javax.servlet.*;
    import java.io.*;

    public class MyTestServlet extends HttpServlet {
        public void doGet(HttpServletRequest request,
        HttpServletResponse response)
        throws ServletException, IOException {

       if ( true ) {
         request.setAttribute("forward","false");
          getServletContext().getRequestDispatcher("/myWebApp/ConnClass2.jsp").forward(request,response);
            
         }

      }
    }
  7. pass an object from servlet to jsp[ Go to top ]

    okay thank you.
    i've found the problem, i was using servlet API 2.1.1 and setAttribute is only sine the 2.2