File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Invoke a Servlet from console ?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Invoke a Servlet from console ??" Watch "Invoke a Servlet from console ??" New topic
Author

Invoke a Servlet from console ??

Kumar Navin
Ranch Hand

Joined: May 27, 2004
Posts: 52
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

Joined: Dec 11, 2004
Posts: 13410

Kumar,

Have you looked at the httpclient project from Apache?
http://jakarta.apache.org/commons/httpclient/


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16014
    
  20

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.


Customer surveys are for companies who didn't pay proper attention to begin with.
Mike Ottinger
Ranch Hand

Joined: Jan 11, 2002
Posts: 125
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.


SCJP 1.4 SCJD 1.5
Murasoli Maran
Ranch Hand

Joined: Jun 08, 2003
Posts: 193
Do you tested by giving the URL in browser?.Is the servlet is clean from errors?.

MM
Mike Ottinger
Ranch Hand

Joined: Jan 11, 2002
Posts: 125
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Invoke a Servlet from console ??