Results 1 to 8 of 8
  1. #1
    java_on_fire is offline Member
    Join Date
    Mar 2010
    Posts
    19
    Rep Power
    0

    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.

  2. #2
    FON
    FON is offline Senior Member
    Join Date
    Dec 2009
    Location
    Belgrade, Serbia
    Posts
    368
    Rep Power
    5

    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!

  3. #3
    java_on_fire is offline Member
    Join Date
    Mar 2010
    Posts
    19
    Rep Power
    0

    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,

  4. #4
    FON
    FON is offline Senior Member
    Join Date
    Dec 2009
    Location
    Belgrade, Serbia
    Posts
    368
    Rep Power
    5

    Default

    Quote 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.


    Java 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.


    Java 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...

  5. #5
    java_on_fire is offline Member
    Join Date
    Mar 2010
    Posts
    19
    Rep Power
    0

    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!!

  6. #6
    FON
    FON is offline Senior Member
    Join Date
    Dec 2009
    Location
    Belgrade, Serbia
    Posts
    368
    Rep Power
    5

    Default

    You are welcome.

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

    Quote 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!

  7. #7
    java_on_fire is offline Member
    Join Date
    Mar 2010
    Posts
    19
    Rep Power
    0

    Default

    Quote 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.

  8. #8
    FON
    FON is offline Senior Member
    Join Date
    Dec 2009
    Location
    Belgrade, Serbia
    Posts
    368
    Rep Power
    5

    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

Similar Threads

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •