• Post Reply Bookmark Topic Watch Topic
  • New Topic

Servlet Initialization with different scope/context

 
Norman Meister
Ranch Hand
Posts: 52
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I asked the same question on javaprogrammingforums, but did not get any answer.

i want to initialize my servlet based on the scope of my application.

For example, I have multiple admins with their respective email address.

I want to perform action within my servlet based on the given environment.

Would this be the right approach, if I set the admin email addresses in my web.xml as init-param:



Now if I am in the admin 1 environment, I would initialize my servlet with request parameter admin=1 and the servlet should load email address of admin 1 and similarly when in the environment 2, should load the servlet with admin 2.



I could do the same by putting the email address of the respective admin as request param value, but i don't want to the email address to appear in the url.

Does my above very basic workflow make any sense or is there any better way. My servlet programming skills are a bit rusty!

thanks.
 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you trying to have different roles in your application? If so you need to read up on how to do JAAS.

P.S if(admin = "1"){ is probably not what you wanted. You compare reference types using the equals method
 
Eric Cornely
Greenhorn
Posts: 9
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well this should work but as you state something about admins... shouldn't you implement any kind of security ?

In you web.xml you add security constraints (why not a basic authentication or form based), configure the server with a Realm of any kind (often a properties file) and in the servlet use the request.getUserPrincipal() to know which admin is logged.

Then I would prefer a database or any kind of file to store the admin specific values rather than the web.xml but it's up to you...

Let's imagine something like this.getClass().getResourceAsStream("/"+request.getUserPrincipal()+"_config.properties") than you can have any number of properties files in your war corresponding to the admin user name and you validate its right in the container realm.
 
Norman Meister
Ranch Hand
Posts: 52
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Eric for your reply.

Actually, the snippet above of admin was just an example to illustrate my case. Infact, the scenario is just to load a particular configuration file (2 in total) based on a given case. So if case 1 (specified in queryString) is true, configuration file 1 (init-param) should be loaded and so on.

I just wanted to reconfirm my strategy and wanted to know if there is any other better option doing the same.

I know i could handle the same scenario as by including the basic authentication and then request.getUserPrincipal(), but that is out of scope as the authentication part is handled in some other part of the application.

Thanks.

 
Norman Meister
Ranch Hand
Posts: 52
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
E Armitage

it was just a pseudocode i created on the fly.
 
Amit Ghorpade
Bartender
Posts: 2856
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norman Meister wrote:I could do the same by putting the email address of the respective admin as request param value, but i don't want to the email address to appear in the url.

If you are using a post request then the email address won't show up in the request. Hidden form fields are another option.

What you are trying to do can be done by using property files as well. I do not get the purpose of init parameters.
I agree that you don't have to add code for property file reading and its only a small set of vales which would easily fit in the init params, but still.
Another thing to consider is servlet init params won't change once the servlet is initialized and loaded. Not sure if this is your requirement but this makes the value static.

I believe that this is something that the login code should be doing. Set the appropriate admin in the session.

 
Ramy Nady
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello ,

1) The idea of making admin value configurable is good , but who will change this value later ?
If it is only you or one from deployment team , so I prefer you can use <init-param> on web.xml file.

If it will changed by admin / business team , so I prefer to make a separated props or xml file to be editable later.

2) No need to add a security authentication complexity if you are not forced to do that.

3) I agree with Amit , using Post instead of Get , email address won't show up.
But putting such configurable values at request parameters is a bad idea anyway.

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!