aspose file tools*
The moose likes JSP and the fly likes How to retrieve data from database, using servlet or a jsp? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "How to retrieve data from database, using servlet or a jsp?" Watch "How to retrieve data from database, using servlet or a jsp?" New topic
Author

How to retrieve data from database, using servlet or a jsp?

chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 806

Hi everybody, I am in a dilemma, I am developing an application for a small company, I am using connection pooling to connect to database. I know how to connect to database using from a jsp page. But that is not encouraged, I must use a servlet and then that servlet calls a bean or model, then that model returns a ResultSet object, then this object is added to request and sent to the jsp page.

But I dont know how to do this using a servlet. Can I do this task with a jsp? Is it appreciated?

Thank you all in advance.


Love all, trust a few, do wrong to none.
chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 806

This is my context.xml under META-INF folder.



pool.jsp
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61753
    
  67

It is highly discouraged to do anything like that in a JSP.

In fact, in a properly structured web application (following MVC) even a servlet is too close to the UI to be doing DB access, but it's better than doing it in the JSP.

What part are you having problems with?

Once you have your resultset, copy the data out of it immediately into other Java constructs and close the resultset and other DB elements. Then pass the Java structures to the JSP for view processing (using JSTL and EL and not java code).


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 806

Bear Bibeault wrote:It is highly discouraged to do anything like that in a JSP.
What part are you having problems with?


Thank you Bear for the help. I actually developed an MVC architecture as you said. I have a controller which takes the arguments and sends the arguments to a model. In return the model returns a ResultSet object. Now I am adding this ResultSet object to request using request.setAttribute(String) menthod.

The problem is I am not using scriptlets. If I use scriptlets I would not even raise this topic. I am using JSTL. Once you said that no body uses scriptles, so I am learning to code using JSTL.

Now in requestScope I have the ResultSet object. How should I use that object to acces the data in it?

Thank you in advance.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61753
    
  67

anusha chaitanya wrote:In return the model returns a ResultSet object. Now I am adding this ResultSet object to request using request.setAttribute(String) menthod.

This is a really really bad idea. The resultsets should never leave the model. Copy the data to collections or beans or some other appropriate Java constructs.

Now in requestScope I have the ResultSet object. How should I use that object to acces the data in it?

As I said above, the resultset should never be sent to the JSP. Rather, send structures that are easy to deal with using JSTL and EL.
chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 806

Can I do like this....in the model I will create a 2 dimensional array, then copy the contents of the ResultSet to that array, then pass that array to the servlet, from there to jsp. can i do this?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61753
    
  67

Sure, but a 2D array isn't always the best way to contain data (unless you are just blatting it out into a <table>).

It depends upon the nature of the data.

For example, let's think about a recipe sharing application. If the point of the operation is to get a list of recipes, capturing the recipe information in a Recipe bean that's put into a List or array would be best.

Think of how you will display the data on the JSP using JSTL and EL, and figure out what sort of collection, or beans, or collection of beans makes that easiest.
chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 806

Bear Bibeault wrote:Sure, but a 2D array isn't always the best way to contain data (unless you are just blatting it out into a <table>).


Thank you very much Bear, thanks a lot. I will try it out with a 2D array, later I will go with a bean or something else. I don't need to play with the data, I just need to display it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to retrieve data from database, using servlet or a jsp?