• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Accessing servlets using URLConnection

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hai all,
Good morning.
I have a small problem in accessing servlets using URLConnection in stand-alone application. Following is the snippet of client and servlet.
URL url = new URL("http://mymachine/abc/Servlet");
HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
httpConnection.setDoOutput(true);
httpConnection.setDoInput(true);
httpConnection.setRequestMethod("POST");
httpConnection.setUseCaches(false);
httpConnection.setRequestProperty("Content-type", "application/octet-stream");
httpConnection.setAllowUserInteraction(false);
PrintWriter out = new PrintWriter(httpConnection.getOutputStream());
String str = "Hello world";
out.println(str);
out.close();

Servlet

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("in doGet");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("in doPost");
}
But it does not seem to invoke any of these methods in servlet.
FYI : I have deployed this servlet in Weblogic 6.1.

Poornachandran
 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Poornachandran,
u can try this after ur PrintWriter out = new PrintWriter(httpConnection.getOutputStream()); line,
BufferedInputStream bis = new BufferedInputStream(httpConnection.getInputStream());
bis.close();
that should work. i face the similar problem and the above solution did work.
i deduced that ONLY IF WE OPEN AN INPUT STREAM CONNECTION to the servlet, the servlet RESPONDS...DOESN'T matter if we don't want to do anything with the input stream we get (thats why we are closing it in above code).
regards
maulin
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure if this is related to your problem, but you should never close the output stream until you are through with the connection - it closes the socket and you will never get any response. Instead you should flush() the output stream so the buffered data will all be sent.
Bill
 
That is a really big piece of pie for such a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic