aspose file tools*
The moose likes Servlets and the fly likes Setting context parameter in a servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Setting context parameter in a servlet" Watch "Setting context parameter in a servlet" New topic
Author

Setting context parameter in a servlet

Mohammed Uddin
Ranch Hand

Joined: Mar 04, 2012
Posts: 31
Hi,

How can one set the value of a context parameter from a servlet?

Thanks
Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3230
    
  49

If you meant to change the value, you can't - they are read-only. In JavaEE 6, you may set new parameters before the context is initialized. There is less chance you will ever need use this method if you wisely design your application.


Author of ExamLab ExamLab - a free SCJP / OCPJP exam simulator
What would SCJP exam questions look like? -- Home -- Twitter -- How to Ask a Question
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61662
    
  67

Red flag alert!

Why do you think you need to do this? Chances are, there's a better way to do what you are trying to accomplish.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Mohammed Uddin
Ranch Hand

Joined: Mar 04, 2012
Posts: 31
Hi,

I am trying to implement a very basic login and logout system.

So, when a user first visits my template website, the contect login state (a boolean variable) is set to false forcing the user to login - after the user logs in i would like to change the context parameter bool value to something like true - I was not aware that this is in fact a very bad thing.

Can someone please suggest how this can be done or rather are there any better practices in doing this?

Thanks
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61662
    
  67

First: use container-managed security instead of rolling your own.

If for some reason, you cannot, then you are definitely heading in the wrong direction.

Think about it for a minute. A context parameter (in addition to be read-only) is shared by all resources in the web app. So do you really think it would be appropriate for storing information for a single user? What if there's a second user? A four hundred and fifty first user? A ten thousandth?

So, again put on your thinking cap: where would be the appropriate place to store information that's pertinent for only one user?
Mohammed Uddin
Ranch Hand

Joined: Mar 04, 2012
Posts: 31
Hi,

I am new to web technology and it's concepts.

Can you suggest how I can do this please?

Thanks.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61662
    
  67

If this is for a real product, then you are not in a position to create your own security system. Learn about container-managed security and use that.

If this is just a play app for learning concepts, you need to learn about the session.
Mohammed Uddin
Ranch Hand

Joined: Mar 04, 2012
Posts: 31
Hi,

Thanks for the update.

I shall look into this.
padmavathi gangasani
Greenhorn

Joined: Oct 10, 2012
Posts: 4
Both servlet context and servlet config will be set in deployment descriptor.Servlet Config is per servlet where as servlet context param value will be shared by total webapp.

So,if you want to chane param value,you need to update in DD and need to restart the server.In real time,it is applicable for storing database values.And we cant use context params per request.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Setting context parameter in a servlet