Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Setting context parameter in a servlet

 
Mohammed Uddin
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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

Thanks
 
Devaka Cooray
ExamLab Creator
Marshal
Pie
Posts: 4392
249
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64826
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mohammed Uddin
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64826
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64826
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Thanks for the update.

I shall look into this.
 
padmavathi gangasani
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic