• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Anirban Chatterjee
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...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
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Anirban Chatterjee
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 18108
52
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Kyle Brown
author
Ranch Hand
Posts: 3892
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Anirban Chatterjee
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic