芥末果酱

导航

2019.10.21

先设置网页直接跳转,设置登录界面,验证输入,设置mysql表格,导入数据库

package servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;

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 util.DBUtil;

/**
* Servlet implementation class RegisterServlet
*/
@WebServlet("/RegisterServlet")
public class RegisterServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public RegisterServlet() {
super();
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
*/

protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
request.setCharacterEncoding("UTF-8");
String sql = "insert into user (user, password, sex, name, num, email, scl, xi, class, year, province, phone) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
Connection connection = DBUtil.getConnection();
PreparedStatement ps = connection.prepareStatement(sql);
ps.setString(1,request.getParameter("user"));
ps.setString(2,request.getParameter("password"));
ps.setString(3,request.getParameter("sex"));
ps.setString(4,request.getParameter("name"));
ps.setString(5,request.getParameter("num"));
ps.setString(6,request.getParameter("email"));
ps.setString(7,request.getParameter("scl"));
ps.setString(8,request.getParameter("xi"));
ps.setString(9,request.getParameter("class"));
ps.setString(10,request.getParameter("year"));
ps.setString(11,request.getParameter("province"));
ps.setString(12,request.getParameter("phone"));
int updaterows = ps.executeUpdate(); //executeUpdate()返回的整型为受影响数据行数
DBUtil.close(ps, connection);
if(updaterows == 1)
response.sendRedirect("success.html");
else
response.sendRedirect("fail.html");
} catch (Exception e) {
e.printStackTrace();
}
}

}

package util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DBUtil {
public static String DBDRIVER = "com.mysql.cj.jdbc.Driver";
public static String DBURL = "jdbc:mysql://localhost:3306/shop";
public static String DBUSER = "root";
public static String DBPASSWORD = "root";
public static Connection getConnection() {
Connection conn = null;
try {
Class.forName(DBDRIVER);
conn = DriverManager.getConnection(DBURL, DBUSER, DBPASSWORD);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
public static void close(Statement state, Connection conn) {
try {
if(state!=null)
state.close();
if(conn!=null)
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void close(ResultSet rs, Statement state, Connection conn) {
try {
if(rs!=null)
rs.close();
if(state!=null)
state.close();
if(conn!=null)
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}

 

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>注册窗口</title>
</head>
<body>

<form action="RegisterServlet" method="post" onsubmit="return check(this)">
登录账号:<input type="text" name="user" placeholder="请输入账号"><br>
登录密码:<input type="password" name="password" placeholder="请输入密码"><br>
性别:<select name="sex">
<option value="男">男</option>
<option value="女">女</option>
</select>
<br>
姓名:<input type="text" name="name" placeholder="请输入姓名"><br>
学号:<input type="text" name="num" placeholder="请输入学号"><br>
电子邮箱:<input type="text" name="email" placeholder="请输入邮箱"><br>
所在学院:<input type="text" name="scl" placeholder="请输入学院"><br>
所在系:<input type="text" name="xi" placeholder="请输入系"><br>
所在班级:<input type="text" name="class" placeholder="请输入班级"><br>
入学年份:<select name="year">
<option value="1998">1998</option>
<option value="1999">1999</option>
<option value="2000">2000</option>
<option value="2001">2001</option>
<option value="2002">2002</option>
<option value="2003">2003</option>
<option value="2004">2004</option>
<option value="2005">2005</option>
<option value="2006">2006</option>
<option value="2007">2007</option>
<option value="2008">2008</option>
<option value="2009">2009</option>
<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>
</select>
届<br>
生源地:<input type="text" name="province" placeholder="请输入地址"><br>
备注:<input type="text" name="phone" placeholder="请输入"><br>
<input type="submit" value="登录">
<input type="reset" value="重置">
</form>

</body>
<script type="text/javascript">
function check(form){
if(form.user.value.length<6 || form.user.value.length>12 || new RegExp("[^0-9a-zA-Z]").test(form.user.value)){
alert("用户名必须由6-12位英文字符或数字组成!");
form.user.focus();
return false;
}
if(new RegExp("[^a-zA-Z]").test(form.user.value.substring(0,1))){
alert("用户名必须以英文字母开头!");
form.user.focus();
return false;
}
//var ses=/^([2]{1}})([0]{1}})([1]{1}})([8]{1}})([0-9]{4})$/
if(form.num.value.length != 8/* || ses.test(form.num.value)*/){
alert("学号请输8位数字!");
form.phone.focus();
return false;
}
//以数字字母开头,中间可以是多个数字字母或下划线;然后是“@”;然后是数字字母;然后是“.”;最后是2-4个字母结尾
var regex = /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/;
if(!regex.test(form.email.value)){
alert("邮箱格式错误!");
form.email.focus();
return false;
}
return true;
}
</script>
</html>

 

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>成功</title>
</head>
<body>

<h1>注册成功!</h1>
<a href="register.html">继续注册</a>

</body>
</html>

 

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>失败</title>
</head>
<body>

<h1>注册失败!</h1>
<a href="register.html">继续注册</a>

</body>
</html>

posted on 2019-10-21 21:52  芥末果酱  阅读(123)  评论(0编辑  收藏  举报