title: JSP page posting form even though it shouldn't - validation error?
body: <p>I am having some trouble with form validation on my JSP-pages. I've tried with both JavaScript and HTML5 validation. The HTML5 validation (required tag) works when viewing from a browser, but doesn't work in eclipse. The Javascript validation doesn't work - it does show the alert window, but is still posting to the database. </p>
  
  <p>Servlet code (Only //CREATE CUSTOMER that's relevant)</p>
  
  <pre><code>  package g24.isp.servlets;
  
  import java.io.IOException;
  import java.io.PrintWriter;
  import java.util.ArrayList;
  import java.util.List;
  
  import javax.ejb.EJB;
  import javax.servlet.RequestDispatcher;
  import javax.servlet.ServletException;
  import javax.servlet.annotation.WebServlet;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  
  import g24.isp.ejb.Cabin;
  import g24.isp.ejb.Customer;
  import g24.isp.ejb.Hotel;
  import g24.isp.ejb.Reservation;
  import g24.isp.facade.Facade;
  import g24.isp.facade.FacadeLocal;
  import g24.isp.ejb.MethodClass;
  
  /**
   * Servlet implementation class HotelServlet
   */
  @WebServlet("/HotelServlet")
  public class HotelServlet extends HttpServlet {
      private static final long serialVersionUID = 1L;
  
      @EJB
      private FacadeLocal facade;
  
      /**
       * @see HttpServlet#HttpServlet()
       */
      public HotelServlet() {
          super();
          // TODO Auto-generated constructor stub
      }
  
      /**
       * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
       *      response)
       */
      protected void doGet(HttpServletRequest request, HttpServletResponse response)
              throws ServletException, IOException {
          // TODO Auto-generated method stub
          PrintWriter out = response.getWriter();
          out.println("MainServlet-doGet");
          out.close();
      }
  
      /**
       * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
       *      response)
       */
      protected void doPost(HttpServletRequest request, HttpServletResponse response)
              throws ServletException, IOException {
          // TODO Auto-generated method stub
          String url = null;
          // Get hidden field
          String operation = request.getParameter("operation");
  
          if (operation.equals("showperson")) {
              System.out.println("MainServlet-show reservation");
  
              MethodClass mc = new MethodClass();
              String cNoString = request.getParameter("cabinTxt");
              int cNo = mc.ParseStringToInt(cNoString);
  
              if (cNo == Integer.MIN_VALUE) {
                  System.out.println("Du måste mata in en siffra");
                  url = "/Index.jsp";
              }
  
              else {
                  Cabin c = facade.findByCabinNo(cNo);
                  if (c != null) {
                      request.setAttribute("cabin", c);
                      url = "/Admin.jsp";
                  }
  
                  else {
                      url = "/Index.jsp";
                  }
  
              }
  
              // SEARCH PERSON
          } else if (operation.equals("searchperson")) {
              System.out.println("MainServlet-searchperson");
              url = "/Index.jsp";
          }
  
          else if (operation.equals("createcustomer")) {
  
              String cPnr = request.getParameter("txtcPnr");
              String cAddress = request.getParameter("txtcAddress");
              String cPhone = request.getParameter("txtcPhone");
              String cName = request.getParameter("txtcName");
  
              if (cPnr != null &amp;&amp; !"".equals(cPnr)) {
                  System.out.println("Entering if-statement");
                  Customer customer = new Customer();
                  customer.setcPnr(cPnr);
                  customer.setcAddress(cAddress);
                  customer.setcPhone(cPhone);
                  customer.setcName(cName);
  
                  request.setAttribute("customer", customer);
  
                  url = "/Index.jsp";
                  System.out.println(url);
                  facade.createCustomer(customer);
                  System.out.println("Customer created");
              }
  
          }
          System.out.println(url);
          RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(url);
          dispatcher.forward(request, response);
      }
  }
  </code></pre>
  
  <p>CreaterCustomer.JSP - page: </p>
  
  <pre><code>&lt;%@ page import = "g24.isp.ejb.Customer" %&gt;
  &lt;%@ page language="java" contentType="text/html; charset=UTF-8"
      pageEncoding="UTF-8"%&gt;
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
  &lt;html&gt;
      &lt;head&gt;
          &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;
          &lt;link rel="stylesheet" href="css/normalize.css"&gt;
          &lt;link rel="stylesheet" href="css/stylesheet.css"&gt;
          &lt;script type="text/javascript" src="js/javascript.js"&gt;&lt;/script&gt;
          &lt;title&gt;Insert title here&lt;/title&gt;
      &lt;/head&gt;
  &lt;body&gt;
      &lt;div id="hogerbox"&gt;
          &lt;h1&gt;&lt;/h1&gt;
          &lt;h2&gt;Add Customer&lt;/h2&gt;
          &lt;p&gt;
              &lt;span class="boldtext"&gt;
                  &lt;form action="/HotelClient/HotelServlet" name="custForm" onsubmit="return validateForm()" method="post"&gt;
                      &lt;p&gt;Customer pnr:&lt;/p&gt;
                      &lt;input style="width: 223px;" type="text" name="txtcPnr" required&gt;
                      &lt;p&gt;First Name:&lt;/p&gt;
                      &lt;input style="width: 223px;" type="text" name="txtcName" required&gt;
                      &lt;p&gt;Phone Nbr:&lt;/p&gt;
                      &lt;input style="width: 223px;" type="text" name="txtcPhone"&gt;
                      &lt;p&gt;Address:&lt;/p&gt;
                      &lt;input style="width: 223px;" type="text" name="txtcAddress"&gt;
                      &lt;p&gt;
                          &lt;input type="submit" name="submit" value="Create Customer"&gt;
                          &lt;input name="operation" value="createcustomer" type="hidden"&gt;
                  &lt;/form&gt;
          &lt;/p&gt;
  
      &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>JS-function</p>
  
  <pre><code>function validateForm() {
      var x = document.custForm.txtcPnr.value;
      if (x == null || x == "") {
          document.getElementByName("txtcPnr").value = "Please enter a cPnr"
          return false;
      }
   }
  </code></pre>
  
  <p>The JS nor HTML5 doesn't work in Eclipse, so we tried to do a workaround in the servlet. But it doesn't print out the "Entering if-statement" but still creates the customer. So clueless here, so some help would be much appreciated. </p>