Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

GET method working but POST method not working

 
Lalit Bansal
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are the doGet and doPost methods in the servlet any different, or are they using the same code?
 
Lalit Bansal
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pls excuse me for putting incorrect servlet code.
The code is as follows:
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've tried with request.getParameter() also. But still the POST method gives null values.
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 212
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Constructor is not the actual problem.

Can you post the code which is working for you ?
 
Rodrigo Tomita
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So are there any differences between these two codes except for the method names?
 
Amit Ghorpade
Bartender
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Dharmendra,

Thanks very much. It does work with your solution.

Any reason(s) for such behavior ?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic