Redirecting from the doView method in a portlet
Question
I'm using Websphere portal 6.0 and I'm wondering if there's a way in which I can tell the server which page to render from the doView method. I know I can do it from the processAction method but unfortunately the semantics of the problem don't allow it.
Thank you for your help
Solution
As I understand, you want to decide which JSP/HTML page you are going to show to the user.
In that case, this is what you need to do.
public void doView(RenderRequest req, RenderResponse res) throws IOException,
PortletException {
PortletRequestDispatcher prd =
getPortletContext().getRequestDispatcher("/WEB-INF/jsp/view.jsp");
prd.include(req, res);
}
You can decide each time which jsp you want to obtain the request dispatcher for.
OTHER TIPS
I doubt it is possible to send a redirect in doView(). Two reasons for that:
- For performance and various other reasons, the portal may call doView() after the headers of portal's HTTP response were generated and sent out - thus too late to issue a redirect.
- It could be pretty "evil" to be able to do that - a portlet's doView() can be called anytime by the portal, without user's interaction with that portlet. Thus a portlet could do the redirect after a random page refresh, or interaction with another portlet.
In general, I'd say if portlet needs to do a redirect in doView, then it may require a redesign. Perhaps try to describe your problem in more details.