This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Servlets and the fly likes database connection from servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "database connection from servlet" Watch "database connection from servlet" New topic
Author

database connection from servlet

sylvia greene
Ranch Hand

Joined: Jun 23, 2004
Posts: 40
I am using intializaton parameters in web.xml to make connection to database.

In HelloWorldServlet I do the following


Now i have made another servlet say AccountServlet which uses the same initialization parameters. Do i need to specify the same paramters within init-param in the AccountServlet entry in web.xml or is there a way to use the parameters i defined in HelloWorldServlet.
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
parameters defined for one servlet are not available for another.
You can also define parameters in the servletContext which are visible to the entire web application.

Short sample from our web.xml:


context-param gets added to the servletContext, init-param (per servlet) to the servletConfig for that particular servlet.
The order of these sections is set in stone!
[ July 16, 2004: Message edited by: Jeroen Wenting ]

42
sylvia greene
Ranch Hand

Joined: Jun 23, 2004
Posts: 40
thanks Jeroen
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

One other thing to keep in mind. Now you are doing a test I assume, based on the name "HelloWorldServlet".

But in the future, you will want to remove Database work inside the actual Servlet code, you would want to seperate that out into another Java class, not a Servlet. The Servlet should only handle getting the Request, the parameters, the submitted request variables, and returning the response. All other business logic should be refactored outside of the actual Servlet class. This is a much cleaner architecture, and making sure that the responsibility of the Servlet, is handling the response and request objects.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
 
Consider Paul's rocket mass heater.
 
subject: database connection from servlet
 
Similar Threads
load on startup causing problems
Web application
Servlet Context Object?
Why this code to get Init Param in JSP not work?
servletConfig!!!