Source:
User.java
package Bean;
public class User {
private String username;
private String password;
private String sex;
private String name;
private String id;
private String email;
private String college;
private String department;
private String team;
private String year;
private String place;
private String remark;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getCollege() {
return college;
}
public void setCollege(String college) {
this.college = college;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public String getTeam() {
return team;
}
public void setTeam(String team) {
this.team = team;
}
public String getYear() {
return year;
}
public void setYear(String year) {
this.year = year;
}
public String getPlace() {
return place;
}
public void setPlace(String place) {
this.place = place;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
public User() { }
public User(String username,String password,String sex,String name,String id,String email,String college,String department,String team,String year,String place,String remark) {
this.username = username;
this.password = password;
this.sex = sex;
this.name = name;
this.sex = sex;
this.id = id;
this.email = email;
this.college = college;
this.department = department;
this.team = team;
this.year = year;
this.place = place;
this.remark = remark;
}
public String getPassword() {
return password;
}
}
private String username;
private String password;
private String sex;
private String name;
private String id;
private String email;
private String college;
private String department;
private String team;
private String year;
private String place;
private String remark;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getCollege() {
return college;
}
public void setCollege(String college) {
this.college = college;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public String getTeam() {
return team;
}
public void setTeam(String team) {
this.team = team;
}
public String getYear() {
return year;
}
public void setYear(String year) {
this.year = year;
}
public String getPlace() {
return place;
}
public void setPlace(String place) {
this.place = place;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
public User() { }
public User(String username,String password,String sex,String name,String id,String email,String college,String department,String team,String year,String place,String remark) {
this.username = username;
this.password = password;
this.sex = sex;
this.name = name;
this.sex = sex;
this.id = id;
this.email = email;
this.college = college;
this.department = department;
this.team = team;
this.year = year;
this.place = place;
this.remark = remark;
}
public String getPassword() {
return password;
}
}
Dao.java
package Dao;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.Statement;
import Bean.User;
import DBUtil.DBUtil;
import DBUtil.DBUtil;
public class Dao {
/*
* 添加新用户信息*/
public static void addusers(User user) {
Connection con = DBUtil.getConnection();
ResultSet rs = null;
Statement statement = null;
try {
String sql = "insert into user(username,password,sex,name,id,email,college,department,team,year,place,remark)"
+ " values('"+user.getUsername()+"','"+user.getPassword()+"','"+user.getSex()+"','"+user.getName()+"','"+user.getId()+"','"+user.getEmail()+"','"+user.getCollege()+"','"+user.getDepartment()+"','"+user.getTeam()+"','"+user.getYear()+"','"+user.getPlace()+"','"+user.getRemark()+"')";
statement = con.prepareStatement(sql);
statement.executeUpdate(sql);
System.out.println ( "new user registration success");
} the catch (Exception E) {
e.printStackTrace ();
} {the finally
DBUtil.close (CON);
DBUtil.close (RS);
DBUtil.close (Statement) ;
}
}
/*
* 添加新用户信息*/
public static void addusers(User user) {
Connection con = DBUtil.getConnection();
ResultSet rs = null;
Statement statement = null;
try {
String sql = "insert into user(username,password,sex,name,id,email,college,department,team,year,place,remark)"
+ " values('"+user.getUsername()+"','"+user.getPassword()+"','"+user.getSex()+"','"+user.getName()+"','"+user.getId()+"','"+user.getEmail()+"','"+user.getCollege()+"','"+user.getDepartment()+"','"+user.getTeam()+"','"+user.getYear()+"','"+user.getPlace()+"','"+user.getRemark()+"')";
statement = con.prepareStatement(sql);
statement.executeUpdate(sql);
System.out.println ( "new user registration success");
} the catch (Exception E) {
e.printStackTrace ();
} {the finally
DBUtil.close (CON);
DBUtil.close (RS);
DBUtil.close (Statement) ;
}
}
}DBUtil.java
package DBUtil;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DBUtil {
public static Connection getConnection() {
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
e.printStackTrace();
}
String user = "root";
String password = "123";
String url = "jdbc:mysql://localhost:3306/test? characterEncoding=utf-8&useSSL=true";
Connection connection = null;
try {
connection = DriverManager.getConnection(url,user,password);
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
public static void close(Connection connection ) {
try {
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void close(Statement Statement ) {
try {
if (Statement != null) {
Statement.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void close(ResultSet resultSet ) {
try {
if (resultSet != null) {
resultSet.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}Servlet.java
public static Connection getConnection() {
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
e.printStackTrace();
}
String user = "root";
String password = "123";
String url = "jdbc:mysql://localhost:3306/test? characterEncoding=utf-8&useSSL=true";
Connection connection = null;
try {
connection = DriverManager.getConnection(url,user,password);
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
public static void close(Connection connection ) {
try {
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void close(Statement Statement ) {
try {
if (Statement != null) {
Statement.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void close(ResultSet resultSet ) {
try {
if (resultSet != null) {
resultSet.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}Servlet.java
package Servlet;
import java.io.IOException;
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 javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import Bean.User;
import Dao.Dao;
import Dao.Dao;
/**
* Servlet implementation class Servlet
*/
@WebServlet("/Servlet")
public class Servlet extends HttpServlet {
private static final long serialVersionUID = 1L;
Dao dao = new Dao();
User name = new User();
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
String method = req.getParameter("method");
System.out.println(method);
if ("add".equals(method)) {//注册
System.out.println("ok");
add(req, resp);
}
}
private void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html;charset=utf8");
request.setCharacterEncoding("utf-8");
response.getWriter().append("Served at: ").append(request.getContextPath());
String username = request.getParameter("username");
String password = request.getParameter("password");
String sex = request.getParameter("sex");
String name = request.getParameter("name");
String id=request.getParameter("id");
String email = request.getParameter("email");
String college = request.getParameter("college");
String department = request.getParameter("department");
String team= request.getParameter("team");
String year= request.getParameter("year");
String place= request.getParameter("place");
String remark= request.getParameter("remark");
System.out.println(username+password+sex+name+id+email+college+department+team+year+place+remark);
User bean = new User(username,password,sex,name,id,email,college,department,team,year,place,remark);
Dao.addusers(bean);
//request.setAttribute("a", a);
request.getRequestDispatcher("login.jsp").forward(request, response);
}
/**
* @see HttpServlet#HttpServlet()
*/
public Servlet() {
super();
// TODO Auto-generated constructor stub
}
* Servlet implementation class Servlet
*/
@WebServlet("/Servlet")
public class Servlet extends HttpServlet {
private static final long serialVersionUID = 1L;
Dao dao = new Dao();
User name = new User();
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
String method = req.getParameter("method");
System.out.println(method);
if ("add".equals(method)) {//注册
System.out.println("ok");
add(req, resp);
}
}
private void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html;charset=utf8");
request.setCharacterEncoding("utf-8");
response.getWriter().append("Served at: ").append(request.getContextPath());
String username = request.getParameter("username");
String password = request.getParameter("password");
String sex = request.getParameter("sex");
String name = request.getParameter("name");
String id=request.getParameter("id");
String email = request.getParameter("email");
String college = request.getParameter("college");
String department = request.getParameter("department");
String team= request.getParameter("team");
String year= request.getParameter("year");
String place= request.getParameter("place");
String remark= request.getParameter("remark");
System.out.println(username+password+sex+name+id+email+college+department+team+year+place+remark);
User bean = new User(username,password,sex,name,id,email,college,department,team,year,place,remark);
Dao.addusers(bean);
//request.setAttribute("a", a);
request.getRequestDispatcher("login.jsp").forward(request, response);
}
/**
* @see HttpServlet#HttpServlet()
*/
public Servlet() {
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
response.getWriter().append("Served at: ").append(request.getContextPath());
}
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
login.jsp
<%@ 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">
<title>Insert title here</title>
</head>
<body>
<%
Object message = request.getAttribute("message");
if(message!=null && !"".equals(message)){
%>
<script type="text/javascript">
alert("<%=request.getAttribute("message")%>");
</script>
<%} %>
<div align="center">
<h1 style="color: red;">添加学生信息</h1>
<form action="Servlet?method=add" method="post" onsubmit="return check()">
<p>登录账号:<input name="username" id="username" type="text" placeholder="请输入用户名"></p>
<p>登录密码:<input name="password" id="password" type="password" placeholder="请输入密码" ></p>
<p>
性 别:
<input name="sex" id="sex" type="radio" value="男">男
<input name="sex" id="sex" type="radio" value="女">女
</p>
<p>姓 名:<input name="name" id="name" type="text" placeholder="请输入姓名"></p>
<p> Science & nbsp; & nbsp; & nbsp ; number: <input name = "id" id = "id" type = "text" placeholder = " Please enter the Student ID"> </ the p->
<p> Email: <input name = "email" id = " email" type = "text" placeholder = " Please enter the email"> </ the p->
<the p-> the colleges: <input name = "college" id = "college" type = "text "placeholder =" Please enter the colleges "> </ p>
<the p-> where the line: <input name =" department " id =" department "type =" text "placeholder =" Please fasten enter where "> </ p>
<p> where the class: <input name = "team" id = "team" type = "text" placeholder = " Please enter the class where"> </ the p->
<p>
<label> 年 份:</label>
<select name="year">
<option value="2010">2010</option>
<option value="2011">2011</option>
<option value="2012">2012</option>
<option value="2013">2013</option>
<option value="2014">2014</option>
<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>
</p>
<p>生源地:<input name="place" id="place" type="text" placeholder="请输入生源地"></p>
<p>备注:<input name="remark" id="remark" type="text" placeholder="请输入备注"></p>
<p><button type="submit">添加</button>
</p>
</form>
</div>
<script type="text/javascript">
function check() {
var username = document.getElementById("username");;
var password = document.getElementById("password");
var sex = document.getElementById("sex");;
var name = document.getElementById("name");
var id = document.getElementById("id");;
var email = document.getElementById("email");
var college = document.getElementById("college");;
var department = document.getElementById("department");
var team = document.getElementById("team");;
var year = document.getElementById("year");
var place = document.getElementById("place");;
var remark = document.getElementById("remark");
//非空
if (!username.value.match(/^[a-zA-Z]\w{5,11}$/)) {
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">
<title>Insert title here</title>
</head>
<body>
<%
Object message = request.getAttribute("message");
if(message!=null && !"".equals(message)){
%>
<script type="text/javascript">
alert("<%=request.getAttribute("message")%>");
</script>
<%} %>
<div align="center">
<h1 style="color: red;">添加学生信息</h1>
<form action="Servlet?method=add" method="post" onsubmit="return check()">
<p>登录账号:<input name="username" id="username" type="text" placeholder="请输入用户名"></p>
<p>登录密码:<input name="password" id="password" type="password" placeholder="请输入密码" ></p>
<p>
性 别:
<input name="sex" id="sex" type="radio" value="男">男
<input name="sex" id="sex" type="radio" value="女">女
</p>
<p>姓 名:<input name="name" id="name" type="text" placeholder="请输入姓名"></p>
<p> Science & nbsp; & nbsp; & nbsp ; number: <input name = "id" id = "id" type = "text" placeholder = " Please enter the Student ID"> </ the p->
<p> Email: <input name = "email" id = " email" type = "text" placeholder = " Please enter the email"> </ the p->
<the p-> the colleges: <input name = "college" id = "college" type = "text "placeholder =" Please enter the colleges "> </ p>
<the p-> where the line: <input name =" department " id =" department "type =" text "placeholder =" Please fasten enter where "> </ p>
<p> where the class: <input name = "team" id = "team" type = "text" placeholder = " Please enter the class where"> </ the p->
<p>
<label> 年 份:</label>
<select name="year">
<option value="2010">2010</option>
<option value="2011">2011</option>
<option value="2012">2012</option>
<option value="2013">2013</option>
<option value="2014">2014</option>
<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>
</p>
<p>生源地:<input name="place" id="place" type="text" placeholder="请输入生源地"></p>
<p>备注:<input name="remark" id="remark" type="text" placeholder="请输入备注"></p>
<p><button type="submit">添加</button>
</p>
</form>
</div>
<script type="text/javascript">
function check() {
var username = document.getElementById("username");;
var password = document.getElementById("password");
var sex = document.getElementById("sex");;
var name = document.getElementById("name");
var id = document.getElementById("id");;
var email = document.getElementById("email");
var college = document.getElementById("college");;
var department = document.getElementById("department");
var team = document.getElementById("team");;
var year = document.getElementById("year");
var place = document.getElementById("place");;
var remark = document.getElementById("remark");
//非空
if (!username.value.match(/^[a-zA-Z]\w{5,11}$/)) {
alert ( "user name from the six to twelve English alphanumeric and underscores beginning with a letter.");
username.focus();
return false;
}
else if (!password.value.match(/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,}$/)) {
else if (!password.value.match(/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,}$/)) {
alert ( "password by the eight above-numeric");
password.focus();
return false;
}
else if (!id.value.match(/^2018\d{4}$/)) {
else if (!id.value.match(/^2018\d{4}$/)) {
alert ( "student number is constituted by the first four and eight 2018");
id.focus();
return false;
}
else if (!email.value.match(/^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/)) {
else if (!email.value.match(/^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/)) {
alert ( "e-mail format error");
email.focus();
return false;
}
The else IF (sex.value == '') {
Alert ( 'empty sex');
sex.focus ();
return to false;
}
the else IF (name.value == '') {
Alert ( 'name is empty ');
name.focus ();
return to false;
}
the else IF (college.value ==' ') {
Alert (' empty's school ');
college.focus ();
return to false;
}
the else IF (Department. == value '') {
Alert ( 'empty lines where');
department.focus ();
return to false;
}
the else IF (Team. value == '') {
alert ( 'empty class where');
team.focus ();
return to false;
}
the else IF (Year.Value == '') {
alert ( 'empty year');
year.focus ();
return to false;
}
the else IF (place.value == '') {
Alert ( 'empty for students');
place.focus ();
return to false;
}
the else IF (remark.value ==' ') {
Alert (' notes for empty ');
remark.focus ();
return to false;
}
}
</ Script>
</ body>
</ HTML>
The else IF (sex.value == '') {
Alert ( 'empty sex');
sex.focus ();
return to false;
}
the else IF (name.value == '') {
Alert ( 'name is empty ');
name.focus ();
return to false;
}
the else IF (college.value ==' ') {
Alert (' empty's school ');
college.focus ();
return to false;
}
the else IF (Department. == value '') {
Alert ( 'empty lines where');
department.focus ();
return to false;
}
the else IF (Team. value == '') {
alert ( 'empty class where');
team.focus ();
return to false;
}
the else IF (Year.Value == '') {
alert ( 'empty year');
year.focus ();
return to false;
}
the else IF (place.value == '') {
Alert ( 'empty for students');
place.focus ();
return to false;
}
the else IF (remark.value ==' ') {
Alert (' notes for empty ');
remark.focus ();
return to false;
}
}
</ Script>
</ body>
</ HTML>
Screenshot: