Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

invoking a servlet through java code

 
ajse ruku
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
ajse
 
Dave Robbins
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

Dave
 
ramprasad madathil
Ranch Hand
Posts: 489
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

ram.
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 809
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic