aspose file tools*
The moose likes JSP and the fly likes I want to load database table content to the jsp page so that it can be displayed to the user? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "I want to load database table content to the jsp page so that it can be displayed to the user?" Watch "I want to load database table content to the jsp page so that it can be displayed to the user?" New topic
Author

I want to load database table content to the jsp page so that it can be displayed to the user?

Parth A. Mehta
Greenhorn

Joined: Mar 01, 2013
Posts: 6
I am new to the jsp/servlet and learning mvc pattern. I want to build forum web app.

I have made login page, registration page using mvc pattern. but how can I load user generated data to the jsp page after the user logged in.

My Problem is:

what i'm doing at this time is adding the database connection code in jsp page using Scriplets <% %> . But, that is not the mvc, how can i load that user generated table following mvc pattern. what should I have to do?

More Clarification:

Suppose at this time I post this question is goes to database and appear on the jsp page so what so be done to fetch this type of data from database. Can I put ResultSet into session scope?

I hope my question is clear to everybody!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61199
    
  66

Parth A. Mehta wrote:
what i'm doing at this time is adding the database connection code in jsp page using Scriplets <% %> . But, that is not the mvc, how can i load that user generated table following mvc pattern. what should I have to do?

Correct -- fetching data in the controller or view layers is most definitely not MVC.

Your database activity should all be in the model, which should have nothing at all to do with the user interface.

Suppose at this time I post this question is goes to database and appear on the jsp page so what so be done to fetch this type of data from database. Can I put ResultSet into session scope?


No. result sets must remain in the model. The model should pass normal Java collections and beans. Result sets should be closed as soon as possible.

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Parth A. Mehta
Greenhorn

Joined: Mar 01, 2013
Posts: 6
Bear Bibeault wrote:No. result sets must remain in the model. The model should pass normal Java collections and beans. Result sets should be closed as soon as possible.


Ok I agree with that but what I'm doing at this time is :

First, user goes to index.html fill login form to LoginServlet, which passes parameter to bean class. Then LoginService class authenticate user. Finally after successful login I put userid in session and
use sendRedirect to Success.jsp

And here is the Problem: Success.jsp page content is generated from other table. So i have to put database connection code and fetching code on to this jsp.

Is there any alternative, of course there is but, I can't figure out how to pass logged in user to that jsp page which also load data from table. What is your suggestion? Or If I'm doing it wrong way then tell me how to do that. Should I have to use other servlet?

Thanks!!!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61199
    
  66

Parth A. Mehta wrote:
And here is the Problem: Success.jsp page content is generated from other table. So i have to put database connection code and fetching code on to this jsp.

No, you don't. Because you should not be redirecting to the JSP. You should be redirecting to a servlet that does the work, and then forwards to the JSP.

Each JSP that needs data should have a servlet that serves as its page controller. Most of the time, JSPs should never be directly addressed.

For more information on these patterns, please read this article.
 
Don't get me started about those stupid light bulbs.
 
subject: I want to load database table content to the jsp page so that it can be displayed to the user?