按照下面的页面做一个用户注册的Sevlet,要求自己设计表,并将表单的数据写入到表中。
表单代码如下:(register.html)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>学员信息管理</title>
<style>
td {
border-width: 1px;
border-style: solid;
}
</style>
<script type="text/javascript" src="jquery-3.2.1.js"></script>
<script type="text/javascript">
$(function() {
/**
* 增加验证学生身份证号是否存在功能
*/
$("#btn_create").click(function(){
var regName =/^[\u4e00-\u9fa5]{2,4}$/;
if(!regName.test($.trim($("#stuName").val()))){
alert('真实姓名填写有误');
$("#stuName").focus();
return ;
}
var regIdNo = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
if(!regIdNo.test($.trim($("#idcard").val()))){
alert('身份证号填写有误');
$("#idcard").focus();
return false;
}
var regGrade = /^[0-9]{4}$/;
if(!regGrade.test($.trim($("#grade").val()))){
alert('年级必须是一个四位的数字');
$("#grade").focus();
return false;
}
if(!(/^1\d{10}$/.test($.trim($("#tel").val())))){
alert("手机号码有误,请重填");
$("#tel").focus();
return false;
}
$("#form1").submit();
});
});
</script>
</head>
<body>
<form action="/save" method="post" id="form1">
<table border="1" align="center"
style="background-color: silver; font-size: 6px; border: solid; border-width: 1px">
<caption>学员信息管理</caption>
<tr>
<td>学生姓名</td>
<td><input type="text" name="name" id="stuName"></td>
<td>性别</td>
<td><input type="radio" name="sex" checked="checked" value="男">男<input
type="radio" name="sex" value="女">女</td>
<tr>
<tr>
<td>学历</td>
<td><select name="degree" id="degree" style="width: 150px">
<option>高中</option>
<option>专科</option>
<option>本科</option>
<option>硕士</option>
<option>博士</option>
</select></td>
<td>身份证号</td>
<td><input type="text" id="idcard" name="idcard" >
<span style="color:royalblue;font-size: medium"></span></td>
<tr>
<tr>
<td>班级</td>
<td><select name="tclass" id="classId" style="width: 150px">
<option>java</option>
<option>python</option>
<option>软件测试</option>
<option>大数据</option>
<option>web前端</option>
</select></td>
<td>讲师</td>
<td><select name="teacher" id="teacherId" style="width: 100px">
<option value="1">张三丰</option>
<option value="2">鬼谷子</option>
<option value="3">王阳明</option>
<option value="4">孔子</option>
<option value="5">孟子</option>
</select></td>
<tr>
<tr>
<td>学校</td>
<td><select name="school.schoolId" id="schoolId" style="width: 160px">
<option value="001">清华大学</option>
<option value="002">北京大学</option>
<option value="003">哈尔滨工业大学</option>
<option value="004">南京大学</option>
<option value="005">上海交通大学</option>
<option value="006">复旦大学</option>
<option value="007">中国科技大学</option>
<option value="008">浙江大学</option>
<option value="009">西安交通大学</option>
</select></td>
<td>专业</td>
<td><input type="text" name="major"></td>
<tr>
<tr>
<td>年级</td>
<td><input type="text" name="grade" id="grade"></td>
<td>联系电话</td>
<td><input type="text" name="tel" id="tel"></td>
<tr>
<tr>
<td>家庭住址</td>
<td><textarea rows="4" cols="30" name="address"></textarea></td>
<td>备注</td>
<td><textarea rows="4" cols="30" name="comment"></textarea></td>
<tr>
<tr align="right">
<td colspan="4"><input type="button" value="创建" id="btn_create"></td>
<tr>
</table>
</form>
</body>
</html>
配置servlet
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!--配置servlet-->
<servlet>
<!--为Servlet起一个名字,与Servlet类名无关-->
<servlet-name>saveServlet</servlet-name>
<!--配置Servlet的全类名(包名+类名)-->
<!--目的:让web服务器(tomcat)通过反射自动创建Servlet对象-->
<servlet-class>com.hyxy.servlet.web0529.servlet.SaveServlet</servlet-class>
</servlet>
<servlet-mapping>
<!--为这个Servlet配置映射地址-->
<servlet-name>saveServlet</servlet-name>
<!--映射的地址-->
<url-pattern>/save</url-pattern>
<url-pattern>/save/*</url-pattern>
</servlet-mapping>
</web-app>
方法一:向控制台输出
SaveServlet.java
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
public class SaveServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("UTF-8");
String name=req.getParameter("name");
String sex=req.getParameter("sex");
String degree=req.getParameter("degree");
String idcard=req.getParameter("idcard");
String tclass=req.getParameter("tclass");
String teacher=req.getParameter("teacher");
String school=req.getParameter("school.schoolId");
String major=req.getParameter("major");
String grade=req.getParameter("grade");
String tel=req.getParameter("tel");
String address=req.getParameter("address");
String comment=req.getParameter("comment");
System.out.println("姓名:"+name);
System.out.println("性别:"+sex);
System.out.println("学历:"+degree);
System.out.println("身份证:"+idcard);
System.out.println("班级:"+tclass);
System.out.println("讲师:"+teacher);
System.out.println("学校:"+school);
System.out.println("专业:"+major);
System.out.println("年级:"+grade);
System.out.println("联系电话:"+tel);
System.out.println("家庭住址:"+address);
System.out.println("备注:"+comment);
}
}
运行结果:
方法二、使用Dao向数据库写入数据
在数据库中创建login表
create table login(
name varchar(20) not null,
sex varchar(4),
degree varchar(10),
idcard varchar(30) not null,
class varchar(20),
teacher varchar(20),
school varchar(30),
major varchar(30),
grade varchar(10),
tel varchar(20) not null,
address varchar(50) not null,
comment varchar(100)
)ENGINE=MyISAM DEFAULT CHARSET=utf8;
User.java
public class User {
private String name;
private String sex;
private String degree;
private String idcard;
private String tclass;
private String teacher;
private String school;
private String major;
private String grade;
private String tel;
private String address;
private String comment;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getDegree() {
return degree;
}
public void setDegree(String degree) {
this.degree = degree;
}
public String getIdcard() {
return idcard;
}
public void setIdcard(String idcard) {
this.idcard = idcard;
}
public String getTclass() {
return tclass;
}
public void setTclass(String classes) {
this.tclass = classes;
}
public String getTeacher() {
return teacher;
}
public void setTeacher(String teacher) {
this.teacher = teacher;
}
public String getSchool() {
return school;
}
public void setSchool(String school) {
this.school = school;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
public String getGrade() {
return grade;
}
public void setGrade(String grade) {
this.grade = grade;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getComment() {
return comment;
}
public void setComment(String comment) {
this.comment = comment;
}
}
SaveServlet.java
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import com.hyxy.servlet.web0529.bean.User;
import com.hyxy.servlet.web0529.dao.SaveServletDao;
public class SaveServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("UTF-8");
String name=req.getParameter("name");
String sex=req.getParameter("sex");
String degree=req.getParameter("degree");
String idcard=req.getParameter("idcard");
String tclass=req.getParameter("tclass");
String teacher=req.getParameter("teacher");
String school=req.getParameter("school.schoolId");
String major=req.getParameter("major");
String grade=req.getParameter("grade");
String tel=req.getParameter("tel");
String address=req.getParameter("address");
String comment=req.getParameter("comment");
resp.setContentType("text/html;charset=UTF-8");
PrintWriter pw= resp.getWriter();
User user=new User();
user.setName(name);
user.setSex(sex);
user.setDegree(degree);
user.setIdcard(idcard);
user.setTclass(tclass);
user.setTeacher(teacher);
user.setSchool(school);
user.setMajor(major);
user.setGrade(grade);
user.setTel(tel);
user.setAddress(address);
user.setComment(comment);
int status = 0;
try {
status = SaveServletDao.save(user);
if (status > 0) {
pw.print("<script>alert('学员信息创建成功...');</script>");
}else {
pw.print("<script>alert('学员信息创建失败...');</script>");
}
} catch (Exception e) {
System.out.println(e);
}
pw.close();
}
}
SaveServletDao.java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import com.hyxy.servlet.web0529.bean.User;
public class SaveServletDao {
//表名
private static String tbName="login";
public static Connection getConnection(){
Connection con=null;
try {
Class.forName("com.mysql.jdbc.Driver");
con= DriverManager.getConnection("jdbc:mysql://localhost:3306/java2113?characterEncoding=utf8", "root", "root");
}catch (ClassNotFoundException e) {
e.printStackTrace();
}catch (SQLException e) {
e.printStackTrace();
}
return con;
}
//向表中插入记录
public static int save(User u){
int status=0;
try {
Connection con=SaveServletDao.getConnection();
String sql="insert into "+tbName+" (name,sex,degree,idcard,class,teacher,school,major,grade,tel,address,comment) values (?,?,?,?,?,?,?,?,?,?,?,?)";
PreparedStatement ps=con.prepareStatement(sql);
ps.setString(1,u.getName());
ps.setString(2,u.getSex());
ps.setString(3,u.getDegree());
ps.setString(4,u.getIdcard());
ps.setString(5,u.getTclass());
ps.setString(6,u.getTeacher());
ps.setString(7,u.getSchool());
ps.setString(8,u.getMajor());
ps.setString(9,u.getGrade());
ps.setString(10,u.getTel());
ps.setString(11,u.getAddress());
ps.setString(12,u.getComment());
status=ps.executeUpdate();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
return status;
}
}
运行结果:
数据库生成的表单
向浏览器返回创建成功