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 GET vs POST-Doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "GET vs POST-Doubt" Watch "GET vs POST-Doubt" New topic
Author

GET vs POST-Doubt

Karthik Rajendiran
Ranch Hand

Joined: Aug 13, 2004
Posts: 211
Hello Friends,
I am having doubt in servlets.
I have one HTML page with form tag. I am sending get request from there.
I have written a servlet class with doPost method and no get method.
IT gives me error while deploying.
my question
1.why is this error.
2. if u say doget has to be there, i say it is there (since i am inheriting from HttpServlet the doGet in the class should come to my class and still why it shows the error.
Explain me please
thanks


SCJP 1.4 SCWCD 1.4 SCDJWS 1.4
Ben Wood
Ranch Hand

Joined: Aug 14, 2001
Posts: 342
my question
1.WHAT is this error?

can you elaborate, post the error message etc!


SCJP 1.4, www.gsi3d.org.uk
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
Let's see if we can turn this into English...

> I am having doubt in servlets.
I am having some questions about servlets.

> I have one HTML page with form tag. I am sending get request from there.
> I have written a servlet class with doPost method and no get method.

actually quite decent

> IT gives me error while deploying.

I doubt the entire IT field would give you AN error, so I assume you mean "I am getting an error during deployment.".

> my question
My questions

> 1.why is this error.
1. Why am I getting this error

> 2. if u say doget has to be there, i say it is there (since i am inheriting from HttpServlet the doGet in the class should come to my class and still why it shows the error.

no clue what you mean here.


> Explain me please

Help me please.

-------------------------------

As Ben said, post the actual error you're getting and maybe someone may be able to help you out.
And for the future: use correct English grammar and spelling, it really makes it easier for others to understand your questions. Not everyone is from India you see, so we don't all have your language background.


42
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Jeroen,
please don't come into my forum and belittle my visitors. Not all visitors to the 'Ranch have English as their first language and sometimes some interpretation is required.

When you decide to be helpful your posts are of great assistance, but if this is not going to be the case please limit yourself to MD.

Dave
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

> 2. if u say doget has to be there, i say it is there (since i am inheriting from HttpServlet the doGet in the class should come to my class and still why it shows the error.

The doGet method that you inherit does nothing but throw an exception, stating that "doGet is not supported".

If you want to support the GET method, you have to override the doGet method.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Karthik Rajendiran
Ranch Hand

Joined: Aug 13, 2004
Posts: 211
First of all i really apologize for my poor english.
Thanks for reply.

Let me explain it properly.
Let us assume i am request from a HTML page using GET METHOD in FORM ACTION parameter.
<FORM action = "HelloWorld">
HelloWorld is the Servlet class.
The code for HelloWorld is as follows.
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class HelloWorld extends HttpServlet
{
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException,IOException
{
PrintWriter pw = response.getWriter();
pw.println("hi");

}
}


My Question is as follows. I am requesting from HTML using GET METHOD.
When the servlet gets initialized and service method is invoked on the servlet instance,
the service method calls the doPost or doGet based on my Http Method.

When i tried deploying this in Tomcat, i got HTTP 405 Error
HTTP method GET is not supported by this URL.

Why should i get this error when there is doGet of HttpServlet class and overrided doPost method in HelloWorld.


doPost has nothing to do in this situation as HTTP METHOD used is GET.
By the rule of inheritance we have doGet of HttpServlet in HelloWorld.

I am unable to predict the reason for the error.
Please help me on this.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Why should i get this error when there is doGet of HttpServlet class and overrided doPost method in HelloWorld.

Because the default behavior for both the doPost and the doGet methods is to throw an exception stating that "This method is not supported".
If you want to support either of these methods, just override them in your own class.
A simple way to make your servlet behave the same way for GET and POST requests is to override the doGet method as follows:


PS: There is no need to apologize. Your English is much better than my Indian.
[ March 15, 2005: Message edited by: Ben Souther ]
Karthik Rajendiran
Ranch Hand

Joined: Aug 13, 2004
Posts: 211
Hello Ben,
This is karthik and its regarding the servlet clarification.
i have already posted the message in the forum,but needs to get more satisfactory reply.I hope u can give me the answer.

While writing servlet , we extend HttpServlet class,
suppose i request from the HTML with GET Method.

Let HelloWorld be the servlet i have written. It extends HttpServlet.
It has no POST method in it.

I am not calling doGet anywhere in my HelloWorld servlet.


since we extend Httpservlet ,we should get the doGet method of the HttpServlet class in our HelloWorld.

When i deploy i got 405 error, application doesnt not support GET.
Why is this thing.

Wht is the purpose of doGet in HttpServlet. Is it mandatory ot override get.
Explain in detail please.
thanks in advanc
Swapnil Deshpande
Greenhorn

Joined: Aug 15, 2004
Posts: 7
Originally posted by karthik rajendiran:


since we extend Httpservlet ,we should get the doGet method of the HttpServlet class in our HelloWorld.

yes, you should and if fact you do get it in your HelloWorld class.


When i deploy i got 405 error, application doesnt not support GET.
Why is this thing.
Wht is the purpose of doGet in HttpServlet. Is it mandatory ot override get.

No, its not mandatory to override this method. But if you dont, you will get get this exception for a get request. Confused ? Ok, let me repeat what Ben has already said


The doGet method that you inherit does nothing but throw an exception, stating that "doGet is not supported".

So if you open your HelloWorld class file with a decompiler, you can see that the inherited doGet method simply throws an exception stating that "doGet is not supported".
hope this helps ..
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by karthik rajendiran:
Hello Ben,
This is karthik and its regarding the servlet clarification.
i have already posted the message in the forum,but needs to get more satisfactory reply.I hope u can give me the answer.

While writing servlet , we extend HttpServlet class,
suppose i request from the HTML with GET Method.

Let HelloWorld be the servlet i have written. It extends HttpServlet.
It has no POST method in it.

I am not calling doGet anywhere in my HelloWorld servlet.


since we extend Httpservlet ,we should get the doGet method of the HttpServlet class in our HelloWorld.

When i deploy i got 405 error, application doesnt not support GET.
Why is this thing.

Wht is the purpose of doGet in HttpServlet. Is it mandatory ot override get.
Explain in detail please.
thanks in advanc



Every time you click on a link, call a webpage from your bookmarks (favorite places, or type the name into your browser's address bar, you are making an HTTP GET request. To make a POST request (or at least, the most common way POST requests are made) is to create an HTML form with the "method" attribute set to "POST" and submit the form.

It's worth Installing Firefox and then adding the

Live HTTP Headers plugin to see the actual request/response headers that get passed between browsers and servers. You'll learn a lot from it.
Anselm Paulinus
Ranch Hand

Joined: Sep 05, 2003
Posts: 389
Originally posted by David O'Meara:
Jeroen,
please don't come into my forum and belittle my visitors. Not all visitors to the 'Ranch have English as their first language and sometimes some interpretation is required.

When you decide to be helpful your posts are of great assistance, but if this is not going to be the case please limit yourself to MD.

Dave


Thanks David for letting Jeron realize this is not EnglishRanch but JavaRanch....No more comment on this.
Chaitanya Athanikar
Greenhorn

Joined: Feb 07, 2005
Posts: 14
Hey karthik,

The doGet() method we get from inheretence need to be overidden to be used in our servlet.By default html sends the request using GET method.
You would have doGet() method from HttpServlet but it would not be useful as no code is written in it to handle your sepecific request.

The server tries to look for your specific doGet() method version in your class and not the doGet() of HttpServlet.With my understanding you need to explicitly override doGet() or else declare mehtod="post" in your html form.

I hope I have answered your question a little bit atleast.


SCJP 1.4<br />SCWCD(in process)
murali kankanala
Ranch Hand

Joined: Nov 15, 2004
Posts: 110
Originally posted by Chaitanya Athanikar:
Hey karthik,

The doGet() method we get from inheretence need to be overidden to be used in our servlet.By default html sends the request using GET method.
You would have doGet() method from HttpServlet but it would not be useful as no code is written in it to handle your sepecific request.

The server tries to look for your specific doGet() method version in your class and not the doGet() of HttpServlet.With my understanding you need to explicitly override doGet() or else declare mehtod="post" in your html form.

I hope I have answered your question a little bit atleast.
murali kankanala
Ranch Hand

Joined: Nov 15, 2004
Posts: 110
Yaa that's right. this answer is suitable as an answer to your question.
Girish Babu
Greenhorn

Joined: Mar 18, 2005
Posts: 6
Originally posted by karthik rajendiran:
Hi,

First of all u have to understand why u r extending the HttpServlet Class, if u want any Http Method to be handled in ur servlet better you override the service method,

your container is designed to handled your servlets depending on the Http methods,

once you override the doPost method and not the doGet, your container understands that you are restricting the user by sending the request by GET method, hence the error,
 
 
subject: GET vs POST-Doubt