5
package lzx;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/LzxServlet")
public class LzxServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
LzxService service = new LzxService();
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
String method = req.getParameter("method");
if ("add".equals(method)) {
add(req, resp);
}
}
/**
* 添加
* @param req
* @param resp
* @throws IOException
* @throws ServletException
*/
private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
req.setCharacterEncoding("utf-8");
String zhanghao = req.getParameter("zhanghao");
String password = req.getParameter("password");
String sex = req.getParameter("sex");
String number = req.getParameter("number");
String name = req.getParameter("name");
String mail = req.getParameter("mail");
String xueyuan = req.getParameter("xueyuan");
String zhuanye = req.getParameter("zhuanye");
String banji = req.getParameter("banji");
String year = req.getParameter("year");
String shengyuandi = req.getParameter("shengyuandi");
String beizhu = req.getParameter("beizhu");
Lzx zzm= new Lzx(zhanghao,password,sex,name,number,mail,xueyuan,zhuanye,banji,year,shengyuandi,beizhu);
//添加后消息显示
if(service.add(zzm)) {
req.setAttribute("message", "添加成功");
req.getRequestDispatcher("zzm.jsp").forward(req,resp);
} else {
req.setAttribute("message", "用户名重复,请重新输入");
req.getRequestDispatcher("zzm.jsp").forward(req,resp);
}
}
}
6写页面
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<link rel="stylesheet" type="text/css" href="css/nav.css">
<link rel="stylesheet" type="text/css" href="font/iconfont.css">
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/nav.js"></script>
</head>
<body>
<%
Object message = request.getAttribute("message");
if(message!=null && !"".equals(message)){
%>
<script type="text/javascript">
alert("<%=request.getAttribute("message")%>");
</script>
<%} %>
<div align="center">
<form action="LzxServlet?method=add" method="post" onsubmit="return check()">
<table width="500px" height="650px"border="1" style="text-align:center">
<tr height="50px" align="center" bgcolor="#FFFFCC">
<td width="100"><font color="black"style="font-family:宋体;">登录账号:</font></td>
<td width="400"><input id="zhanghao" name="zhanghao" placeholder="请输入账号"/></td>
</tr>
<tr height="50px" align="center" bgcolor="#FFFFCC">
<td width="100"><font color="black"style="font-family:宋体;">登录密码:</font></td>
<td width="400"><input type="password" name="password" placeholder="请输入密码"/></td>
</tr>
<tr height="50" align="center" bgcolor="#FFFFCC">
<td width="100"><font color="black"style="font-family:宋体;">性别:</font></td>
<td><select id="sex" name="sex" >
<option value="男">男</option>
<option value="女">女</option>
<select></td>
</tr>
<tr height="50px" align="center" bgcolor="#FFFFCC">
<td width="100"><font color="black"style="font-family:宋体;">姓名:</font></td>
<td width="400"><input id="name" name="name" placeholder="请输入姓名"/></td>
</tr>
<tr height="50" align="center" bgcolor="#FFFFCC">
<td width="100"><font color="black"style="font-family:宋体;">学号:</font></td>
<td width="400"><input id="number" name="number" placeholder="请输入学号"/></td>
</tr>
<tr height="50" align="center" bgcolor="#FFFFCC">
<td width="100"><font color="black"style="font-family:宋体;">电子邮箱:</font></td>
<td width="400"><input id="mail" name="mail" placeholder="请输入邮箱" /></td>
</tr>
<tr height="50" align="center" bgcolor="#FFFFCC">
<td width="100"><font color="black"style="font-family:宋体;">所在学院:</font></td>
<td width="400"><input id="xueyuan" name="xueyuan" placeholder="请输入学院名" /></td>
</tr>
<tr height="50" align="center" bgcolor="#FFFFCC">
<td width="100"><font color="black"style="font-family:宋体;">所在专业:</font></td>
<td width="400"><input id="zhuanye" name="zhuanye" placeholder="请输入专业" /></td>
</tr>
<tr height="50" align="center" bgcolor="#FFFFCC">
<td width="100"><font color="black"style="font-family:宋体;">所在班级:</font></td>
<td width="400"><input id="banji" name="banji" placeholder="请输入班级" /></td>
</tr>
<tr height="50" align="center" bgcolor="#FFFFCC">
<td width="100"><font color="black"style="font-family:宋体;">入学年份:</font></td>
<td><select id="year" name="year" >
<option value="2015">2015</option>
<option value="2016">2016</option>
<option value="2017">2017</option>
<option value="2018">2018</option>
<option value="2019">2019</option>
<select></td>
</tr>
<tr height="50" align="center" bgcolor="#FFFFCC">
<td width="100"><font color="black"style="font-family:宋体;">生源地:</font></td>
<td width="400"><input id="shengyuandi" name="shengyuandi" placeholder="请输入生源地" /></td>
</tr>
<tr height="100" align="center" bgcolor="#FFFFCC">
<td width="100"><font color="black"style="font-family:宋体;">备注:</font></td>
<td width="400"><input id="beizhu" name="beizhu" placeholder="无" /></td>
</tr>
<tr height="50" align="center" bgcolor="#FFFFCC">
<td width="400"><button type="submit" >添加<button></td>
<td width="400"><input type="reset" value="重新输入" /></td>
</tr>
</form>
</div>
<script type="text/javascript">
function check() {
var zhanghao = document.getElementById("zhanghao");
var password = document.getElementById("password");
var number = document.getElementById("number");
var mail = document.getElementById("mail");
if (!zhanghao.value.match(/^[a-zA-Z]\w{5,11}$/)) {
alert("用户名由六到十二英文字符和数字组成,以英文字母开头");
num.focus();
return false;
}
else if (!password.value.match(/^[A-Za-z0-9]\w{7,20}$/)) {
alert(" 密码由八 位 以上英文 和数 字 组成");
mima.focus();
return false;
}
else if(number.value<"20180000"|| number.value>"20189999")
{
alert(" 学号由2018开头的八位组成");
number.focus();
return false;
}
else if (!mail.value.match(/^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/)) {
alert(" 邮箱格式错误");
mail.focus();
return false;
}
}
</script>
</body>
</html>