I am trying to create a simple sign in screen that has an email field, a password field, and a sign in button (all three enclosed inside a <form/>
). When the user submits the form, the client does an AJAX POST (asynchronous so that the user doesn't have to "suffer" a page reload!) to a servlet. The servlet, for now, just does a dummy check and returns a "1" if the user successfully signed in, or a "0" if the email/password were bad. The client then handles the response (again, will be either 0 or 1).
What's happening is this: when I submit the form, the servlet receives the HTTP POST request, but the email/password fields are showing up as NULL:
public class SigninService extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) {
PrintWriter writer = null;
try {
writer = response.getWriter();
JSONSerializer serializer = new JSONSerializer();
System.out.println("\tFetching POST data...");
String email = request.getParameter("email");
System.out.println("\tEmail is " + email + "...");
String password = request.getParameter("password");
System.out.println("\tPassword is " + password + "...");
Integer answer = null;
// Dummy check; will replace with actual code once I get this example working...
if("testing@example.com".equals(email) && "12345".equals(password))
answer = 1;
else
answer = 0;
System.out.println("\tSerializing the HTTP response...");
String json = serializer.deepSerialize(answer);
System.out.println("\tSending response back to client...");
writer.write(json);
} catch (Throwable throwable) {
System.err.println(throwable.getMessage());
throwable.printStackTrace();
} finally {
writer.close();
}
}
}
The above code prints null
values for both email/password. I read several articles on extracting POST data from HttpServletRequest
(notably this article as well as this one), and none of them seem to be helping me. Both seem to suggest using a request.getReader()
method, but that isn't what I need. I need a way to directly access the form variables and their respective values.
I can't tell if my client code is incorrectly formatting the POST request and sending the form data in a way that the Java servlet can't handle, or if the Java servlet isn't coded correctly to handle the request.
When I run Firebug and submit the form, here are the contents of the Post
tab associated with the request:
-----------------------------18671963685702-some-really-long-number
Content-Disposition: form-data; name="email"
testing@example.com
-----------------------------18671963685702-another-really-long-number
Content-Disposition: form-data; name="password"
12345
-----------------------------18671963685702-and-another-really-long-number--
To an untrained eye this looks like the client is correctly sending a non-NULL email
variable, as well as a non-NULL password
variable, but again, I'm in unfamiliar territory here...
The bottom line
I need to first figure out whether this is a client- or server-side bug. This question is an attempt to either diagnose it as a server-side bug, or rule it out as such. So do any battle-weary servlet veterans see anything wrong the either the HTTP request, or the servlet code handling it? And if this is a server bug, what do I need to change to access my form variables?