You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Chuck Chopp <ch...@rtfmcsi.com> on 2004/09/02 21:21:24 UTC

forwarding to an action w/o an input JSP

I'm looking for some good solid examples that show how to configure the JSP 
files, struts-config.xml [global forwards, action mappings, form beans] and 
action classes where a JSP may forward/redirect to an action [may end up 
being a chain of actions] before another JSP is finally displayed.  The 2 
books that I've been using for learning Struts are "The Struts Framework: A 
Practical Guide for Java Programmers" by Sue Spielman and "Programming 
Jakarta Struts" by Chuck Cavaness.  Both of these books seem to be lacking 
solid explanations & examples of the the full range of configurations for 
forwards & actions.  I keep getting the feeling that if I could see one good 
comprehensive working example everything would be clear to me about how this 
is supposed to work.

I have a welcome file named "index.jsp".  I'm questioning if it should be 
forwarding to the JSP file "login.jsp" [which I want to protect from direct 
access from the browser] or should it be forwarding to the action named 
"login" [or would it be "login.do"]?  The action named "login" expects to 
use "login.jsp" as its input, and I'm just not certain now whether 
forwarding to a JSP will invoke the action or if forwarding to the action 
will cause its input form to be displayed.


Also, in my web.xml file, I have a url-pattern of "*.do" set in my 
servlet-mapping.  What I'm not clear on is how/why URLs appear back in the 
browser such as "login.do" and "success.do" when the global forwards that my 
actions are using are called by their configured names of "login" and 
"success".  I'm concerned about the browser backward/forward buttons and the 
reload button being clicked & what this might do to navigation within my webapp.


-- 
Chuck Chopp

ChuckChopp (at) rtfmcsi (dot) com http://www.rtfmcsi.com

RTFM Consulting Services Inc.     864 801 2795 voice & voicemail
103 Autumn Hill Road              864 801 2774 fax
Greer, SC  29651

Do not send me unsolicited commercial email.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: forwarding to an action w/o an input JSP

Posted by Erik Weber <er...@mindspring.com>.
Yeah, you can actually have actions that are this simple (no Action 
class or form bean required):

<action path="/bar" forward="/bar.jsp"/>

My logout action mappings typically look like this:

<action path="/actor/logout" type="SessionManagerAction" 
parameter="executeLogout">
<forward name="success" path="/shared/main_index.jsp"/>
</action>

I would think yours would be similar except that your forward path would 
be to a login page instead of a main index page, and you might not be 
using the parameter attribute. The parameter attribute is there because 
I have a single action that handles login as well as logout, so it has 
to be parameterized.

Those books drive me crazy too. (Don't you just love the ones that say 
things like "Here is some code. Now, if this code was *actually meant to 
be used*, we would be handling errors, etc., here . . . "). They are 
written in three months typically, and, in my judgment, usually not 
edited by anyone who could pass Journalism 101.

There are exceptions though. Two I have encountered:

"Thinking in Java" (self published) by Bruce Eckel
"A Programmer's Guide to Java Certification" by Mughal and Rasmussen (my 
favorite Java book of all).

I actually enjoyed reading the Servlet specification as well (it used to 
be only 70 pages long -- then it was truly awesome).

Erik




Chuck Chopp wrote:

> Erik Weber wrote:
>
>> Chuck, I think reading the Servlet specification would help you a 
>> lot! I think it is well edited, and it sure helped me a lot.
>
>
> Thanks for that tip. I will head on over to Sun's web site and read 
> over the specs themselves independent of the actual servelet container 
> implementations.
>
> The part that was getting me was something that was hinted at in an 
> example in one of the books I mentioned, but the example wasn't 
> complete enough to get working code from it.
>
> JSP #1 accepts user credentials and the submit button invokes Action #1.
>
> Action #1 verifies user credentials and either forwards to JSP #2 or 
> stores errors in the request and forwards badk to JSP #1.
>
> JSP #2 can have several functions performed with different submit 
> buttons, most of which forward to other JSPs and one of them invokes a 
> logout action. The part that was giving me fits was getting the logout 
> action mapping done properly since there's no input form associated 
> with it. Once I got it worked out properly, I could have the logout 
> action do its task and then that action, in turn, forwarded back to 
> JSP #1 that prompts for a login.
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: forwarding to an action w/o an input JSP

Posted by Chuck Chopp <ch...@rtfmcsi.com>.
Erik Weber wrote:

> Chuck, I think reading the Servlet specification would help you a lot! I 
> think it is well edited, and it sure helped me a lot.

Thanks for that tip.  I will head on over to Sun's web site and read over 
the specs themselves independent of the actual servelet container 
implementations.

The part that was getting me was something that was hinted at in an example 
in one of the books I mentioned, but the example wasn't complete enough to 
get working code from it.

JSP #1 accepts user credentials and the submit button invokes Action #1.

Action #1 verifies user credentials and either forwards to JSP #2 or stores 
errors in the request and forwards badk to JSP #1.

JSP #2 can have several functions performed with different submit buttons, 
most of which forward to other JSPs and one of them invokes a logout action. 
  The part that was giving me fits was getting the logout action mapping 
done properly since there's no input form associated with it.  Once I got it 
worked out properly, I could have the logout action do its task and then 
that action, in turn, forwarded back to JSP #1 that prompts for a login.


-- 
Chuck Chopp

ChuckChopp (at) rtfmcsi (dot) com http://www.rtfmcsi.com

RTFM Consulting Services Inc.     864 801 2795 voice & voicemail
103 Autumn Hill Road              864 801 2774 fax
Greer, SC  29651

Do not send me unsolicited commercial email.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: forwarding to an action w/o an input JSP

Posted by Erik Weber <er...@mindspring.com>.
Chuck, I think reading the Servlet specification would help you a lot! I 
think it is well edited, and it sure helped me a lot.

Keep in mind that just as a container dispatches requests to Servlets 
according to how the Servlets are mapped in web.xml, the Struts 
controller Servlet dispatches requests to Actions according to how the 
Actions are mapped in struts-config.xml.

The container asks, which Servlet should I invoke? If it is the Struts 
controller Servlet, then the controller Servlet will look at the path 
even more closely and, in turn, ask, which Action should I invoke? An 
Action might even service three or four different (but similar) URLs, so 
the pattern can go on within narrower and narrower contexts.

If your controller Servlet is mapped to *.do in web.xml, then the 
container sees /actor1/usecase1.do and /actor2/usecase3.do as the same 
-- they are headed for the controller Servlet. But the controller 
Servlet sees the difference in these -- it will invoke one Action for 
one and a different Action for the other.



Chuck Chopp wrote:

> I'm looking for some good solid examples that show how to configure 
> the JSP files, struts-config.xml [global forwards, action mappings, 
> form beans] and action classes where a JSP may forward/redirect to an 
> action [may end up being a chain of actions] before another JSP is 
> finally displayed. The 2 books that I've been using for learning 
> Struts are "The Struts Framework: A Practical Guide for Java 
> Programmers" by Sue Spielman and "Programming Jakarta Struts" by Chuck 
> Cavaness. Both of these books seem to be lacking solid explanations & 
> examples of the the full range of configurations for forwards & 
> actions. I keep getting the feeling that if I could see one good 
> comprehensive working example everything would be clear to me about 
> how this is supposed to work.


I think you should check out the examples on Rick Reumann's site:

http://www.reumann.net/do/struts/main

>
> I have a welcome file named "index.jsp". I'm questioning if it should 
> be forwarding to the JSP file "login.jsp" [which I want to protect 
> from direct access from the browser] or should it be forwarding to the 
> action named "login" [or would it be "login.do"]? 


The link would be to "/login.do". The ".do" portion tells the container 
to map to the controller Servlet. The "/login" portion tells the 
controller Servlet which Action to invoke. That action will forward to 
"/login.jsp".

> The action named "login" expects to use "login.jsp" as its input, and 
> I'm just not certain now whether forwarding to a JSP will invoke the 
> action or if forwarding to the action will cause its input form to be 
> displayed.
>
>
> Also, in my web.xml file, I have a url-pattern of "*.do" set in my 
> servlet-mapping. What I'm not clear on is how/why URLs appear back in 
> the browser such as "login.do" and "success.do" when the global 
> forwards that my actions are using are called by their configured 
> names of "login" and "success". I'm concerned about the browser 
> backward/forward buttons and the reload button being clicked & what 
> this might do to navigation within my webapp.


Say the user's browser has requested "/restrictedpage.do". The container 
dispatches the request to your Struts controller Servlet. The controller 
Servlet invokes the "/restrictedpage"Action. The Action determines that 
the user is not logged in, so instead of forwarding to 
"/restrictedpage.jsp" as the user might have expected, you instead 
forward to "/login.jsp". The user's browser is still going to reflect 
"/restrictedpage.do". You serviced the request -- you sent back some 
HTML as a response by forwarding to some JSP. The browser didn't care 
what page on the server the HTML actually came from. If you want the 
browser to reflect "/login.do", you need to issue a redirect in your 
Action, instead of a forward. A redirect is a very small response that, 
instead of including any HTML to display, simply tells the user's 
browser to make a *different* request -- this time to the login page.

Hope that helps,
Erik



>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: forwarding to an action w/o an input JSP

Posted by Chuck Chopp <ch...@rtfmcsi.com>.
Wendy Smoak wrote:

> Have you spent some quality time with struts-example.war yet?  Run that in a
> debugger (JSwat, if you don't already have a favorite,) for an hour, and
> most of the mysteries will be solved.

No, but now that I know where it is I can spend some time with it.  I just 
found it inside the Struts distribution kit.  I wasn't aware that I needed 
the Struts distribution kit at all as it looked like MyEclipse Enterprise 
Workbench was providing all of the Struts files needed for a project. 
Apparently the Struts demo/example webapps don't get distributed with that 
particular Eclipse plug-in and they don't appear to be part of the Tomcat 
distribution, either.


-- 
Chuck Chopp

ChuckChopp (at) rtfmcsi (dot) com http://www.rtfmcsi.com

RTFM Consulting Services Inc.     864 801 2795 voice & voicemail
103 Autumn Hill Road              864 801 2774 fax
Greer, SC  29651

Do not send me unsolicited commercial email.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: forwarding to an action w/o an input JSP

Posted by Wendy Smoak <ja...@wendysmoak.com>.
From: "Chuck Chopp" <ch...@rtfmcsi.com>
> I keep getting the feeling that if I could see one good
> comprehensive working example everything would be clear to me about how
this
> is supposed to work.

Have you spent some quality time with struts-example.war yet?  Run that in a
debugger (JSwat, if you don't already have a favorite,) for an hour, and
most of the mysteries will be solved.

-- 
Wendy Smoak


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: forwarding to an action w/o an input JSP

Posted by Chuck Chopp <ch...@rtfmcsi.com>.
David Durham wrote:

> Chuck Chopp wrote:
> 
>> I have a welcome file named "index.jsp". I'm questioning if it should be
>> forwarding to the JSP file "login.jsp" [which I want to protect from 
>> direct
>> access from the browser] or should it be forwarding to the action named
>> "login" [or would it be "login.do"]? 
> 
> 
> If you're going to protect a JSP from direct access, one good way is to 
> put the JSP inside the WEB-INF and then have an action do a forward to 
> that JSP (presumably after a security check).

I did that.  Specifically, I have a subfolder named "form" under the 
"WEB-INF" folder.  I placed my protected JSP files in there.  My index.jsp 
file [the welcome file] used the "logic:forward" tag to forward to an action 
named "login", and the "login" action uses my protected "login.jsp" and the 
corresponding ActionForm & Action classes.  What I found is that on WinXP 
running Tomcat v4.1.30 & NetWare v6.5 running Tomcat v4.1.28, the webapp 
deployed & ran properly.  However, Tomcat v4.1.24 running on OpenVMS Alpha 
v7.3-1 refuses to compile my JSP files that are in a subfolder under 
"WEB-INF".  This really threw me for a loop and I've been trying to discover 
why this is happening.  Needless to say, when I take code that was already 
working in one Tomcat implementation and it suddenly stops working in 
another implementation of a comparable version [keeping the JDK constant at 
v1.4.2], it leaves me confused.


-- 
Chuck Chopp

ChuckChopp (at) rtfmcsi (dot) com http://www.rtfmcsi.com

RTFM Consulting Services Inc.     864 801 2795 voice & voicemail
103 Autumn Hill Road              864 801 2774 fax
Greer, SC  29651

Do not send me unsolicited commercial email.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: forwarding to an action w/o an input JSP

Posted by David Durham <da...@scott.af.mil>.
Chuck Chopp wrote:

> I'm looking for some good solid examples that show how to configure 
> the JSP
> files, struts-config.xml [global forwards, action mappings, form 
> beans] and
> action classes where a JSP may forward/redirect to an action [may end up
> being a chain of actions] 

Dear God, please let no one help him with this.  Are you talking about 
request dispatching from a JSP to an Action?  That's not a chain of 
Actions.  Thats, well, I don't think there's a name for it -- not a good 
one, anyway.

> before another JSP is finally displayed. The 2
> books that I've been using for learning Struts are "The Struts 
> Framework: A
> Practical Guide for Java Programmers" by Sue Spielman and "Programming
> Jakarta Struts" by Chuck Cavaness. 

I don't know about the first book, but the second is very well written, 
IMHO.

> Both of these books seem to be lacking
> solid explanations & examples of the the full range of configurations for
> forwards & actions. 

Again, I have to humbly disagree with you on this point.

> I keep getting the feeling that if I could see one good
> comprehensive working example everything would be clear to me about 
> how this
> is supposed to work.

There's plenty of them out there.  Struts itself ships with one.

> I have a welcome file named "index.jsp". I'm questioning if it should be
> forwarding to the JSP file "login.jsp" [which I want to protect from 
> direct
> access from the browser] or should it be forwarding to the action named
> "login" [or would it be "login.do"]? 

If you're going to protect a JSP from direct access, one good way is to 
put the JSP inside the WEB-INF and then have an action do a forward to 
that JSP (presumably after a security check).

> The action named "login" expects to
> use "login.jsp" as its input, and I'm just not certain now whether
> forwarding to a JSP will invoke the action or if forwarding to the action
> will cause its input form to be displayed.

The request itself cause an action to be invoked.  That is, a request 
comes in that matches the URI mapping to the ActionServlet.  Hopes 
that's clear (it's crucial.)  The ActionServlet receives the request and 
does something with it (probably calls an Action, passing it the 
request).  Then you're action does some stuff and returns a Forward.  
The ActionServlet then calls a RequestDispatcher's forward method based 
on the Forward that was returned to it.  Basically, your Action returns 
a forward to a JSP (better off not to chain actions), and the JSP writes 
to the response.


> Also, in my web.xml file, I have a url-pattern of "*.do" set in my
> servlet-mapping. 

This is so that requests matching *.do get passed to the ActionServlet.  
The ActionServlet looks at the * part to pass off to the correct action.

> What I'm not clear on is how/why URLs appear back in the
> browser such as "login.do" and "success.do" when the global forwards 
> that my
> actions are using are called by their configured names of "login" and
> "success".

I think you're confusing a "forward" with a redirect.  A forward, in 
this context refers to an internal process in you ServletContainer.  
Namely, a RequestDispatcher is used to pass the request off between 
Servlets.  In this case, we're passing the request off (internally) from 
the model (ActionServlet and Action) to the view(a JSP).  Your URL will 
still be reflective of the original request (something.do), because a 
redirect wasn't returned to your client -- the directing was handled 
internally.  This is a major strength of Struts.

> I'm concerned about the browser backward/forward buttons and the
> reload button being clicked & what this might do to navigation within 
> my webapp.

I don't have too much for you on this.  I'm almost positive that there 
are hooks to check for "duplicate" requests occurring within a timeframe 
(not nessecarily part of struts), and the backward/forward button issue 
has to do with the statelessness (did I type that right?) of HTTP.  That 
is, you have to be ready for any kind of request with security and 
validation in mind.  Struts is not going to do that for you, but it 
contains certain extension points, including plugins, that make it easy 
to consolidate this code.


- Dave

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org