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?
-
Passing information from servlets to jsps (6 messages)
- Posted by: Frank Chan
- Posted on: May 07 2001 12:42 EDT
Threaded Messages (6)
- Passing information from servlets to jsps by Frank Villarreal on May 07 2001 18:04 EDT
- Passing information from servlets to jsps by Paquito on May 15 2001 06:11 EDT
- Re: Passing information from servlets to jsps by Viral Shah on July 07 2008 10:29 EDT
- Passing information from servlets to jsps by Paquito on May 15 2001 06:11 EDT
- Passing information from servlets to jsps by Kishore V.A.V.K. on May 07 2001 18:43 EDT
- Passing information from servlets to jsps by Geoffrey Wiseman on May 07 2001 23:12 EDT
- Passing information from servlets to jsps by sankar subramanian on May 08 2001 04:54 EDT
-
Passing information from servlets to jsps[ Go to top ]
- Posted by: Frank Villarreal
- Posted on: May 07 2001 18:04 EDT
- in response to Frank Chan
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! -
Passing information from servlets to jsps[ Go to top ]
- Posted by: Paquito
- Posted on: May 15 2001 06:11 EDT
- in response to Frank Villarreal
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
-
Re: Passing information from servlets to jsps[ Go to top ]
- Posted by: Viral Shah
- Posted on: July 07 2008 10:29 EDT
- in response to Paquito
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?? -
Passing information from servlets to jsps[ Go to top ]
- Posted by: Kishore V.A.V.K.
- Posted on: May 07 2001 18:43 EDT
- in response to Frank Chan
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.
-
Passing information from servlets to jsps[ Go to top ]
- Posted by: Geoffrey Wiseman
- Posted on: May 07 2001 23:12 EDT
- in response to Frank Chan
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?
-
Passing information from servlets to jsps[ Go to top ]
- Posted by: sankar subramanian
- Posted on: May 08 2001 04:54 EDT
- in response to Frank Chan
Hi,
I feel you can use setAttribute() and getAttribute() methods of HttpServletRequest class to pass information.