5

I have a jsp page with a form. After submitting it is calling a httpservlet class. But all getParamter() operations are returning null. What am I doing wrong?

JSP

<form action="GatherController" method="post">
    <input type='text' name="a"/>
    <input type='date' name="b" />
    ...
    <input type="submit" />
</form>

Servlet

@WebServlet(name = "GatherController", urlPatterns = { "/GatherController" })
public class GatherController extends HttpServlet {

    ...

    @Override
    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {


        String a = request.getParameter("a");
        System.out.println(a);  

        ... 

    }
}

Edit

-I am using Tomcat v8.0

-doPost(...) method is executed, I am getting an output with System.out.println(a); which is null

timbmg
  • 3,192
  • 7
  • 34
  • 52
  • 1
    what is the server you're using ? Are you absolutely positive that on form submit the control is coming your GatherController's doPost method ? – Arkantos Apr 22 '15 at 07:59
  • did you check with b also? what is the b value? – Giovanni Apr 22 '15 at 08:02
  • For the servlet container to register your controller, GatherController.class should be inside `WEB-INF/classes` directory.. I hope it's already there :) – Arkantos Apr 22 '15 at 08:03
  • 1
    I have checked your code and it worked for me I used `out.println(a)` (which print in the browser) for printing instead of `System.out.println(a); ` (which prints in console) – singhakash Apr 22 '15 at 08:03
  • @Arkantos how do I register my controller? – timbmg Apr 22 '15 at 08:09
  • like i said already in my previous comments, you need to keep your GatherController class inside WEB-INF/classes directory. If you're on tomcat, then it's something like `tomcat-directory/webapps/your-app/WEB-INF/classes/your-package/GatherController.class` – Arkantos Apr 22 '15 at 08:13
  • Did you fill values before submit? Try hard coded value attribute for input tags. Also monitor the browser console on the net tab and watch the parameters being sent –  Apr 22 '15 at 08:21
  • @blckbird Are you sure that the above code doesnt print the desired output in the console. seems correct to me . why dont you try deploying it again ? – Santhosh Apr 22 '15 at 08:56
  • I know this is weird but are you actually typing any value in the text box before submitting the form ;) ? – Arkantos Apr 22 '15 at 09:20
  • Do you have > 1 input named 'a'? If so, you ought to be getting back the 1st val in the collection it finds named 'a'. No guarantee it'll be the one you have typed a value. Give the form a name just for the heck of it. Also make sure there are no other 'a'-named objects on the page or in that form (i.e., you may have a radio button named 'a' and that could be messing up things as well. Also noticed you have an input type of 'date' for one form element. Your browser may not support that and so may be messing up the submitted values. Try getting rid of anything like that from your form. – Matt Campbell May 28 '15 at 22:11

3 Answers3

1

I have no enough reputation to put comments, so I put it as answer if you don't mind.

Please make sure that you don't call other methods on httpServletRequest before, like getReader() or getInputStream(). You can't access your post parameters after these calls.

Anptk
  • 1,125
  • 2
  • 17
  • 28
androberz
  • 744
  • 10
  • 25
-1
<form action="GatherController" method="post"><input type="text" name="a"/>

Please use double quotes,it might work

Dhivya
  • 152
  • 1
  • 2
  • 13
-1
<html>
<h1>Register</h1><br>
    <body>
        <form name="userRegistration" method="post">
            <input type="text" name="firstname"  class="textbox" required/>
            <input type="text" name="lastname" class="textbox" required/>
            <input type="text" name="email" class="textbox" required/>
        </form>
    </body>
 </html>

servlet code

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    response.setContentType("text/html");
    PrintWriter out = response.getWriter();

    String firstname = request.getParameter("firstname");
    String lastname = request.getParameter("lastname");
    String email = request.getParameter("email");


    HttpSession session = request.getSession(false);

    if (//handle your logic) {
        out.print("<p style=\"color:red\">Account Created</p>");
        RequestDispatcher rd = request.getRequestDispatcher("index.jsp");
        rd.forward(request, response);
    } else {
        out.print("<p style=\"color:red\">Error Occured </p>");
        RequestDispatcher rd = request.getRequestDispatcher("newuser.jsp");
        rd.include(request,response);
    }
}
Shalika
  • 1,457
  • 2
  • 19
  • 38