Question

I have the following Servlet:

// Sets the path to base URL + /servlet
@Path("/servlet") 
public class Servlet 
{ 

// This method is called if TEXT_PLAIN is request 
@POST 
@Produces(MediaType.TEXT_HTML) 
@Consumes(MediaType.APPLICATION_FORM_URLENCODED) 
public void storeData(@FormParam("data1") String data1, 
        @FormParam("data2") String data2, 
        @Context HttpServletResponse servletResponse) throws IOException 
{
 //Does stuff here
}
} 

I have the following Client:

//Create HTTP Client
DefaultHttpClient httpclient = new DefaultHttpClient();

// Use SSL
httpclient = sslClient(httpclient);

// Prepare the post statement
HttpPost httppost = new HttpPost(url);

// Add auth header with utility account information
httppost.addHeader("Authorization", "Basic " + Base64.encodeToString(("username" + ":"
            + "password").getBytes(), Base64.NO_WRAP));

postParameters.add(new BasicNameValuePair("data1", data1));
postParameters.add(new BasicNameValuePair("data2", data2));

httppost.setEntity(new UrlEncodedFormEntity(postParameters));

// Execute the post request
HttpResponse response = httpclient.execute(httppost);

Server Test Code in PHP - Grabs the string value for "Key" and outputs the value to a file

$content =$_POST['data1'];
$file = "newtext.txt";
$Saved_File = fopen($file, 'w');
fwrite($Saved_File, $content);
fclose($Saved_File);

The PHP test script works. It receives the post and I can view the data1 in the text file on the server. However the Servlet @POST method does not seem to get called.

Is there anyway to debug this? Any one have any ideas as how to get this to work? I am not sure if I should use @FormParam or @QueryParam or @HeaderParam. How does the @POST method get called?

Was it helpful?

Solution

Used postman plugin for Firefox to test and debug. Works great.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top