File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Invoke a Servlet from console ??

 
Kumar Navin
Ranch Hand
Posts: 52
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kumar,

Have you looked at the httpclient project from Apache?
http://jakarta.apache.org/commons/httpclient/
 
Tim Holloway
Saloon Keeper
Pie
Posts: 17616
39
Android Eclipse IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Mike Ottinger
Ranch Hand
Posts: 125
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Murasoli Maran
Ranch Hand
Posts: 193
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic