This week's book giveaway is in the Spring forum.
We're giving away four copies of Spring in Action (5th edition) and have Craig Walls on-line!
See this thread for details.
Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

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

 
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.
 
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
 
Bartender
Posts: 19988
95
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.
 
author
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).]
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!