It's not a secret anymore!
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

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("");

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


System.out.println("Getting an output stream...");
OutputStream os = con.getOutputStream();
String msg = "Message from client";
OutputStreamWriter osw = new OutputStreamWriter(os);
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: Server returned HTTP response code: 405 for URL:
at Source)
at GoodURLPost.main(
I have tried the links I found on Javaranch for the similar problem but in vain. Can anybody rectify the mistake or suggest something.

Ben Souther

Joined: Dec 11, 2004
Posts: 13410


Have you looked at the httpclient project from Apache?

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: 17421

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.

An IDE is no substitute for an Intelligent Developer.
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?.

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 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.
I agree. Here's the link:
subject: Invoke a Servlet from console ??
It's not a secret anymore!