File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes How do I make stuff show on a web page? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "How do I make stuff show on a web page?" Watch "How do I make stuff show on a web page?" New topic
Author

How do I make stuff show on a web page?

Annette L'Heureux
Ranch Hand

Joined: Dec 07, 2000
Posts: 135
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

Joined: Mar 22, 2000
Posts: 12761
    
    5
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

Joined: May 05, 2000
Posts: 13974
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).]


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Annette L'Heureux
Ranch Hand

Joined: Dec 07, 2000
Posts: 135
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

Joined: May 05, 2000
Posts: 13974
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

Joined: Dec 07, 2000
Posts: 135
Thanks! I'll try it and let you know.
Annette
Annette L'Heureux
Ranch Hand

Joined: Dec 07, 2000
Posts: 135
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

Joined: Nov 06, 2000
Posts: 1510
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


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
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

Joined: Dec 07, 2000
Posts: 135
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

Joined: May 05, 2000
Posts: 13974

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).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How do I make stuff show on a web page?
 
Similar Threads
404 Error
WebPage to Applet to WebPage to Java
Web Based JUnit testing
how can i do this in javascript?
Need help in identifying a problem.