下面是我的 JSP 文件,我使用 servlet 检查字段是否为空,但显然这需要在客户端完成。当按下提交按钮并且字段为空时,如何防止表单提交?
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page session="false" %>
<!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=ISO-8859-1">
<style type="text/css">
h1 { font-family : cooper black;}
</style>
<title>Add a Phonebook Entry</title></head>
<body>
<h1> Add a Phonebook Entry</h1>
<form action="process.do" method="GET">
<table summary="phonebook entry table">
<tr>
<td>Last name:</td>
<%
String last = "";
String first = "";
String phone = "";
if(request.getParameter("surname")!=null){
last = (String) request.getParameter("surname");
}
if(request.getParameter("firstname")!=null){
first = (String) request.getParameter("firstname");
}
if(request.getParameter("phone")!=null){
phone = (String) request.getParameter("phone");
}
%>
<td><input type="text" title="enter your last name here" name="surname" name="initials" size=10 maxlength=10 value=<%= last %> ></td>
</tr>
<tr>
<td>First name:</td>
<td><input type="text" name="firstname" size=10 maxlength=10 value=<%=first %> ></td>
</tr>
<tr>
<td>Phone number: </td>
<td><input type="text" name="phone" maxlength=10 size=10 value=<%=phone %> > </td>
</tr>
</table>
<br><input type="submit" value="Submit">
</form>
<p> View the phonebook <a href="./Display.jsp">here</a></p>