• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

How do I make stuff show on a web page?

 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry if this sounds very basic, but I'm writting a servlet that will connect to a database and show results of queries on a web page.
What is the code to display the result set? Do I need to have a java server page that calls the servlet?
Annette
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can do everything from within a servlet. Don't try to get fancy. If you have not done any database work before, I suggest you try to get the query and response handling working with a regular Java application first, then move to servlets.
Output of results is just printing to an output stream, but the composition of the page will have to use HTML of course.
Bill

------------------
author of:
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This code will print out all the rows in your ResultSet with column headings in a nice grid.

[This message has been edited by Thomas Paul (edited December 07, 2000).]
 
Annette L'Heureux
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is it necessary to use metadata if I will only be dealing with one database? or is this the best way to do it.
Annette
 
Thomas Paul
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The ResultSetMetaData object gives you information about the ResultSet. In the example, it tells us how many columns are in the ResultSet and the names of the columns. Therefore, the example above is actually reusable since it can be used with any ResultSet.
 
Annette L'Heureux
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks! I'll try it and let you know.
Annette
 
Annette L'Heureux
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, I've tried it and I need to declare out (I'm assuming it's the PrintWriter). How do I do that? The only code I've seen with that in it has a doGet method, and everything is done in that method. Does this sound familiar?
annette
 
Ranch Hand
Posts: 1514
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Annette, Yes, out could be printWriter as in...
PrintWriter out = response.getWriter()
response is the servlet's response. It is used to return data to the client.
Bosun

 
Thomas Paul
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Correct. The code above would be in a service(), doGet(), or doPost() method. The "out" represents the response.getWriter() just as Bosum said.
You could take the above code and move it to a separate class and have it return a String or StringBuffer instead of writing to out. I will leave that as an excercise for anyone interested.
i.e. create a method with a signature of:
public static StringBuffer buildResultGrid(ResultSet rs)
 
Annette L'Heureux
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok, this is starting to make my brain hurt! Doesn't anyone have a simple example of a servlet that accesses a database and displays the results of a simple sql statement (select * from <table> ) on a web page?
I don't imagine it should be that complicated, but when you've never done it before and the only resources you have are a couple of books and this website, it can be pretty overwhelming (especially when your boss would like to see something soon!).
Anyway, one day I'll get there. So basically I call doGet() from main() which is supposed to display the results of my query?
 
Thomas Paul
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Notes: there is no main() method in a servlet. Servlets are executed through the web server/servlet engine.


[This message has been edited by Thomas Paul (edited December 08, 2000).]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic