File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes Suggest me the best practise for accesing database by JSP page Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Suggest me the best practise for accesing database by JSP page" Watch "Suggest me the best practise for accesing database by JSP page" New topic
Author

Suggest me the best practise for accesing database by JSP page

Saurabh Joshi
Ranch Hand

Joined: Nov 15, 2007
Posts: 37
Hi,

Can you please suggest me the best practises to connect to database through JSP page.

I am using the Struts-Portlet environment. And I have to display some contents from the database to the jsp page(view.jsp).

Please suggest.

Saurabh.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

The best practice is not to connect to a database from within a JSP.

It is better to build your business logic (including database access) in plain old Java objects 'POJOs' that can be accessed from any interface.
Then use a servlet to interact with those classes.
One all of this interaction is done and you've got your data in a simple datastructure (an arraylist of beans for example) forward to the JSP.

A search on the web for "MVC" will bring up plenty of information for you to explore.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Here is an excellent article to get you started:
http://www.javaranch.com/journal/200603/Journal200603.jsp#a5
sudhir nim
Ranch Hand

Joined: Aug 29, 2007
Posts: 212

Never connect to database directly from jsp.
Create dao for data access.
Create services and let services use dao to perform data base operations.
Let servlets use services and put the data in appropriate scope and forward to jsp.
Let the jsp display data.

I want to say, never put business logic in jsp pages, use jsp only for displaying data. let servlets or other helper classes do the actual processing, connect to database and make data available to jsp for display.

It applies to pure jsp/servlet based application as well as to applications developed with other web frameworks like struts,webwork etc in this case it will be action classes insted of servlets which access services, also the way you make data available to jsp will differ.

If you must access data from jsp, use custom tags or helper classes.
[ November 16, 2007: Message edited by: sudhir nim ]

[Servlet tutorial] [Servlet 3.0 Cook Book]
Saurabh Joshi
Ranch Hand

Joined: Nov 15, 2007
Posts: 37
HI Ben & Sudhir,

Thanks for that good information. I will surely keep that in mind before going any further.

Thanks & Regards,
Saurabh.
Saurabh Joshi
Ranch Hand

Joined: Nov 15, 2007
Posts: 37
Actually I was earlier using JSTL tags for the datasource connectivity <c:sql..../> but it was unable to give me proper output. Thats why I was looking for an appropriate option.

Thanks a lot.
Saurabh.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

If you look at the JSTL spec, you will see that that the JSTL SQL tags were not meant for real production apps.

Personally, I think it was a big mistake including them in the specification in the first place. Your best bet is to ignore them completely.
Saurabh Joshi
Ranch Hand

Joined: Nov 15, 2007
Posts: 37
Yes Ben I absolutly agree with you but I took 2 whole days to find out my mistake. Anyways now I will follow the guideline given by you.

Saurabh.
Bryce Martin
Ranch Hand

Joined: Nov 19, 2007
Posts: 269
question about performance. Does it take any longer to pass from the dao to the beans and onto the jsp rather than just doing it all from the jsp using a resultset? I'm just curious to any performance gains or losses from one method to the other
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Bryce,
It would be better if you posted your question in a new thread.

Since the original poster's question was already answered, aksing here isn't really a hi-jack but the search engine works better if each thread contains one question and the answers to it.

Thanks
-Ben
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Suggest me the best practise for accesing database by JSP page