File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSP and the fly likes jsp and servlets with jdbc Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "jsp and servlets with jdbc" Watch "jsp and servlets with jdbc" New topic

jsp and servlets with jdbc

mark babcock

Joined: Jun 26, 2002
Posts: 14
I have some tutorials I am working through from the Sum core servlets and javaserver pages book. I have a question about using JDBC with JSP. All the examples in the book show you how to make a connection to a database using serlvets and display the information back to the browser using the println method. However, is there a way to return the result to a jsp page? What i'm trying to do is build an application that uses MVC. Have my servlet make the connection then have the result set passed back to the jsp page. How can I do this?
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63854

After your servlet collects the info it needs from the database, it can tack it onto the request as an attribute and forward to a JSP page. The JSP page retrieves the data to display from the request.
Be sure to forward rather than redirect since a forward maintains the same request context.

[Asking smart questions] [About Bear] [Books by Bear]
Andy Bowes
Ranch Hand

Joined: Jan 14, 2003
Posts: 171
If you are really using MVC then the database access should be encapsulated in the 'Model' tier rather than in the 'Control' tier that is implemented by the servlet. The control servlet should invoke methods on the model tier and load the information onto the current request as specified by Bear so that the JSP/View tier can handle the display of the data.

Andy Bowes<br />SCJP, SCWCD<br />I like deadlines, I love the whoosing noise they make as they go flying past - Douglas Adams
I agree. Here's the link:
subject: jsp and servlets with jdbc
It's not a secret anymore!