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 How to make a 'post' request from an application to a Servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to make a Watch "How to make a New topic
Author

How to make a 'post' request from an application to a Servlet

Anirban Chatterjee
Greenhorn

Joined: Feb 06, 2001
Posts: 18
...how can I make a 'POST' request from an application to a servlet .
I assume that the URLConnection makes a 'GET' request.
Ray Smilgius
Ranch Hand

Joined: Jan 29, 2001
Posts: 120
URLconnection is a post by default and to your question
just use a basic html form and in your action set it to post
Hope that helps
Ray Smilgius
------------------
Sun Certified Java Programmer
Sun Certified Java Developer
I-Net Certified
A+ Certified
Network+ Certified
MCP


SCJO, SCJD, SCWCD, I-Net+, A+, Network+, MCSD, MCDBA, MCP, MCT
Anirban Chatterjee
Greenhorn

Joined: Feb 06, 2001
Posts: 18
hi ,
Thanks for the reply .
Can you also let me know if its possible to make a 'GET' request through the URLConnection ?
Anirban
Ray Smilgius
Ranch Hand

Joined: Jan 29, 2001
Posts: 120
Just make the request from your browser and put the url connection in the doGet() method of the servlet and that will do a request to the server.
------------------
Sun Certified Java Programmer
Sun Certified Java Developer
I-Net Certified
A+ Certified
Network+ Certified
MCP
Anirban Chatterjee
Greenhorn

Joined: Feb 06, 2001
Posts: 18
Hi ,
I am sorry , I might not have explained the situation properly .
I needed to make either a GET or a POST request from a application to a set of servlets .These servlets were designed badly by God-knows-who.They either have a doGet() or a doPost() method , but not both .
A browser never comes into picture at all .
From the application , I neet to make an URLConnection , and read/post data to these servlets by making either a GET or a POST request .
Can you help me out ?
Thanks
Anirban
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15629
    
  15

It's not uncommon for a servlet to be written so that doGet() invokes doPost(), making the request type immaterial. If you're writing a client application or applet you set the request type and URL and -- if it's a POST, open the POST stream to write the POST data. Beyond that, I can only recommend you read the URLConnection autodocs and/or a text on Java networking.


Customer surveys are for companies who didn't pay proper attention to begin with.
Kyle Brown
author
Ranch Hand

Joined: Aug 10, 2001
Posts: 3879
I second the response that you should start by READING the JavaDoc for URLConnection and HttpURLConnection. If you do so you will find the following method in HttpURLConnection:
public void setRequestMethod(String method)
throws ProtocolException
Set the method for the URL request, one of: GET POST HEAD OPTIONS PUT DELETE TRACE are legal, subject to protocol restrictions. The default method is GET.
(BTW, when you get back a URLConnection from the constructor that uses a URL starting with "http://" what is really returned is an HttpURLConnection).
Kyle

------------------
Kyle Brown,
Author of Enterprise Java (tm) Programming with IBM Websphere
See my homepage at http://members.aol.com/kgb1001001 for other WebSphere information.


Kyle Brown, Author of Persistence in the Enterprise and Enterprise Java Programming with IBM Websphere, 2nd Edition
See my homepage at http://www.kyle-brown.com/ for other WebSphere information.
Anirban Chatterjee
Greenhorn

Joined: Feb 06, 2001
Posts: 18
Thanks a lot .
I was checking out the HttpURLConnection class myself .

though I would like to disagree with Kyle about the return object of url.openConnection
when I wrote a code like :
<code>
URL myUrl = new URL("http://www.yahoo.com");
HttpURLConnection urlc = myUrl.openConnection();
</code>
I got this compilation error :
incompatible types; found: java.net.URLConnection, required: java.net.HttpURLConnection
So I had to use a cast to get teh required HttpURLConnection like so :
<code>
URL myUrl = new URL("http://www.yahoo.com");
HttpURLConnection urlc = (HttpURLConnection)myUrl.openConnection();
</code>

Thanks
Anirban
PS:I would like to correct myself . Kyle is right that HttpUrlConnection is indeed returned from UrlConnection .Otherwise , it would have thrown a run-time class-cast Exception.
[This message has been edited by Anirban Chatterjee (edited November 16, 2001).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to make a 'post' request from an application to a Servlet
 
Similar Threads
Html POST & GET
POST request from Java app
passing large data to a servlet
Running Ant from a Servlet
changing http get request to post request