Dlu.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>登录</title>
<style>
tr{
height:50px;
}
td{
text-align: center;
}
</style>
</head>
<body>
<form action="myServlet02" method="get">
<center>
<table>
<h1>注册</h1>
<p>账号:<input type="text" name="username"/></p>
<p>密码:<input type="password" name="password"/></p>
<p>确认密码:<input type="text" name="confirmpwd"/></p>
<p>性别:<input type="radio" name="sex"/>男<input type="radio" name="sex"/>女</p>
<p>
爱好:<input type="checkbox" name="hobby"/>足球
<input type="checkbox" name="hobby"/>篮球
<input type="checkbox" name="hobby"/>电影
<input type="checkbox" name="hobby"/>游戏
</p>
<p>
籍贯:<select name="province">
<option>山东省</option>
<option>山西省</option>
<option>福建省</option>
<option>江西省</option>
<option>江苏省</option>
<option>广东省</option>
</select>
</p>
<p>个性签名:
<textarea rows="3" cols="30" name="signature"></textarea></p>
<p><input type="submit" value="注册" ></p>
</table>
</center>
</form>
</body>
</html>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<welcome-file-list>
<welcome-file>Dlu.html</welcome-file>
</welcome-file-list>
</web-app>
Servlet02.java
import org.apache.commons.dbutils.QueryRunner;
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 java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
@WebServlet("/myServlet02")
public class Servlet02 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String username = request.getParameter("username");
String password= request.getParameter("password");
String confirmpwd = request.getParameter("confirmpwd");
String sex = request.getParameter("sex");
String province = request.getParameter("province");
String signature = request.getParameter("signature");
String[] hobbies = request.getParameterValues("hobby");
String hobbyStr = "";
for (String hobby : hobbies){
hobbyStr += hobby+"";
}
System.out.println(hobbyStr);
QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
String sql = "insert into t_user values(null,?,?,?,?,?,?)";
try {
qr.update(sql,username,password,sex,hobbyStr,province,signature);
} catch (SQLException e) {
e.printStackTrace();
}
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.println("<h1>注册成功!</h1>");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
DruidUtils.java
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class DruidUtils {
//Druid德鲁伊,据说是魔兽世界中的一个角色,森林女神
public static DruidDataSource dataSource;//数据库连接池
//1.初始化Druid连接池
static {
//第二种方式:使用软编码通过配置文件初始化
try {
Properties properties = new Properties();
//通过类加载器加载配置文件
InputStream inputStream = DruidUtils.class.getClassLoader().getResourceAsStream("druid.properties");
properties.load(inputStream);
//创建连接池
dataSource = (DruidDataSource) DruidDataSourceFactory.createDataSource(properties);
} catch (Exception e) {
e.printStackTrace();
}
}
druid.properties
#连接设置
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/db_book
username=root
password=root
#<!-- 初始化连接 -->
initialSize=10
#最大连接数量
maxActive=50
#<!-- 最小空闲连接 -->
minIdle=5
#<!-- 超时等待时间以毫秒为单位 6000毫秒/1000等于60秒 -->
maxWait=6000