Wednesday, June 5, 2013

Can you Explain PortletRequestDispatcher?

In Render methods (doView,doEdit,doHelp) we can write portlet content by writing directly to the PrintWriter object

Ex:

@RenderMode(name = "VIEW")
    public void sayHello(RenderRequest request,    RenderResponse response) throws
    PortletException, IOException {
       
    PrintWriter out = response.getWriter();
    out.println("Hello World! This is implemented using GenericPortlet and "
            + "content coming from sayHello annotation method");
   
    }



But if the markup fragment generated by the portlet is complex, it’s better to pass on the responsibility of content generation to a JSP page, using the PortletRequestDispatcher object.

We can dispatch a request to a JSP, servlet, or HTML page

You can obtain the PortletRequestDispatcher object from the PortletContext
object with either of these methods:

■ getRequestDispatcher(String path)—The path parameter is the path to the
JSP, servlet, or HTML page within the portlet application. The path is relative to
the portlet application’s context root and must begin with a slash (/).


■ getNamedDispatcher(String name)—The name parameter is the name of a
servlet (defined in web.xml) within the portlet application.


The PortletRequestDispatcher defines the following methods to include content
generated by a resource or forward request to a resource:
■ include—This method includes the content generated from the resource in the
portlet response. There are two versions of the include method, one of which is
meant purely for backward compatibility with the Portlet 1.0 specification; it
accepts the RenderRequest and RenderResponse objects as method parameters.


■ forward—This method forwards the request for generating content to
another resource.

Example:


@RenderMode(name="HELP")
public void renderHelp(RenderRequest request, RenderResponse response)throws PortletException, IOException {
getPortletContext().getRequestDispatcher("/WEB-INF/jsp/help.jsp").


include(request,response);
}

No comments:

Post a Comment