Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Pass parameters to servlet

 
Sasikanth Malladi
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I want to parameterize my servlet with typical stuff like the JDBC properties file, the log file location etc.

How do I do this? Should I add stuff to web.xml for this app?
I thought the ServletConfig might be relevant but it doesn't seem to be.

How do I pass in parameters to a servlet so that when the servlet container (Tomcat in my case) loads the servlet, it can read the relevant info?

Thanks,
Sashi
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Listeners seems to me of best use here.
 
Sasikanth Malladi
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Both: web.xml and ServletConfig.

I found some useful info here: http://www.awprofessional.com/articles/article.asp?p=170963&seqNum=5&rl=1

I guess I should've RTFM.
 
Fola Fadairo
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IMHO, ServletContext appears the way to go. In web.xml, add context-param tag, and populate with the desired parameters...
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use servlet init params and then query the parameters from servletconfig , use the values to initialize the log4j and other stuffs.
You can pass initialization parameter to servlets from web.xml.

But context listeners is a better option.
[ May 15, 2007: Message edited by: Rahul Bhattacharjee ]
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to parameterize my servlet with typical stuff like the JDBC properties file, the log file location etc.


An enterprise application would lookup a datasource and use log4j for this. You could use the init param or the context param, which ever is applicable.
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have a lot of config / property file for your application then put the config directory name as init param to the server and then use the servlet's init method to take config files one by one and initialize sub systems respectively.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic