File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes How to set a value in request scope in struts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "How to set a value in request scope in struts" Watch "How to set a value in request scope in struts" New topic
Author

How to set a value in request scope in struts

Kanchan Singh
Ranch Hand

Joined: Dec 27, 2007
Posts: 38
Hi all

I am trying to set a value in request scope on a JSp page and trying to get it on Action class.
My code goes as...

at this line it gives me null pointer exception
Rajkamal Pillai
Ranch Hand

Joined: Mar 02, 2005
Posts: 443
    
    1

Hi,

In the ServletRequest object the signature of the getAttribute() method is :-



with the name being the name of the attribute. I did not understand how the code you have in your action class compiled because you have two arguments for the same method. I feel if you give

alone it should work. Correct me if my understanding is not correct.

Cheers,
Raj.
[ September 16, 2008: Message edited by: Raj Kamal ]
Kanchan Singh
Ranch Hand

Joined: Dec 27, 2007
Posts: 38
Hi Raj Kamal
Thanks for reply
you are correct in action calss the code goes as


it gives me null pointer exception..

I tried same thing on a plain JSP-Servlet example..
code is


here also same error..
any reason why???
Tom Rispoli
Ranch Hand

Joined: Aug 29, 2008
Posts: 349
Perhaps I'm just not understanding what you're trying to do but I don't think this can work. Struts executes your JSP after your action class so any attributes set in your request by the JSP will be put there after your action class has already tried (and failed) to get them.

If your trying to set the attribute on the JSP that generates the page that calls your action class, this won't work either since the request that the JSP is setting to will go out of scope once the html is sent to the client and new request will be created when the action is called.
Kanchan Singh
Ranch Hand

Joined: Dec 27, 2007
Posts: 38
Hi Tom
Thanks for reply
what my problem is I have to create a supplier_Reg.jsp page and company_Reg.jsp page.When I fill up the supplier_Reg.jsp I dont want that to be inserted in database on clicking continue what I am doing is I am adding all the supplier info in a list and setting it in request attribute this is done in supplier_action class ."success" of supplier_action class takes to company_Reg.jsp, I fill up all the company information and submit.This jsp page should insert all the date.
Now in company_Reg.jsp I have [list=request.getAttribute("list")] and request.setAttribute("list",list).I want this list object in company_Action class so that I can insert supplier and company information.

Is there other way round of doing it.
Please help
Thanks in advance
kanchan
Tom Rispoli
Ranch Hand

Joined: Aug 29, 2008
Posts: 349
I'm having a little trouble understanding your scenario, so if I make a bad assumption here please correct me.

The user first goes to supplier_Reg.jsp. On the screen they enter in the supplier information and hit continue. Your struts config takes you to supplier_action, where you take the supplier information that you have and store it in the request attribute. You return "success" and your struts configuration then sends you to company_Reg.jsp.

This is where I'm guessin you have your problem. Since your request object is going to go out of scope when company_Reg.jsp finishes. If you don't want to lose your list data you either need to store it in the html that company_Reg.jsp is generating or you have to store the data in your session scope so it will last longer than this one request.

Struts is basically designed to prevent you from having to manually store things in the request scope (there are of course exceptions to this). Have you concidered having company_Reg.jsp populate hidden fields on the screen with the supplier data that I'm assuming was already in your action form? Struts has custom tags for just this purpose.

Otherwise you can manually set the values onto the screen in company_Reg.jsp or you can keep the data in session scope.
Kanchan Singh
Ranch Hand

Joined: Dec 27, 2007
Posts: 38
Thanks Tom for the reply
As you said storing list in session is one of the solution but this would be the last option as I have already many attributes in session.

in supplier_action I put all the information in a list and put the list in request attribute.
on coming to comp_Reg.jsp I get the list from request attribute and set it again . On company_action I am trying to get the list again from request attribute and here is the problem...
Thanks in advance
Rajkamal Pillai
Ranch Hand

Joined: Mar 02, 2005
Posts: 443
    
    1

Hi,

I don't see where the issue lies from what you said but just to narrow it, have you checked whether you are getting the value in the text field 'name'?

Cheers,
Raj.
Tom Rispoli
Ranch Hand

Joined: Aug 29, 2008
Posts: 349
Hi Kanchan, The problem is that the request that you have access to in comp_Reg.jsp, the onee that you are seeting the list into, disappears when the jsp is done. When the call is sent to company_action you get a brand new request object, and I don't thing there is anyway around that. So storing the list in your request object will not get the data to company_action.

I can understand not wanting to put this in session scope, thats usually a bad idea for application data. So I think your only option is to store the list in input fields (probably hidden ones) in the html generated by comp_Reg.jsp.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Originally posted by Tom Rispoli:

I can understand not wanting to put this in session scope, thats usually a bad idea for application data

I disagree with this statement. There is absolutely nothing wrong with judiciously using the HTTPSession object to store data when needed. That's what it's there for.

Many developers have read white papers warning of the dangers of over-using the HTTP Session and pointing out that too much data there can degrade the scalability of the site. OK, that's fine, but many developers have taken this to mean that one should never use the HTTPSession.

First of all, these white papers were written back when Java app servers were fairly young and they sometimes did have problems handling HTTPSession object. Modern versions of app servers are much better at handling the session.

As long as you use it sparingly and take measures to clean up data once it's no longer needed, using the HTTPSession will not degrade the performance of your site, and there's no good reason not to use it.


Merrill
Consultant, Sima Solutions
Tom Rispoli
Ranch Hand

Joined: Aug 29, 2008
Posts: 349
While it is true I was that I was told to avoid using session scope for performance reasons, (and to be honest I've never enountered the sessioin object to be a big memory hog in any application I've done) I have found other dangers in using it to store application data.

If you allow users to use the back and forward buttons or bookmarks you can get some very strange bugs that are hard to recreate due to the fact that the data in thier session isn't appropriate for the page they are on. To prevent this I've found it helpful to reserve the session scope for data that will be valid for the duration of the session. This pretty much limits me to only using it for data about the current user.

However if you limit browser controls maybe that would prevent this kind of problem from occuring with session data.
 
 
subject: How to set a value in request scope in struts