File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes invoking a servlet through java code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "invoking a servlet through java code" Watch "invoking a servlet through java code" New topic

invoking a servlet through java code

ajse ruku
Ranch Hand

Joined: May 06, 2005
Posts: 196
Hi all,
I want to invoke a servlet through java code.But issue is how to get the gif files used in the servlets?Becouse when we invoke a servlet through java code instead of browser,we get only bytes or strings from the inputstream of the servlet.

with regards,
Dave Robbins
Ranch Hand

Joined: Sep 16, 2003
Posts: 131
you can call a servlet from your code using a HttpURLConnection
I do this often and have the servlet send me a custom object that contains the data I want
since I know what's coming back I read it with an ObjectInputStream then cast it to what it is
I guess the same approach would work with a gif
perhaps somebody else has an easier way

ramprasad madathil
Ranch Hand

Joined: Jan 24, 2005
Posts: 489

You wouldnt usually want a Servlet that responds to a browser's http request (& responds with html content) to be invoked from a java program.

Servlets meant to be invoked from stand alone java programs write out Object data or primitive data types or Strings. Occasionally you may find some writing out byte streams, but very rarely html

Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
To get the resources referenced in the page (JavaScript, CSS, images) you need to send additional requests. The browser sends those automatically, but your Java client would need to parse the HTML it received to determine which images it should also download.

Once you have the image, it's just a stream of bytes, just like the text of the HTML page. You can store it to a file or display it or whatever else you need to do with it.

There are libraries that help you interpret a stream of HTML, like TagSoup, JHTMLTidy and NekoXMI.
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
Following link might help


Asking Smart Questions FAQ - How To Put Your Code In Code Tags
I agree. Here's the link:
subject: invoking a servlet through java code
It's not a secret anymore!