This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Servlets and the fly likes Pass parameters to servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Pass parameters to servlet" Watch "Pass parameters to servlet" New topic
Author

Pass parameters to servlet

Sasikanth Malladi
Ranch Hand

Joined: Nov 04, 2000
Posts: 175
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

Joined: Apr 13, 2003
Posts: 1088
Listeners seems to me of best use here.
Sasikanth Malladi
Ranch Hand

Joined: Nov 04, 2000
Posts: 175
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

Joined: Feb 16, 2004
Posts: 35
IMHO, ServletContext appears the way to go. In web.xml, add context-param tag, and populate with the desired parameters...
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
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 ]

Rahul Bhattacharjee
LinkedIn - Blog
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

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.


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Pass parameters to servlet
 
Similar Threads
finding mimetype using javascript
config parameters for JSP
passing initial parameters to jsp
Servlet Called From Javascript
Netbean help