环境配置:
在进行项目之前首先呢需要配置我们所需要的各种环境(jdk,Mysql,Tomcat,Maven,jdbc);
下面是我们Tomcat相关配置,如图:
Mysql相关表,为了方便我们只设置了用户姓名和密码
create table user(
username varchar(20) not null,
`password` varchar(20) not null
);
Login.jsp
将表单的数据提交给hello-servlet
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<body>
<form action="hello-servlet" method="post">
姓名:<input type="text" name="username" id="i01"><br/>
密码:<input type="password" name="password" id="i02"><br/>
提交:<input type="submit">
</form>
</body>
</html>
User.java 用户类
private String username;
private String password;
public User(String username, String password) {
this.username = username;
this.password = password;
}
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;
}
@Override
public String toString() {
return "User{" +
"username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
JDBCUtil.java
为了项目开发方便,提前将JDBC封装成一个jdbc工具类:
public class JDBCUtil {
private Connection conn = null;
private PreparedStatement ps = null;
private static String username = "root";
private static String password = "root123";
private static String driver = "com.mysql.jdbc.Driver";
private static String url = "jdbc:mysql:///jdbc";
public JDBCUtil() {
}
static {
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public Connection getConnection(){
Connection conn = null;
try {
conn = DriverManager.getConnection(url,username,password);
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public PreparedStatement ps(String sql){
PreparedStatement ps = null;
Connection conn = getConnection();
try {
ps = conn.prepareStatement(sql);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return ps;
}
public void Close(){
if (ps != null) {
try {
ps.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
private void Close(ResultSet rs){
if (rs != null) {
try {
rs.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
Close();
}
}
HelloServlet.java
在用户进行表单输入之后,将表单数据传给Servlet进行处理:
//添加注解
@WebServlet(name = "helloServlet", value = "/hello-servlet")
public class HelloServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
UserDao dao = new UserDao();
User user = null;
int result = 0;
PrintWriter out = null;
String username = request.getParameter("username");
String password = request.getParameter("password");
user = new User(username, password);
result = dao.add(user);
out = response.getWriter();
if (result !=0){
out.print("<font style='color:red;font-size:40'>注册成功</font>");
}else {
out.print("<font style='color:red;font-size:40'>注册失败</font>");
}
}
}
UserDao.java
拿到表单数据之后用UserDao类进行对数据库的添加操作:
public class UserDao {
private JDBCUtil jdbcUtil = new JDBCUtil();
public int add(User user) {
String sql = "insert into user values(?,?)";
PreparedStatement ps = jdbcUtil.ps(sql);
int result = 0;
try {
ps.setString(1, user.getUsername());
ps.setString(2, user.getPassword());
result = ps.executeUpdate();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return result;
}
}
启动Tomcat,访问Login.jsp,在表单中输入姓名:张三 ,密码:123;
此时界面上输出了注册成功:
我们进入到数据库查看结果:
注册成功!