Reputation: 201
I am using a session scope to store the bean,and i want to project the bean value to the jsp page when needed like this way
request.getSession().setAttribute("bean", bean);
response.sendRedirect("test.jsp");
And in the jsp i am using the below code to get the value on jsp
<% bean1 bean = (bean1) session.getAttribute("bean");
%>
<%= bean.getValue() %>
Instead of using a session scope i want to use a request scope,so can i set my attribute in my servlet in this way
request.setAttribute("bean", bean);
So how can i call it on my jsp can i say
<% bean1 bean = (bean1) request.getAttribute("bean");
But it is showing error.Or instead of using scriplet how can i show my output using JSTL.
Upvotes: 0
Views: 1782
Reputation: 691755
You're not understanding what a redirect is. A redirect is a response you send to the browser so that the browser sends another, new request to the location you redirected to. So, when you call sendRedirect("test.jsp")
, the browser will send a new request to test.jsp. And obviously, all the attributes you have stored in the current request won't be available anymore.
It's impossible, without context, to say if a redirect is something you should do in this case, or if you should instead forward to the JSP. A forward is very different from a redirect, since it only transfers the responsibility of the current request and response to another component. In that case, there would be a unique request, and the JSP could find the attribute set by the servlet in the request.
The only thing I can say is that, in a properly designed MVC application, the JSP is used as a view, and there should never be a direct request to the view. Each request should go through a controller.
Upvotes: 4