Javaweb第二章课后习题
编写一个雇员注册的表单,要求输入以下内容:雇员编号、雇员姓名、雇员工作、雇佣日期、基本工资和奖金。并对表单进行JavaScript验证,验证要求如下:
1. 雇员编号:只能是数字;
2. 雇员姓名:不能为空;
3. 雇员工作:不能为空;
4. 雇佣日期:必须是日期格式,即2018-10-13;
5. 基本工资:必须是数字(小数);
6. 奖金:必须是数字(小数)。
思路:先写一个html表单文件,进行JavaScript验证,并把表单内容提交给jsp文件,然后建立一个class类加入无参构造,最后写一个jsp文件接收并显示内容。代码如下:
register.html
<!DOCTYPE html>
<html>
<head>
<meta charset="GBK">
<title>雇员注册表单</title>
<script type="text/javascript">
function Check(){
if(document.getElementById('empno').value.length==0)
{
alert("编号不能为空!");
document.getElementById('empno').focus();
return false;
}
var value = document.getElementById("empno").value;
var reg=/^[1-9]\d*$|^0$/;
if(reg.test(value)!=true)
{
alert("编号必须为数字!");
document.getElementById('empno').focus();
return false;
}
if(document.getElementById('empname').value.length==0)
{
alert("姓名不能为空!");
document.getElementById('empname').focus();
return false;
}
if(document.getElementById('empjob').value.length==0)
{
alert("工作不能为空!");
document.getElementById('empjob').focus();
return false;
}
if(!document.getElementById("empdate").value.match("^[0-9]{4,4}-[0-9]{2,2}-[0-9]{2,2}$"))
{
alert( "日期格式错误 xxxx-xx-xx");
document.getElementById('empdate').focus();
return false;
}
if(!document.getElementById("empsal").value.match("^[0-9]{1,9}.[0-9]{2,2}$"))
{
alert( "工资格式错误x...x.xx ");
document.getElementById('empsal').focus();
return false;
}
if(!document.getElementById("empbond").value.match("^[0-9]{1,9}.[0-9]{2,2}$"))
{
alert( "奖金格式错误x...x.xx ");
document.getElementById('empbond').focus();
return false;
}
}
</script>
</head>
<body>
<form action="get.jsp" method="post" name="emp"
onSubmit="return Check()">
雇员编号:<input type="number" id="empno" name="empno"><br>
雇员姓名:<input type="text" id="empname" name="empname"><br>
雇员工作:<input type="text" id="empjob" name="empjob"><br>
雇佣日期:<input type="text" id="empdate" name="empdate"><br>
基本工资:<input type="text" id="empsal" name="empsal"><br>
奖 金:<input type="text" id="empbond" name="empbond"><br>
<input type="submit" value="提交"><input type="reset" value="重置"><br>
</form>
</body>
</html>
SimpleBean.java
package demo;
public class SimpleBean {
private String empno;
private String empname;
private String empjob;
private String empdate;
private String empsal;
private String empbond;
public String getEmpno() {
return empno;
}
public void setEmpno(String empno) {
this.empno = empno;
}
public String getEmpname() {
return empname;
}
public void setEmpname(String empname) {
this.empname = empname;
}
public String getEmpjob() {
return empjob;
}
public void setEmpjob(String empjob) {
this.empjob = empjob;
}
public String getEmpdate() {
return empdate;
}
public void setEmpdate(String empdate) {
this.empdate = empdate;
}
public String getEmpsal() {
return empsal;
}
public void setEmpsal(String empsal) {
this.empsal = empsal;
}
public String getEmpbond() {
return empbond;
}
public void setEmpbond(String empbond) {
this.empbond = empbond;
}
}
get.jsp
<%@ page language="java" contentType="text/html; charset=GBK"
pageEncoding="GBK"%>
<%@ page import="demo.*" %>
<!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=GBK">
<title>获取表单</title>
</head>
<% request.setCharacterEncoding("GBK"); %>
<body>
<%
SimpleBean simple = new SimpleBean();
simple.setEmpno(request.getParameter("empno"));
simple.setEmpname(request.getParameter("empname"));
simple.setEmpjob(request.getParameter("empjob"));
simple.setEmpdate(request.getParameter("empdate"));
simple.setEmpsal(request.getParameter("empsal"));
simple.setEmpbond(request.getParameter("empbond"));
%>
<h3>雇员编号:<%=simple.getEmpno() %></h3>
<h3>雇员姓名:<%=simple.getEmpname() %></h3>
<h3>雇员工作:<%=simple.getEmpjob() %></h3>
<h3>雇佣日期:<%=simple.getEmpdate() %></h3>
<h3>基本工资:<%=simple.getEmpsal() %></h3>
<h3>奖 金:<%=simple.getEmpbond() %></h3>
</html>
运行结果如下: