473,322 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Using HttpURLConnection to POST to Servlet in Tomcat 5 results in GET not POST on server side

Does anyone know what is wrong with this code or why I might be experiencing
this behavior?

I have a Java application that submits HTTP POST requests to a servlet (with
content in the body of the request). I am using the java.net package. My
system worked fine with Tomcat 4.1.18: the servlet was invoked with a POST
request and it could read the content of the request using an InputStream.
But when I deploy my servlet in Tomcat 5 it no longer is invoked as a POST,
but rather a GET (the doGet method is called instead of the doPost, which is
called when deployed in Tomcat 4). The Serlvet therefore does not get the
body of the request (the Servlet's InputStream has no content).

My client code looks like this:

String request = "test";
java.net.URL netUrl = new java.net.URL(http://localhost:8080/myservlet);
connection = (HttpURLConnection) netUrl.openConnection();

connection.setRequestMethod( "POST" );

connection.setRequestProperty("Content-Length", "" +
Integer.toString(request.getBytes().length));

connection.setUseCaches(false);

connection.setDoInput(true);

connection.setDoOutput(true);
DataOutputStream out = new DataOutputStream( connection.getOutputStream() );
out.writeBytes( request );

out.flush();

out.close();
BufferedReader reader = new BufferedReader( new InputStreamReader(
connection.getInputStream() ) );
String response = reader.readLine();
while( null != response )

{

System.out.println( response );
response = reader.readLine();

}

Does anyone have any ideas?

Thank you,

Steve
Jul 17 '05 #1
2 30771

"Steve" <no@email.given> wrote in message
news:Ci*****************@newsread2.news.pas.earthl ink.net...
Does anyone know what is wrong with this code or why I might be experiencing this behavior?

I have a Java application that submits HTTP POST requests to a servlet (with content in the body of the request). I am using the java.net package. My
system worked fine with Tomcat 4.1.18: the servlet was invoked with a POST
request and it could read the content of the request using an InputStream.
But when I deploy my servlet in Tomcat 5 it no longer is invoked as a POST, but rather a GET (the doGet method is called instead of the doPost, which is called when deployed in Tomcat 4). The Serlvet therefore does not get the
body of the request (the Servlet's InputStream has no content).

My client code looks like this:

String request = "test";
java.net.URL netUrl = new java.net.URL(http://localhost:8080/myservlet);
connection = (HttpURLConnection) netUrl.openConnection();

connection.setRequestMethod( "POST" );

connection.setRequestProperty("Content-Length", "" +
Integer.toString(request.getBytes().length));

connection.setUseCaches(false);

connection.setDoInput(true);

connection.setDoOutput(true);
DataOutputStream out = new DataOutputStream( connection.getOutputStream() );

out.writeBytes( request );

out.flush();

out.close();
BufferedReader reader = new BufferedReader( new InputStreamReader(
connection.getInputStream() ) );
String response = reader.readLine();
while( null != response )

{

System.out.println( response );
response = reader.readLine();

}

Does anyone have any ideas?

Thank you,

Steve


Try appending a slash (/) to the URL you are using to refer to the servlet.
Tomcat might have decided to do a redirect with the slash appended. I have
seen this behaviour before with other servlet containers also suddenly
introduced with new release versions.

Silvio Bierman
Jul 17 '05 #2
I discovered that Tomcat 5 was indeed doing a redirect (after appending the
"/"). Appending the "/" to the URL was the cure.

Thank you,
Steve

"Silvio Bierman" <sb******@idfix.nl> wrote in message
news:40***********************@news.xs4all.nl...

"Steve" <no@email.given> wrote in message
news:Ci*****************@newsread2.news.pas.earthl ink.net...
Does anyone know what is wrong with this code or why I might be experiencing
this behavior?

I have a Java application that submits HTTP POST requests to a servlet

(with
content in the body of the request). I am using the java.net package. My
system worked fine with Tomcat 4.1.18: the servlet was invoked with a POST request and it could read the content of the request using an InputStream. But when I deploy my servlet in Tomcat 5 it no longer is invoked as a

POST,
but rather a GET (the doGet method is called instead of the doPost, which is
called when deployed in Tomcat 4). The Serlvet therefore does not get
the body of the request (the Servlet's InputStream has no content).

My client code looks like this:

String request = "test";
java.net.URL netUrl = new java.net.URL(http://localhost:8080/myservlet);
connection = (HttpURLConnection) netUrl.openConnection();

connection.setRequestMethod( "POST" );

connection.setRequestProperty("Content-Length", "" +
Integer.toString(request.getBytes().length));

connection.setUseCaches(false);

connection.setDoInput(true);

connection.setDoOutput(true);
DataOutputStream out = new DataOutputStream(

connection.getOutputStream() );


out.writeBytes( request );

out.flush();

out.close();
BufferedReader reader = new BufferedReader( new InputStreamReader(
connection.getInputStream() ) );
String response = reader.readLine();
while( null != response )

{

System.out.println( response );
response = reader.readLine();

}

Does anyone have any ideas?

Thank you,

Steve


Try appending a slash (/) to the URL you are using to refer to the

servlet. Tomcat might have decided to do a redirect with the slash appended. I have
seen this behaviour before with other servlet containers also suddenly
introduced with new release versions.

Silvio Bierman

Jul 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Nischal Topno | last post by:
Hi, Is there any way to auto load/instantiate a new class of a servlet into the JVM (with shutting the web server) after replacing the old class with new one? For e.g., after a servlet is...
2
by: Jon Dellaria | last post by:
I have been using MySql as the database using JSP's and JavaBeans but recently I have wanted to start using the database connection pooling mechanism built into TomCat. I think I am having a...
6
by: jajoo | last post by:
Hi, I have a problem with using *.jar files with Tomcat 5.0.16. I place the jar in the lib directory of the application but server throws Exception. java.lang.NoClassDefFoundError:...
1
by: SPG | last post by:
Hi, I have a servlet application that I am trying to write a very basic load tester for. There application has several servlets, but all rely on the same session being used (IE: For logged in...
15
by: Nathan | last post by:
I have an aspx page with a data grid, some textboxes, and an update button. This page also has one html input element with type=file (not inside the data grid and runat=server). The update...
1
by: kommaraju | last post by:
iam a starter to db2 & jdbc.i have a servlet program which connects to ibm db2 using jdbc.when i run this using apache tomcat 4.1.34 , it is showing a error message of HTTP STATUS 500 my jdbc...
1
by: kommaraju | last post by:
iam a starter to db2 & jdbc.i have a servlet program which connects to ibm db2 using jdbc.when i run this using apache tomcat 4.1.34 , it is showing a error message of HTTP STATUS 500 my jdbc...
3
by: arasub | last post by:
ep 20, 2007 11:25:57 AM org.apache.catalina.core.AprLifecycleListener lifecycleEvent INFO: The Apache Tomcat Native library which allows optimal performance in production environments was not found...
4
by: shivapadma | last post by:
1.I installed apache tomcat. 2.I have the following java servlet import java.io.*; import javax.sevlet.*; import javax.servlet.http.*; public class ser extends HttpServlet {
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.