Status
Not open for further replies.

karthik_rcs

Broken In
Hello Friends,
I need some help regarding the HTTP methods (TRACE,DELETE,PUT,HEAD).

PROBLEM EXPLAINED
------------------
I want to request from html to servlet using these methods.As the HTML FORM method attribute supports only get or post, we cannot request use other methods for the <FORM METHOD=""> attribute.
While searching in our forum, i found the following thread

If you want a servlet to respond to other request types, use java.net.HttpURLConnection.setRequestMethod("TRACE");
A URL created with a string starting "*" will give you a HttpURLConnection from the getConnection method.


Can anybody please explain me
1. How to request via other http methods
2. how can i make my servlet to respond to these requests
3. What is the purpose of the HttpURLConnection
Thanks in advance
If we are going to write the
java.net.HttpURLConnection.setRequestMethod("TRACE"); in the servlet class,
then it wont work as
servlet comes into picture only after the request from page.

Can anyone please throw some examples with little explanation. I kindly request people to post with example.

SECOND QUESTION
What is the use of HttpURLConnection class. What is the purpose of that.
Explain me in detail.
 

icecoolz

Cyborg Agent
OK, first of all FORM METHOD need not necessarily be the only way to traverse an HTML file. I could for example when I click the submit button it in fact could call a JS which could invoke the servlet. The real problem would arise from all the variables as in form fields which would need to be sent along with the URL. I dont understand why you dont want to use POST/GET with the form pages.
 
OP
K

karthik_rcs

Broken In
Reply please help

YOu are right.
But i want to see how the doTrace and other http methods work.
its not i am against any get or post request.
I want to learn how the request can be submitted via other httpmethods.
I know servlets .
Please explain me how to set request via DELETE,PUT,TRACE,...methods
apart from the usual get or post.

I hope u will understand my query
please do reply
 

icecoolz

Cyborg Agent
You could simply link it to the servlets directly. And then see what happens. Its been ages since I worked with servlets, however if I remem correctly servlets mainly has three methods, doGet, do Post and service method. The service method will be invoked if the request which comes is neither a GET nor a POST. And the service method basically contains two parameters. HTTPRequest and HTTPResponse. So using these two variables one can find out all sorts of informtion such as request IP MIME types and so on.
 

enoonmai

Cyborg Agent
1. How do you request via other methods? Well, as icecoolz already explained, you use the normal HttpServlet class and override the service() method or override one of these methods directly - doGet(), doPost(), doHead(), doDelete(), doPut(), etc. You already knew that, I guess. The service() method itself is not typically overridden, nor are the doTrace() and doOptions() methods, since the service method automatically handles TRACE and OPTIONS methods by calling the required methods automatically. Also, know that doGet automatically provides support for HEAD operations too, so there would be no point in overriding that either.

2. To make your servlet handle these requests, you have to override them manually. Know that you do not ever override them by default, unless you know what you are doing since its very easy to mess it up and you hardly ever use the doPut, doDelete methods, except in some rare custom applications running on AS's such as WebLogic, where you need the functionality. So, if I were to override, lets say the doPut method, which is like using FTP to send a file. It would look something like this:

Code:
protected void doPut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException

So when you send a file/request to store it in the server with the PUT form method, this doPut() method is called and its contents executed. Of course, if you use such a method, you have to remember to not modify any content headers sent with the request including Content-Length, Content-Type, Content-Transfer-Encoding, Content-Encoding, Content-Base, Content-Language, Content-Location, Content-MD5, and Content-Range, unless you want to see a 501 Not Implemented error. Overriding rules depend on the method that's being overridden. See this page for more rules:
*java.sun.com/products/servlet/2.1/api/javax.servlet.http.HttpServlet.html

Also remember that this works only for HTTP 1.1.

Coming to the HttpURLConnection class, its just a URLConnection class with support for HTTP features, such as status codes, redirects (301/302, etc), Content Type, Encoding, headers, etc. Most of its features are derived from java.net.URLConnection. If I were to explain all the features and supported methods in the class, it would make a really, really huge post. Instead, here's the link to the official class doc, along with detailed explanations of the methods.
*java.sun.com/j2se/1.4.2/docs/api/java/net/HttpURLConnection.html

I will post examples later, if you still need them.
 
Status
Not open for further replies.
Top Bottom