Passing information from servlets to jsps

Discussions

Web tier: servlets, JSP, Web frameworks: Passing information from servlets to jsps

  1. Passing information from servlets to jsps (6 messages)

    I'm developing an application which requires communication between servlets and jsps.

    What is the best way to pass information

    a ) from servlet to servlet
    b ) from servlet to jsp
    c ) from jsp to servlet

    Should I pass info using the session or request? When should I use getParameter, getAttribute and getValue?

    Can someone get back to me and explain which way is better?
  2. Adding to the first question...

    Specifically, how would you pass a JavaBean (not enterprise) from a servlet to a JSP? How do you specify/set scope for the bean from within the servlet (or is that an irrelevant question)? I know that in the JSP, scope is a parameter of the <use:bean> tag. Thanks for any help in advance!
  3. For passing JBeans from Servlet to JSP I'm using the Request object. First I store all info in JBean, then I put it in HttpRequest Object and then I call JSP with RequestDispatcher Object:

    theBean = DataBean.getBean(request,beanClassName);
    request.setAttribute("myBean"),theBean);
    request.getRequestDispatcher("/myjsp.jsp").forward(request, response);

    I hope this helps you.

    Regards,

    alx
  4. I am trying to display a sensible message on the browser when an error occurs, I do this in my servlet, finally { if (user_row_inserted == true){ RequestDispatcher view = request.getRequestDispatcher("Success.jsp"); view.forward(request, response); } else if (user_row_inserted == false){ String[] errorlist = {exception.getMessage()}; request.setAttribute("button2",errorlist); RequestDispatcher view = request.getRequestDispatcher("Error.jsp"); view.forward(request, response); } else if (errmsg != null){ String[] errorlist = {exception.getMessage()}; request.setAttribute("button2",errorlist); RequestDispatcher view = request.getRequestDispatcher("Error.jsp"); view.forward(request, response); } else { RequestDispatcher view = request.getRequestDispatcher("OtherErrors.jsp"); view.forward(request, response); } } And this is what my JSP looks like, <%String[] s =(String [])request.getAttribute("button2");%> <%=s%> LoadUserData reported error: on the server.

    Load User Data


    Load User From SAP :

    Load User From External Data Source:

    Could you tell me what is it that I am doing wrong here??
  5. Hi

      Informatin that is to be passed from Servlet to JSP or reverse depends upon the type of information. Like whether it is passive data or not.

      If the data is for that request only then best way is to use setAttribute and getAttribute methods of the HttpServletRequest class.

     If the data is for the session then the best way is to store in the session.

  6. If you're passing information in the context of a single request, you want to pass it in request scope. If you're passing information that has to persist in a session but for longer than a single request, then you'd use session scope.

    In a JSP, your best bet is <jsp:usebean />, although you can access the request and session directly. In a servlet, you'll be accessing those objects directly (e.g.: request.setAttribute( ); )

    As far as setting the scope within the servlet, it's done by the object you use. If you set attributes in the session object, you'll be using session.setAttribute( ), whereas in the request, you'll be using request.setAttribute( ). That's assuming you have the appropriate variables defined.

    Does that answer the question?

  7. Hi,
    I feel you can use setAttribute() and getAttribute() methods of HttpServletRequest class to pass information.