jQuery in Action, 3rd edition
The moose likes Servlets and the fly likes Displaying html Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Displaying html" Watch "Displaying html" New topic

Displaying html

Mark Leong
Ranch Hand

Joined: Dec 11, 2000
Posts: 60
I am trying to develop a very simple hit counter user servlet. I know how to store, retrieve and update the counter. However I have problems displaying the counter?
As the counter is inserted into the middle of a webpage, I am pretty sure I only need to output a small portion of html to the calling page. The webpage itself will contain the html, body, header tags, etc....
At the calling page I placed a link to the counter servlet by inserting an image tag and pointing the source of the image to my servlet. At the servlet end, I set the contenttype to text/html and define the printwriter. However nothing is displayed at the calling page when I write the output from the servlet.
Can anybody enlighten me?
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13037
Undoubtedly the browser is expecting an image and you are sending text/html - it is not surprising that it does not know what to do with it.
My servlets book has a chapter on using servlets to create images and other resources on the fly. (other servlets books do to but we don't talk about THEM)

author of:
Mark Leong
Ranch Hand

Joined: Dec 11, 2000
Posts: 60
Thank. Actually I do not want to display graphics, just a plain text counter. I have tried setting the content type to text/plain. Still no results.
Any more help?
Frank Carver

Joined: Jan 07, 1999
Posts: 6920
It's your use of the IMG tag which requires the image. When the browser sees such a tag, it tries (naturally enough) to retrieve an image from the specified URL. If it is given back something which is not an image, then it can't display it.

Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
manav kher
Ranch Hand

Joined: Nov 21, 2000
Posts: 98
if you are referencing your servlet directly like this you might try this...write a .js file on the server(simply var counter=x and in the html page include this js file and display the variable as plain text on your page ....ideally the whole page should come from the counter servlet but i've tried this other method and it works
Mark Leong
Ranch Hand

Joined: Dec 11, 2000
Posts: 60
Thanks Frank. But is there anyway I could output the result of the text counter to a webpage without using the image tag?
Mark Leong
Ranch Hand

Joined: Dec 11, 2000
Posts: 60
Found an answer to my problem - display the counter in an applet. The applet will talk to a servlet to update the counter which is fed back to the applet. The applet can be configured by the client.
I agree. Here's the link: http://aspose.com/file-tools
subject: Displaying html
It's not a secret anymore!