Sponsors: Michael Fertik - Best JAVA Web hosting Company & 30% off


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-10-2010, 04:42 PM
Member
 
Join Date: Mar 2010
Posts: 13
Rep Power: 0
java_on_fire is on a distinguished road
Default database-html/jsp
hi guys,
i'm new to Java and currently focusing on learning servlet. I'm trying to figure out this example that i have to do. It's asking me to connect to a database retrieve records and display them on a jsp page.

So i have my servlet connected to a database, Now my question is.

1) should i be calling a utility class from a servlet passing it the ResultSet so it can make it ready to be displayed on a jsp page! the utility class then have to do some html coding to display the ResultSet in table format on the jsp page.

2) Use JSTL and create a custom tag to loop through resultsets.

I'm not a big fan of coding html inside classes/servlets. I want classes to do their part and use jsp to view results. Did i answer my own question!!

what other approach you would recommend?

Thanks.
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 03-10-2010, 06:05 PM
FON FON is offline
Senior Member
 
Join Date: Dec 2009
Location: Belgrade, Serbia
Posts: 307
Rep Power: 1
FON is on a distinguished road
Default
In some way you did answer it and you are on good way

Be sure that each class you write has clean and simple responsibility.

So don't call DB from servlet, create another class to do it.
Servlet has its own resposibility.

Once you have ResultSet you can do many things
but be sure never to use scriplets on JSP page.

You don't have to always write tags for this,
if you have simple application then some simple "ResultBean" class will do it.
Use JSTL to iterate through that class if in collection and
simply display it in JSP.

regards!
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 03-11-2010, 05:11 PM
Member
 
Join Date: Mar 2010
Posts: 13
Rep Power: 0
java_on_fire is on a distinguished road
Default
Thanks FON.

I have been searching for the best way to approach this,
My database has a table with four fields, UserID, FirstName, LastName, Address. I have read that it can be done by creating a User bean class with three private instances to match database fields, with getter and setter. Iterate through resultset and create a new user object to store data into user variables then add the user object to ArrayList<User>. But if you have a large ResultSet would this not impact performance by creating that many user objects?

Can I store each row of ResultSet in Map or List?

Thanks,
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 03-11-2010, 07:45 PM
FON FON is offline
Senior Member
 
Join Date: Dec 2009
Location: Belgrade, Serbia
Posts: 307
Rep Power: 1
FON is on a distinguished road
Default
Originally Posted by java_on_fire View Post
Thanks FON.

I have been searching for the best way to approach this,
My database has a table with four fields, UserID, FirstName, LastName, Address. I have read that it can be done by creating a User bean class with three private instances to match database fields, with getter and setter. Iterate through resultset and create a new user object to store data into user variables then add the user object to ArrayList<User>.

Yes that can be one of the solutions.


Code:
But if you have a large ResultSet would this not impact performance by creating that many user objects?
Ask yourself what is it that you want to show to end user in browser.
Is it list of all users? Some of them? You can always use "paging" to
display like 20 of xxx users in one moment on screen. What is this view JSP
all about? Do you really need all that info or you will can DB more often and get info in smaller chunks.


Code:
Can I store each row of ResultSet in Map or List?
Matter of taste...more or less...Create both solutions and see which one
suites your needs better. Play around with members of List/Map on your view JSP page to see how will you get them one by one, sort them, show detailed info about each one...
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 03-12-2010, 04:07 PM
Member
 
Join Date: Mar 2010
Posts: 13
Rep Power: 0
java_on_fire is on a distinguished road
Default finally i did it!
Thanks FON.
I created the following classes;

Final Class User - Bean
Final Class DbConnection with static members, connection, statement and will return statement to servlet class.
Final ResultSetHelper, which has User as an object and returns ArrayList<User>

and it worked.

There is SQL JSP Standard Library which can be used to populate data but i don't know if this considered a good practice!!
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 03-13-2010, 02:08 AM
FON FON is offline
Senior Member
 
Join Date: Dec 2009
Location: Belgrade, Serbia
Posts: 307
Rep Power: 1
FON is on a distinguished road
Default
You are welcome.

Those classes are just fine. Be sure you use names for them not related to
java keywords.

Originally Posted by java_on_fire View Post

There is SQL JSP Standard Library which can be used to populate data but i don't know if this considered a good practice!!
Really haven't used it yet...

TIP:
Do spend some time studying basic web MVC principals.
You are on good way.
Don't rush into any framework just create you own classes that will act as MVC. Once you cleat up all concepts you can easily switch to any framework on market today.

Maybe start here for help:

Server-side Java: Understanding JavaServer Pages Model 2 architecture - JavaWorld

good luck!
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 03-17-2010, 03:03 AM
Member
 
Join Date: Mar 2010
Posts: 13
Rep Power: 0
java_on_fire is on a distinguished road
Default
Originally Posted by FON View Post
You are welcome.

Those classes are just fine. Be sure you use names for them not related to
java keywords.



Really haven't used it yet...

TIP:
Do spend some time studying basic web MVC principals.
You are on good way.
Don't rush into any framework just create you own classes that will act as MVC. Once you cleat up all concepts you can easily switch to any framework on market today.

Maybe start here for help:

Server-side Java: Understanding JavaServer Pages Model 2 architecture - JavaWorld

good luck!
Hi FON
I'm about to finish Murach's Java Servlets and Jsp, really excellent book, i will probably read another book before i move on to hibernate.

Thanks.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 03-18-2010, 01:48 PM
FON FON is offline
Senior Member
 
Join Date: Dec 2009
Location: Belgrade, Serbia
Posts: 307
Rep Power: 1
FON is on a distinguished road
Default
Hi java_on_fire,

I'm glad to hear you find yourself a good book.
Go on with your reading and be free to ask any question
thats in your way.

I was recommended a
"Manning.Java.Persistence.with.Hibernate.Nov.2 006"
for hiberante and i found it OK.

regards
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
load graphics in html page using jsp and database charu JavaServer Pages (JSP) and JSTL 0 03-04-2009 07:12 PM
How to convert access database to mysql database? vrk Advanced Java 2 02-11-2009 04:43 AM
How to check password of a jsp/html with the password of Database(mysql) #1 sk.mahaboobbhasha@gmail.c Java Servlet 2 11-14-2008 01:11 PM
How can I include a html file in html textarea? surya_dks New To Java 2 10-04-2008 07:20 AM
how to populate html:select with values from database sathya_k_83 JavaServer Pages (JSP) and JSTL 1 11-07-2007 10:53 AM


Java Forums is supported by the best jsp hosting.

All times are GMT +2. The time now is 08:51 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org