httpservlet doPost() problem
Hi everyone,
I'm having trouble with httpServlet. I can't seem to make a doPost() happen.
Here's my problem:
Everytime I click on the submit button on the html whether it is the "submit post" or "submit get". The text that appears on the console is "doGet". I never get a "doPost". I tried overriding the service method (which I commented out right now), to see what the method type of the request is and it is always GET even if I submitted using post.
Is there something wrong with my code? Or am I not just understanding how doPost() works correctly. I'm really stumped, I don't know what else to do.Any help would be appreciated.
here is my simple html form:
<html><head><title>tester</title></head>
<form name="input" action=http://localhost:8080/testSevlet method="get">
Username:
<input type="text" name="user">
<input type="submit" value="Submit get">
</form>
<form name="input" action=http://localhost:8080/testSevlet method="post">
Username:
<input type="text" name="user">
<input type="submit" value="Submit post">
</form>
Here's my processing servlet
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
public class testServlet extends HttpServlet
{
public testServlet)
{
super();
}
public void init() throws ServletException
{
super.init();
}
public void destroy()
{
super.destroy();
}
/*
public void service(HttpServletRequest req, HttpServletResponse res)
throws ServletException,IOException
{
System.out.println(req.getMethod());
}
*/
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
System.out.println("doGet");
}
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
System.out.println("doPost");
}
}
Its working fineTry it once more...
Hi! Thanks for tyring this one out. What tomcat version did you use and what JDK version? Im using Tomcat 5.5.16 and JDK 5 update 6. I've read from the other threads that when they switched versions of JDK these doPost() problem started to happen.
jakarta-tomcat-5.5.4 and Java 1.5
Thanks! I tried running the servlet using jakarta-tomcat-5.5.4 and same thing still happens. Did you do anything else to make it work?
NOthing much , in the HTML code i added the nbody tag....thats all
Since this is a related problem, I'll ask my question here.
In my servlet I try to get posted data. If i create a html page an make a POST request to the servlet the servlet manages to get the data fine with request.getParameter("A") where A is the variable. I'm using a class call ClientHttpRequest which do the convertion to POST for me on the client side. But it uses the "boundary" thing in the HTTP protocol, and it seems like getParameter("A") in the servlet doesnt handle the boundary thing.
The request to the servlet:
POST /nsb/KomfortfeilServlet HTTP/1.1
Content-Type: multipart/form-data; boundary=-16667epcqi4j-ajqbtgbc0ehq96469kxd00k0
User-Agent: Java/1.5.0_06
Host: 138.62.21.175:8080
Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
Connection: keep-alive
Content-Length: 218
16667lepcqi4j-ajqbtgbc0ehq96469kxd00k0
Content-Disposition: form-data; name="A"
THIS IS THE VALUE OF THE VAR A
16667lepcqi4j-ajqbtgbc0ehq96469kxd00k0--
Anyone have any idea how i get a hold of the variable A and it's value (in this case it's value is "THIS IS THE VALUE OF THE VAR A" :) ?
Hope someone can help me, thanks!
I tried adding the body tag and still nothing, I also tried changing the
name of the forms and the results are the same. Anyway thanks gokul112_3000.
How about you kozmico? you are able to do a POST. What JDK and
Tomcat version are you using?
As for your problem, you could try out this code. This is what I was
using before when I still could do a doPost().
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
String requestContent;
ArrayOutputStream baos = new ByteArrayOutputStream();
InputStream in = req.getInputStream();
int c;
while ((c = in.read()) != -1)
{
baos.write(c);
}
in.close();
requestContent = baos.toString();
doServiceRequest(requestContent, res); // write code here to process the XMl request
}
I'm having a similar problem with POST and HttpServlet in Tomcat 5.0.25 and JDK 1.5.0_06-b05
The problem appears to be performing a POST to an HttpServlet before performing a GET.
Using a very simple form test.html:
--
<html>
<body>
<form method="post" action="http://localhost/test" name="request">
<textarea rows="10" cols="60" name="textarea">
some sample text
</textarea>
<input type="submit" name="submit" value="Ok">
</form>
</body>
</html>
--
If I load this form from Tomcat as http://localhost/test/test.html and click the Ok button, my test HttpServlet works fine, and outputs the following debug info:
--
req.getRequestURL: http://localhost/test/
req.getContentLength: 30
req.getContentType: application/x-www-form-urlencoded
req.getMethod: POST
req.getProtocol: HTTP/1.1
Request.Headers
accept=image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*
referer=http://localhost/test/test.html
accept-language=en-us
content-type=application/x-www-form-urlencoded
accept-encoding=gzip, deflate
user-agent=Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)
host=localhost
content-length=30
connection=Keep-Alive
cache-control=no-cache
Request.Parameters
xml=some sample text
submit=Ok
--
However, if I load the same html file into the browser from the filesystem(e.g., C:\Program Files\Apache Software Foundation\Tomcat 5.0\webapps\test\test.html) and click OK, I get the following output:
--
req.getRequestURL: http://localhost/test/
req.getContentLength: -1
req.getContentType: null
req.getMethod: GET
req.getProtocol: HTTP/1.1
Request.Headers
accept=image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*
accept-language=en-us
accept-encoding=gzip, deflate
user-agent=Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)
host=localhost
connection=Keep-Alive
cache-control=no-cache
Request.Parameters
--
Highlighting the differences:
In the first case -- when the form is POSTed after GETting the form through Tomcat, things are correct:
* the method is POST
* the content-length and content-type are set appropriately
* the request parameters (from req.getParameter()) are correct
However, when the form is POSTed after being opened from the filesystem, HttpServlet has done some strange things:
* the method has changed to GET
* the content-length has changed to -1, and the content-type isn't set
* the content-length and content-type HTTP headers are gone
* there are no request parameters
In both cases, I've verified that the browser is outputting the same HTTP (with the exception of the referer header, which is missing if the form was opened from the filesystem):
--
POST / HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*
Accept-Language: en-us
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)
Host: localhost:8002
Content-Length: 41
Connection: Keep-Alive
Cache-Control: no-cache
textarea=some+sample+text%0D%0A&submit=Ok
-
Why is HttpServlet responding differently to effectively the same HTTP? The only difference is the Referer header -- I don't see why that should matter....
Any help here would be much appreciated!
Thanks,
Gerald
Because if you are running test.html in the same JVM, (ie through tomcat) its a POST. Otherwise its always a GET. So, within JVM requests are always a POST by default, unless you specify method=GET. And from one JVM to other JVM is always a GET.
Change your action attribute to action="/testSevlet" and run your html file from tomcat like- http://localhost:8080/test.html. depending on your directory structure. Then you can see the difference between GET and POST submits.
Are you saying that any request sent from another application to an HttpServlet will be treated as a GET?So how would one ever POST a request to an HttpServlet from another application, e.g., in a client-server architecture?Thanks!
In the client-server architecture, if you are following a HTTP protocol and making a call to the servlet1 to servlet2 which we assume live on two different servers
(different JVMs), then its always a GET, where parameters you pass are always stored as the environment variables in the server where servlet2 is running. So, are all redirects.
I'm still puzzled....
Here are the two situations, condensed.
Situation 1:
* Client requests from HttpServlet, using GET, an HTML page test.html (which contains a form)
* HttpServlet responds (code 200) with the HTML page
* Client POSTs the completed form back to HttpServlet
* HttpServlet interprets it correctly and responds (code 200) with some response HTML
Situation 2:
* Client POSTs form data directly to HttpServlet
* HttpServlet converts the POST to a GET, seems to ignore the form parameters, and responds (code 200)
Since HTTP is stateless, it shouldn't care how the POST message originated. Besides, a browser is just another application in a separate JVM (maybe not even in a JVM!).
Are you saying the HttpServlet in the 2nd situation has actually parsed the form parameters correctly and put them somewhere else, using a redirect and a GET? What if the original POST contents are too big for GET? What if the POST is not form data at all -- say, content-type: text/xml?
Thanks again,
Gerald
Thanks for the help,
Gerald
Not sure I understood situation 2. But if you mean invoking a servlet from a browser by typing in the URL, something like-
http://localhost:7001/resource/LoginServlet?login=aaa
it executes the doGet() in your servlet.
> Since HTTP is stateless, it shouldn't care how the
> POST message originated. Besides, a browser is just
> another application in a separate JVM (maybe not even
> in a JVM!).
Not sure what HTTP being stateless has to do with POST or GET.
GET method is actually requesting a resource in the server.
But when you POST a form, you are actually posting the encrypted
data on to the server, which you only can do when the form (suppose login.html) resides on the same JVM as the LoginServlet.
Again this is just my understanding. Correct me if I am wrong.
I'm definitely using POST, as the raw HTTP data in my earlier posting shows.
The situation I was referring to is invoking the HttpServlet specifically using POST method, but without first GETing a resource from the HttpServlet. (The fact that HTTP is stateless says HttpServlet shouldn't care whether a GET was performed first of not.)
In my example, it was done using an HTML form opened in a browser from the filesystem, and then submitted.
However, it could just as well be any content POSTed from another application -- e.g., a SOAP client, an Excel spreadsheet, an Ajax request, a ServerXMLHTTP COM object, or typed from the keyboard using Telnet.
For some reason, HttpServlet or some other component of Tomcat is converting an explicit POST to a GET and ignoring the parameters in the content. I can't find any explanation for this behavior in the HTTP 1.1 documentation http://www.w3.org/Protocols/rfc2616/rfc2616.html, nor can I find any explanation in the Tomcat or Servlet docs.
Any other suggestions?
Thanks,
Gerald
