4

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?

halfer
  • 19,824
  • 17
  • 99
  • 186
IAmYourFaja
  • 55,468
  • 181
  • 466
  • 756
  • 1
    The server code seems ok. Can you show the client code? You can try to submit your form without Ajax as a normal POST to check if your server code is ok. – vanje Dec 24 '13 at 13:50
  • Thanks @kuporific (+1) - I'm not opposed to showing the client code, but think about it: if I didn't name the inputs, how is the HTTP POST showing a variable named `email` and another one named `password`?!? – IAmYourFaja Dec 24 '13 at 13:52
  • Thanks @vanje (+1) - please see my comment underneath palayan's answer - I have the same question for you! – IAmYourFaja Dec 24 '13 at 13:53

1 Answers1

1

When you're submitting the form via ajax are you sending the data in JSON format? if you are then, you should explicitly fetch the data in your jquery or js file from the userId and password box and then send it to the server in JSON format. That would fix the problem.

palayan
  • 59
  • 6
  • Thanks @palayan (+1) - **No**, I'm *not* submitting the form data as JSON, and that could very well fix the problem, so thank you! However it leaves me interested as to *why* it might fix the problem. Is it that the server is getting the request body as one big blob of text, and I need to use `request.getReader()` to manually parse that text blob for key-value pairs? Is that why formatting the data in JSON would help the server parse the request body? Thanks again! – IAmYourFaja Dec 24 '13 at 13:52
  • 1
    Yes, it looks like my suspicions were correct. It looks like you need to use `request.getReader()` to read the contents of the request body, **line by line** (grrrr....thanks J2EE), and then come up with your own paradigm for parsing out key-value pairs...alternatively, like @palayan here suggests, I could force the client to send a custom JSON object that contains the email and password, and then use GSON or FlexJSON on the server to deserialize the JSON object into a POJO. So that's what I'll do! – IAmYourFaja Dec 24 '13 at 14:05
  • json type data format would be much easier to use if there is no constraint over sending the data in json format at that point of your application. – palayan Jan 02 '14 at 11:58