aspose file tools*
The moose likes Struts and the fly likes Is this possible? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Is this possible?" Watch "Is this possible?" New topic
Author

Is this possible?

A Harry
Ranch Hand

Joined: Jan 23, 2002
Posts: 124
From a servlet I can forward to a JSP using this code -

public class UploadServlet extends HttpServlet
{
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
RequestDispatcher rd = getServletContext().getRequestDispatcher ( "/Main.jsp" ) ;
dispatcher.forward(request, response);
}
}

BUT how can I forward to a Struts action class? i.e the path="/Main" bit in my struts-config.xml below so that MainAction is called?

<actionpath="/Main"
type="uk.gov.ea.prd.action.MainAction"
name="formMenu" scope="request">
<forward name="logout" path="/PRD.do"/>
<forward name="success" path="/main.jsp"/>
</action>

Is this possible? - tried all sorts of combinations but it only seems to be able to forward to a JSP PAge?

many thanks

harry
A Harry
Ranch Hand

Joined: Jan 23, 2002
Posts: 124
dispatcher = getServletContext().getRequestDispatcher ( "/Main.do" );

seems to work but can you get one of the forward's i.e "logout"?
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
It's called "action chaining". It's certainly possible, and this link explains how. You will also note from the link that it's not recommended. If you have logic in one action that another needs to use, it's better to extract the logic to a utility class or business delegate where both actions can access it than to chain actions together.
[ March 24, 2006: Message edited by: Merrill Higginson ]

Merrill
Consultant, Sima Solutions
A Harry
Ranch Hand

Joined: Jan 23, 2002
Posts: 124
Thanks for replying Merrill, I'm trying to do this from a Servlet though - is it still possible to somehow get the "logout" forward as specified in -

<action path="/Main"
type="uk.gov.ea.prd.action.MainAction"
name="formMenu" scope="request">
<forward name="logout" path="/PRD.do"/>
<forward name="success" path="/main.jsp"/>
</action>

What also complicates this is that the servlet will be used to handle file uploading i.e. using "multipart/form-data" so when the servlet does a forward using something like "dispatcher.forward(request, response);" the receiving action generates the warning "One of the getParameter family of methods called after reading..." which I believe there is no way around as struts uses the getParameter() internally & you can't stop it?

I was hoping (when I can get hold of a forward that is) to use the redirect="true" attribute to generate a new request that not multi-part!

Hope this makes some sense mate, this has been driving me crazy for weeks!

Cheers

Harry

P.S I should have said to start with I'm using Struts 1.2.7, Weblogic 8.1 (sp3) - no idea if that makes any difference though?
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
If you want to call a Struts action from a Servlet, I suggest you use response.sendRedirect() rather than requestDispatcher.forward(). The forward method redirects to a physical file, while sendRedirect instructs the browser to go to a different URL. A sendRedirect is processed exactly as if the user had entered the URL from a browser. The one thing you need to be aware of, though, is that with a sendRedirect, the HttpServletRequest object is not passed along, so any information you want to be passed to the URL must be passed in the form of parameters in the query string.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Is this possible?