• 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

Initialising Database connections with JSP

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm new to JSP but quite familiar with Servlets. I'm trying to understand how you would go about initialising a connection to a Database with JSP.
With a Servlet I would create a module levelConnection object in the init() method and then use this connection in the service() method.
How would I do this with JSP?
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could put the code in jspInit() although this makes the JSP more like a servlet and less like a web presentation page. Scriplets are another possibility although this may not necessarily a good idea from a design view.
 
Stephen Tallamy
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What would the best way to do this (from a design point of view) be then?
 
Tom Arons
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your idea of putting it in a servlet seems like a good design idea.
 
Ranch Hand
Posts: 2166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ranchers.
It just me thinking loud . Understand this more as a request to the experts to comment on this:
In a fully fledged J2EE application the initialization of a database connection belongs in some sort of a data access layer and not in the presentation layer.
J2EE-architecture should support the easy substituition of a web-client frontend with other frontends such like a Swing-App or a Wap-Interface. So the backend data access logic should be very loosely coupled with the frontend.
In a pure webApp (where the probability of the need to support other client types in the future change path is very low) you might put it into a bean queried from the jspInit() or in the Controller servlet of a ModelViewController architecture.
It seems to be not very intelligent to put it in a taglib, because you cannot call the taglib code from jspInit(). So you can`t use the session life-cycle stuff and the connection is initialized everytime the JSP is invoked.
Axel
[ January 09, 2002: Message edited by: Axel Janssen ]
 
Ranch Hand
Posts: 267
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How about creating a database bean which handles the connection & then use <useBean> tag in the jsp.

Originally posted by Stephen Tallamy:
I'm new to JSP but quite familiar with Servlets. I'm trying to understand how you would go about initialising a connection to a Database with JSP.
With a Servlet I would create a module levelConnection object in the init() method and then use this connection in the service() method.
How would I do this with JSP?

 
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
I agree with Roopa. You want to seperate any database connection etc, from the presentation layer. the use of a JavaBean, EJB or other Servlet is the best way to do this. If using another Servlet, the purpose of that Servlet should be database access only, and no other services.
Mark
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic