JSP页面发布表格,即使它不应该 - 验证错误?

时间:2016-03-09 15:10:15

标签: javascript java html5 jsp

我在JSP页面上进行表单验证时遇到了一些问题。我尝试过JavaScript和HTML5验证。从浏览器查看时,HTML5验证(必需标记)有效,但在eclipse中不起作用。 Javascript验证不起作用 - 它确实显示警报窗口,但仍然发布到数据库。

Servlet代码(仅//创建相关的CUSTOMER)

  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 && !"".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);
    }
}

CreaterCustomer.JSP - page:

<%@ page import = "g24.isp.ejb.Customer" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" href="css/normalize.css">
        <link rel="stylesheet" href="css/stylesheet.css">
        <script type="text/javascript" src="js/javascript.js"></script>
        <title>Insert title here</title>
    </head>
<body>
    <div id="hogerbox">
        <h1></h1>
        <h2>Add Customer</h2>
        <p>
            <span class="boldtext">
                <form action="/HotelClient/HotelServlet" name="custForm" onsubmit="return validateForm()" method="post">
                    <p>Customer pnr:</p>
                    <input style="width: 223px;" type="text" name="txtcPnr" required>
                    <p>First Name:</p>
                    <input style="width: 223px;" type="text" name="txtcName" required>
                    <p>Phone Nbr:</p>
                    <input style="width: 223px;" type="text" name="txtcPhone">
                    <p>Address:</p>
                    <input style="width: 223px;" type="text" name="txtcAddress">
                    <p>
                        <input type="submit" name="submit" value="Create Customer">
                        <input name="operation" value="createcustomer" type="hidden">
                </form>
        </p>

    </div>
</body>
</html>

JS-功能

function validateForm() {
    var x = document.custForm.txtcPnr.value;
    if (x == null || x == "") {
        document.getElementByName("txtcPnr").value = "Please enter a cPnr"
        return false;
    }
 }

JS和HTML5在Eclipse中不起作用,因此我们尝试在servlet中进行解决方法。但它不会打印出“输入if语句”,但仍会创建客户。这里很无能为力,所以一些帮助将非常感激。

1 个答案:

答案 0 :(得分:0)

使用HTML5 doctype。看起来像是

<!DOCTYPE html>

...您可以放弃JavaScript验证,让HTML5为您完成工作。