It's not a secret anymore!*
The moose likes Struts and the fly likes Struts Action to Servlet Chaining Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Struts Action to Servlet Chaining" Watch "Struts Action to Servlet Chaining" New topic
Author

Struts Action to Servlet Chaining

Joshua Elkino
Ranch Hand

Joined: Jul 24, 2007
Posts: 91
For the love of me i cannot figure out how to perform this.
If i set my <forward name="success" path="/home.do"/> this works perfectly. But once i try to change it so it runs my servlet such as <forward name="success" path="/MyServlet"/> I get an exception with ${pageContext.errorData.throwable.message} being blank. Nothing is being populated in my log files either!
If i try to remove the '/' then i get a missing '/' exception.

What am i doing wrong?

Thanks
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
If you're going to forward to a servlet, you must specify redirect="true".


Merrill
Consultant, Sima Solutions
Joshua Elkino
Ranch Hand

Joined: Jul 24, 2007
Posts: 91
I will try this out. 1 thing that worries me is that i don't want the user to know they visited that servlet.... thus is why i figured i would try to "forward" my request rather than redirect. Also should the redirect rule apply to my other mappings? Will this resolve my "this page has been submitted" warning when you refresh a page?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

The post-redirect-get pattern eliminates that.
Joshua Elkino
Ranch Hand

Joined: Jul 24, 2007
Posts: 91
The <forward name="success" path="/MyServlet" redirect="true"/> forward did not work. I still get the exception page without a stack trace and when i do some simply syserrs in my action page, they all print until my last line return mapping.findForward("success");
Joshua Elkino
Ranch Hand

Joined: Jul 24, 2007
Posts: 91
if i do a return new ActionForward("/MyServlet", true); instead, i see the redirect, but i hit the exception page as well. It looks like its failing before it gets to MyServlet because my first piece of code in either the post or get is an output that i never see in the logs.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

You might be able to use the regular forward mechanism from the action and return null to tell Struts not to do anything more.

It's unclear to me why you need to do any of this, though; is the servlet so complicated/archaic/whatever that it can't be made into an action, or its logic refactored out so it can be used by either?
Joshua Elkino
Ranch Hand

Joined: Jul 24, 2007
Posts: 91
Thanks David. I was trying to separate some responsibility for development purposes, but i guess i could just plug everything in together.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Separating stuff is a good idea, of course--it's just a question of how best to separate it. IMO separating into actions and servlets is a bad idea; the logic itself can be separated into some form of service.

This is one thing Inversion of Control (IoC)/Dependency Injection (DI) is great at--the implementation can be plugged in later, so the action could be written before the real service is implemented. In the meantime the action would use a dummy service. Or vice-versa--the service could be written and tested in isolation then used by an action written later.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Struts Action to Servlet Chaining
 
Similar Threads
any idea please
Godaddy JasperException: Module 'null' not found.
Is It Possible to Invoke An Action By Selecting an Option from a Drop-Down Menu?
Godaddy - Tomcat 5-0-27 JasperException: Module 'null' not found.
Godaddy - Struts Tiles Problems