aspose file tools*
The moose likes Servlets and the fly likes GET method working but POST method not working Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "GET method working but POST method not working" Watch "GET method working but POST method not working" New topic
Author

GET method working but POST method not working

Lalit Bansal
Ranch Hand

Joined: Jan 29, 2007
Posts: 63
Hi,

I'm implementing a simple web application.
I've a jsp page. In the FORM tag I've defined action of my servlet class and method as POST.
The form contains two text fields and a submit button.

In my servlet, I'm doing request.getParameter(<<text field name>> .

If I'm using GET method, I get the values entered in the text fields.
However, if I'm using POST method, I get null values.

Can anyone pls help me out with a solution for same.

Thanks in advance.

I'm using Tomcat5 webserver.

regards,
Lalit
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41905
    
  63
Are the doGet and doPost methods in the servlet any different, or are they using the same code?


Ping & DNS - my free Android networking tools app
Lalit Bansal
Ranch Hand

Joined: Jan 29, 2007
Posts: 63
I've written the login (of retrieving parameters) in doPost method.
In doGet I'm calling the doPost method by passing request and response objects.





Lalit Bansal
Ranch Hand

Joined: Jan 29, 2007
Posts: 63
Pls excuse me for putting incorrect servlet code.
The code is as follows:
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Hi,

i am not sure,inside your post method...

your testing the textbox value..using
System.out.println(request.getAttribute("empId"));System.out.println(request.getAttribute("loginPwd"));

use--- request.getParameter("textbox_name");

thanks & regards,
seetharaman
Lalit Bansal
Ranch Hand

Joined: Jan 29, 2007
Posts: 63
I've tried with request.getParameter() also. But still the POST method gives null values.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41905
    
  63
The fact that this class has a constructor is suspicious - servlets should not have an explicit constructor, nor should any of your code be calling it.
[ April 29, 2008: Message edited by: Ulf Dittmer ]
Lalit Bansal
Ranch Hand

Joined: Jan 29, 2007
Posts: 63
I'm using Eclipse 3.3.2 IDE.

The servlet stub code was generated automatically from it. I've just implemented my logic in the doPOST method.
sudhir nim
Ranch Hand

Joined: Aug 29, 2007
Posts: 212

Constructor is not the actual problem.

Can you post the code which is working for you ?


[Servlet tutorial] [Servlet 3.0 Cook Book]
Rodrigo Tomita
Ranch Hand

Joined: Apr 28, 2008
Posts: 70
Originally posted by Lalit Bansal:
I've tried with request.getParameter() also. But still the POST method gives null values.


OK. But just to be clear, you do know the difference between request.getParameter() and request.getAttribute(), right? For form input boxes, it should be getParameter().

I'm just asking because, as Seetharam mentioned, you are printing getAttribute() and using getParameter() later:



Regards.
Lalit Bansal
Ranch Hand

Joined: Jan 29, 2007
Posts: 63
Pls excuse me for a long post. As becuase there was a bit of confusion in the forum because I'd posted incorrect code earlier, I'm posting both the code - which does not work(i.e., code using POST method) and which works (i.e., code using GET method)

CODE THAT
DOES NOT
WORK






CODE THAT WORKS



[ UD: Removed the HTML from both examples, because -being unformatted in a single line- it screwed up the layout. Please be careful to post formatted code only, and check the layout after posting. ]
[ April 29, 2008: Message edited by: Ulf Dittmer ]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41905
    
  63
So are there any differences between these two codes except for the method names?
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

Well I am not so much experienced as Ulf and Ben, so with due respect to their posts I would just like to suggest that check if the form that the servlet gets data from, uses POST method to submit data.



Hope this helps


SCJP, SCWCD.
|Asking Good Questions|
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41905
    
  63
check if the form that the servlet gets data from, uses POST method to submit data.

Well, we could have checked that until I deleted the complete HTML from the previous post :-) Sorry, had to be done - it made this topic completely unreadable.

But it's unlikely to be the problem - as far as the getParameter method is concerned, there is no difference between GET and POST.
Lalit Bansal
Ranch Hand

Joined: Jan 29, 2007
Posts: 63
Ulf,

There are no differences in the code except the method names.

When my html request is GET type, I get the values of input fields.
But when request is POST type, I get null values.

Amit : Form uses GET method when call in servlet is to doGet Method
Form uses POST method when call in servlet is to doPost Method

Is this issue anything related with server I'm using?
Dharmender Singh Singh
Greenhorn

Joined: Apr 30, 2008
Posts: 1
you have to remove the attribute (enctype="text/plain") from your jsp/html page from the <form> tag, to work fine. after removing try the application it wil certainly work.
Lalit Bansal
Ranch Hand

Joined: Jan 29, 2007
Posts: 63
Hi Dharmendra,

Thanks very much. It does work with your solution.

Any reason(s) for such behavior ?
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

The default encoding for an HTTP post request (what your servlet is expecting) is application/x-www-form-urlencoded; not text/plain.

Nice catch by Dharmender Singh Singh.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: GET method working but POST method not working