You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Michael Mok <mi...@consultech.net.au> on 2000/08/10 04:32:26 UTC

JSP to Servlet to JSP

Hello

Is it possible in Struts to pass information from JSP to a Servlet and then
from the Servlet to another JSP?

I have look at the struts-example code and would like to implement something
such in the logonAction it forwards the success request to a servlet. This
servlet will be retrieving some records from a database. The when serlvet is
done, it will pass the resultset to another JSP to display the result.

How does the servlet passing information back into the STRUTS framework.

Eg

   public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
        throws IOException, ServletException

	// get rows from database return a resultset
...
            // get a handle to the current session and set a session
attribute for the resultset
	...

            // forward request to the JSP do I send the request to the
framework using
	// return (mapping.findForward("success"));
	???   what do I do here?


TIA


Michael Mok


Re: JSP to Servlet to JSP

Posted by "Craig R. McClanahan" <Cr...@eng.sun.com>.
See below.

Michael Mok wrote:

> Hello
>
> Is it possible in Struts to pass information from JSP to a Servlet and then
> from the Servlet to another JSP?
>
> I have look at the struts-example code and would like to implement something
> such in the logonAction it forwards the success request to a servlet. This
> servlet will be retrieving some records from a database. The when serlvet is
> done, it will pass the resultset to another JSP to display the result.
>
> How does the servlet passing information back into the STRUTS framework.
>
> Eg
>
>    public void doGet(HttpServletRequest request,
>                       HttpServletResponse response)
>         throws IOException, ServletException
>
>         // get rows from database return a resultset
> ...
>             // get a handle to the current session and set a session
> attribute for the resultset
>         ...
>
>             // forward request to the JSP do I send the request to the
> framework using
>         // return (mapping.findForward("success"));
>         ???   what do I do here?
>
> TIA
>
> Michael Mok

What Struts actually uses to "forward" things is RequestDispatcher.forward()
from the servlet API.  When you create a RequestDispatcher, you pass the
context-relative path to the resource you wish to forward to.  For normal Struts
use this is the path to a JSP page, but it could also be the path to a servlet
instead.

To forward stuff from your servlet, you've got two choices:

* Create a dispatcher that maps to a Struts action
  (such as "/logonAction.do") and then call
  the forward() method.

* Create a dispatcher to the ultimate JSP page to
  display the results, and call forward().

The first option sounds like what you want if you need to do something else,
after your servlet runs, before the ultimate response is created by a JSP page.

Craig