trying to put objects in the request object using struts

N

Nathan Given

Hello Everyone,

I am a newbie struts programmer... I have programmed jsps and
servlets without struts before using the Front Controller pattern, but
now I am trying out something new.

On my login page I have a checkbox, if it is checked, I want to print
out some extra information on the logged in page; if it is not
checked, I don't want to print out that extra info.

The problem I am having is that I am using the request object to store
the information ( like this: request.setAttribute("extra_info","Hello
New User") )

But, on the page, when I use request.getAttribute("extra_info") it
comes out null.

Here are some code snippets...


----LoginAction class-------

execute(ActionMapping actionMapping, ActionForm actionForm,
HttpServletRequest request, HttpServletResponse response) throws
Exception {

....
//if the passwords match...

//get the session
HttpSession session = request.getSession(true);
//set the user object to the session
session.setAttribute("user", user);

if(((LoginForm)actionForm).getCheckzip()){

//run the zip code stuff
...
request.setAttribute("checkzip","yes");

} else {

System.out.println("LoginAction: setting checkzip to no");
request.setAttribute("checkzip","no");

}//if else

return actionMapping.findForward(target);

} //end execute method
------------ end login action class --------------

As you can see, I set "checkzip" with the string value of "yes" using
request.setAttribute() if the box is checked, and "no" if the the
check box is not checked.


---------- struts-config.xml ---------------


<struts-config>
<form-beans>
<form-bean name="loginForm"
type="edu.byu.cid.ssp.bank.LoginForm" />
</form-beans>

<action-mappings>
<action path="/login" type="edu.byu.cid.ssp.bank.LoginAction"
name="loginForm" input="/login.jsp" validate="true" scope="request">
<forward name="success" path="/welcome.jsp" redirect="true"
/>
<forward name="failure" path="/login.jsp" />
</action>
</action-mappings>

</struts-config>


-------- end struts-config.xml -------------


So, on successful login, the user should be redirected to welcome.jsp.
On welcome.jsp I have the following...


------------ welcome.jsp ----------------

<%= request.getAttribute("checkzip") %>

----------- end snippet -------------------

that always comes out null, always. It should at least come out "yes"
or "no" right?


I've done this using the Front Controller pattern, and the last couple
of lines line in my Controller.java file was

RequestDispatcher dispatcher =
request.getRequestDispatcher("/welcome.jsp");
dispatcher.forward(request, response);


Thanks!
 
R

Ryan Stewart

*snip*
----LoginAction class-------

execute(ActionMapping actionMapping, ActionForm actionForm,
HttpServletRequest request, HttpServletResponse response) throws
Exception {

...
//if the passwords match...

//get the session
HttpSession session = request.getSession(true);
//set the user object to the session
session.setAttribute("user", user);

if(((LoginForm)actionForm).getCheckzip()){

//run the zip code stuff
...
request.setAttribute("checkzip","yes");

} else {

System.out.println("LoginAction: setting checkzip to no");
request.setAttribute("checkzip","no");

}//if else

return actionMapping.findForward(target);

} //end execute method
------------ end login action class --------------

As you can see, I set "checkzip" with the string value of "yes" using
request.setAttribute() if the box is checked, and "no" if the the
check box is not checked.


---------- struts-config.xml ---------------


<struts-config>
<form-beans>
<form-bean name="loginForm"
type="edu.byu.cid.ssp.bank.LoginForm" />
</form-beans>

<action-mappings>
<action path="/login" type="edu.byu.cid.ssp.bank.LoginAction"
name="loginForm" input="/login.jsp" validate="true" scope="request">
<forward name="success" path="/welcome.jsp" redirect="true"
/>
<forward name="failure" path="/login.jsp" />
</action>
</action-mappings>

</struts-config>


-------- end struts-config.xml -------------


So, on successful login, the user should be redirected to welcome.jsp.

Assuming you set "target" to "success" in your action, yes.
On welcome.jsp I have the following...


------------ welcome.jsp ----------------

<%= request.getAttribute("checkzip") %>

----------- end snippet -------------------

that always comes out null, always. It should at least come out "yes"
or "no" right?
Wrong. Description of the redirect attribute of the forward element:

redirect Set to "true" if a redirect instruction should be
issued to
the user-agent so that a new request is issued for this
forward's resource. If true,
RequestDispatcher.Redirect is
called. If "false", RequestDispatcher.forward is called
instead.
[false]

As you should be aware if you use JSPs often, a redirect does not preserve
the request object. A forward does. Why did you put that redirect on the
forward in the first place?
 
N

Nathan Given

On welcome.jsp I have the following...
------------ welcome.jsp ----------------

<%= request.getAttribute("checkzip") %>

----------- end snippet -------------------

that always comes out null, always. It should at least come out "yes"
or "no" right?
Wrong. Description of the redirect attribute of the forward element:

redirect Set to "true" if a redirect instruction should be
issued to
the user-agent so that a new request is issued for this
forward's resource. If true,
RequestDispatcher.Redirect is
called. If "false", RequestDispatcher.forward is called
instead.
[false]

As you should be aware if you use JSPs often, a redirect does not preserve
the request object. A forward does. Why did you put that redirect on the
forward in the first place?


Thank you for your response. It all makes sense now. (And, yes, I
should know that a redirect does not preserve the request object...
but I didn't... I've never read any books about JSPs and such... are
there any good online resources that you know of?)

Here is the short answer to your question (Why did you put that
redirect on the forward in the first place?): I am programming in a
group of three people. The person who programmed the above code copied
that particular line from an example in a book. I'm not sure whether
he knew what the redirect="true" was doing.

Where did you get that documentation? I would like to be able to
troubleshoot my own problems :)

Thanks!
 
R

Ryan Stewart

Nathan Given said:
On welcome.jsp I have the following...


------------ welcome.jsp ----------------

<%= request.getAttribute("checkzip") %>

----------- end snippet -------------------

that always comes out null, always. It should at least come out "yes"
or "no" right?
Wrong. Description of the redirect attribute of the forward element:

redirect Set to "true" if a redirect instruction should be
issued to
the user-agent so that a new request is issued for this
forward's resource. If true,
RequestDispatcher.Redirect is
called. If "false", RequestDispatcher.forward is called
instead.
[false]

As you should be aware if you use JSPs often, a redirect does not preserve
the request object. A forward does. Why did you put that redirect on the
forward in the first place?


Thank you for your response. It all makes sense now. (And, yes, I
should know that a redirect does not preserve the request object...
but I didn't... I've never read any books about JSPs and such... are
there any good online resources that you know of?)
Unfortunately, I've never found a good online JSP resource. I've never even
read a book on the subject myself. I'd like to find a good book on webapp
design.
Here is the short answer to your question (Why did you put that
redirect on the forward in the first place?): I am programming in a
group of three people. The person who programmed the above code copied
that particular line from an example in a book. I'm not sure whether
he knew what the redirect="true" was doing.
I suspected it was a copy and paste artifact. :)
Where did you get that documentation? I would like to be able to
troubleshoot my own problems :)
That snippet came from the struts-config DTD, here:
http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd

I got the link from the Struts documentation, which is available online or
as a .war file with the download. That particular link I found in the User
Guide in section 4.8, "Writing Action Mappings". The User Guide and
Developer Guides, which have the API specs and other good stuff, are
available as a link under "Nightly Build" on the left nav bar. Or click on
"Learning" near the top of the bar, and there will be a link near the top of
the document that opens.
No problem.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,782
Latest member
ThomasGex

Latest Threads

Top