Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
chaitanya karthikk
Ranch Hand
Posts: 806
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
chaitanya karthikk
Ranch Hand
Posts: 806
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is my context.xml under META-INF folder.



pool.jsp
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64858
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
chaitanya karthikk
Ranch Hand
Posts: 806
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64858
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 806
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64858
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 806
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic