Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

database connection from servlet

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
sylvia greene
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Jeroen
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Do Re Mi Fa So La Tiny Ad
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic