This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Servlets and the fly likes Can not get context attribute Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Can not get context attribute" Watch "Can not get context attribute" New topic
Author

Can not get context attribute

sudipto shekhar
Ranch Hand

Joined: Apr 02, 2008
Posts: 818

Hi I am very new to Servlets. Please help.



Here I take the attribute from the field,
set it as an attribute of ServletContext.
The next page of my application tries to retrive this attribute as :



When I execute this there is no error but the value which is printed is null. I don't get it.
I want to take the value from the textfield itself.

The code works well when I set the attribute as



and the value is displayed.

Where am I wrong?





Regards, Sud.
SCJP 5 ScjpFAQ JLS
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60755
    
  65

In your first servlet you are creating the form and trying to get the value of a form field before it has even been sent to the browser. How is the user supposed to fill in the field?

You should be trying to obtain the value of the form field in the servlet that is the result of submitting the form after it has been displayed in the browser.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
sudipto shekhar
Ranch Hand

Joined: Apr 02, 2008
Posts: 818

Yes, I want the value from the field of that page.
I want to set the value,which I received from the field,as context attribute and want to retrieve that value in the next page and display it.

I too got a hint about this problem but could not come up with an idea how to solve this silly problem.
Sorry to waste your time,but can you suggest my something about what I want to do?


sudipto shekhar
Ranch Hand

Joined: Apr 02, 2008
Posts: 818

Originally posted by Bear Bibeault:


You should be trying to obtain the value of the form field in the servlet that is the result of submitting the form after it has been displayed in the browser.


I did not get it clearly.
sudipto shekhar
Ranch Hand

Joined: Apr 02, 2008
Posts: 818

Ok I got what you say.
But my query is cant I set the context attribute with the retrieved value?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60755
    
  65

You can't retrieve the value until it's submitted. Simply writing the form to the response doesn't create the form. The response must be sent to the browser, and then when the user submits the form, you can retrieve the value in another servlet that's invoked as the action of the form.
sudipto shekhar
Ranch Hand

Joined: Apr 02, 2008
Posts: 818

Thank you.
I got that.

 
Consider Paul's rocket mass heater.
 
subject: Can not get context attribute
 
Similar Threads
HFS page 171
Cannot get HttpSessionBindingListener method valueBound to trigger
Problems with Tomcat
attributes and listeners HF p171-176
getContextPath not working