jspservlets

When will a submit button in a jsp file return a "null" value in the servlet?


I am trying to understand if I have a fill.jsp file with the form below:

<form action="calculate" method="post">
  <input id="userName" type="text" name="userName">
  <input id="grossPay" type="number" name="grossPay">
  <input id="noOfDependents" type="number" name="noOfDependents">
  <input id="btn" type="submit" name="submit" value="Submit">
</form>

and I have a servlet with the below method:

protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
   response.setContentType("text/html;charset=UTF-8");
   String url = "/fill.jsp";
   String buttonValue = request.getParameter("submit");

   if(buttonValue != null) {
     // do something here
   }
}

when will the buttonValue == null? Every time, I am clicking the button I get the value, "Submit". I am unable to understand when will I ever get a "null" value for the button?


Solution

  • Under normal circumstances, you will never get null for the button value.

    This is because the only way to submit the form is by clicking the Submit button.

    Furthermore, considering that it is a POST request, it really is the only way to even call the servlet.

    However, if the user modifies the button name in the console, then that is one possibility in which it will return null.

    EDIT: It is possible to call the POST servlet request, if someone was to attempt an XSS attack, or submit a form to the POST method of the servlet.