package com.bjsxt.registration.model;

//贫血模型 充血模型
public class User {
private Integer id;
private String username;
private String password;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getpassword() {
return password;
}

public void setpassword(String password) {
this.password = password;
}

}


package com.bjsxt.registration.service;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.bjsxt.registration.model.User;

public class UserManager {
public boolean exists(User u) throws Exception {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/spring", "root", "root");

String sqlQuery = "select count(*) from user where username = ?";
PreparedStatement psQuery = conn.prepareStatement(sqlQuery);
psQuery.setString(1, u.getUsername());
ResultSet rs = psQuery.executeQuery();
rs.next();
int count = rs.getInt(1);
psQuery.close();
conn.close();
if (count > 0) {
return true;
}
return false;
}

public void add(User u) throws Exception {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/spring", "root", "root");

String sql = "insert into user values (null, ?, ?)";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, u.getUsername());
ps.setString(2, u.getpassword());
ps.executeUpdate();
ps.close();
conn.close();
}
}


<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

<title>用户注册</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">

</head>

<body>
<h1>用户注册</h1><br/>
<form action="registDeal.jsp" method="post">
用户名:<input type="text" name="username"/><br/>
密码:<input type="password" name="password"/><br/>
确认密码:<input type="password" name="password2"/><br/>
<input type="submit" value=" 提 交 "/>
</form>
</body>
</html>
<%@ page language="java" import="java.util.*, java.sql.*"
pageEncoding="GB18030"%>
<%@ page import="com.bjsxt.registration.model.User"%>
<%@ page import="com.bjsxt.registration.service.UserManager"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
String username = request.getParameter("username");
String password = request.getParameter("password");
String password2 = request.getParameter("password2");

User u = new User();
u.setUsername(username);
u.setpassword(password);

UserManager um = new UserManager();
boolean exits = um.exists(u);
if (exits) {
response.sendRedirect("registFail.jsp");
return;
}

um.add(u);
response.sendRedirect("registSuccess.jsp");
%>


<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

<title>失败</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">

</head>

<body>
<h1>Fail!</h1>
</body>
</html>


<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

<title>成功</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">

</head>

<body>
<h1>Success!</h1>
</body>
</html>