• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How do I make stuff show on a web page?

 
Annette L'Heureux
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • 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
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13058
6
  • Mark post as helpful
  • send pies
  • 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:
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • 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
  • 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
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
 
Bosun Bello
Ranch Hand
Posts: 1510
  • Mark post as helpful
  • send pies
  • 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
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • 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
  • 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
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • 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).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic