• 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

Invoke a Servlet from console ??

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi! Friends,
My problem might seem to be a repetitive one, but am not able to debug it. I have written a class which invokes a Servlet using net package but is giving an error.
The code snippet of class is as follows:
public static void main(String args[])
{
URL url = new URL("http://12.125.0.15/MyProj/servlet/MyServlet");

URLConnection con = url.openConnection();
System.out.println("Received a : " + con.getClass().getName());

con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);

System.out.println("Getting an output stream...");
OutputStream os = con.getOutputStream();
String msg = "Message from client";
OutputStreamWriter osw = new OutputStreamWriter(os);
osw.write(msg);
osw.flush();
osw.close();
System.out.println("After flushing output stream. ");

System.out.println("Getting an input stream...");
InputStream is = con.getInputStream();
// any response?
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line = null;
......
}

When I execute this code from the command line, it gives an error as such:

java.io.IOException: Server returned HTTP response code: 405 for URL: http://12.125.0.15/sec/servlet/MailServlet
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at GoodURLPost.main(GoodURLPost.java:38)
...........................................................................
I have tried the links I found on Javaranch for the similar problem but in vain. Can anybody rectify the mistake or suggest something.
Thanks,

-Navin.
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kumar,

Have you looked at the httpclient project from Apache?
http://jakarta.apache.org/commons/httpclient/
 
Saloon Keeper
Posts: 27764
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, look in your server's logfiles. A 405 means the servlet threw an exception, but the servers generally will dump a stacktrace to either the logfile or stdout/err.
 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kumar, I had a similar issue. Using the httpclient api works quite well. Any 4xx http codes has to do with some sort of authentication/authorization issue. The httpclient GetMethod class allows for you to supply credentials to a http call before invoking it. To see what's going on in your code invoke your url using the GetMethod class without any credential settings, wrap your code in a simple try/catch, it *should* print a stack trace giving you more info on what's wrong. Another thing to do as well is do a print out of the remote user getRemoteUser() on the servlet that you're invoking, just to see what username is associated with the request. Good luck.
 
Ranch Hand
Posts: 193
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you tested by giving the URL in browser?.Is the servlet is clean from errors?.

MM
 
Mike Ottinger
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, the point of httpclient is to have your application *act* like a browser. Go to http://jakarta.apache.org/commons/httpclient/ and check out the authentication guide for passing credentials for an http session. Also check out the section on methods, it will give you snippets of code on how to use this.
 
reply
    Bookmark Topic Watch Topic
  • New Topic