<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<form method="post" action="register.jsp">
用户名:<input type="text" name="username"><br>
密码:<input type="text" name="password"><br>
性别:<input type="text" name="gender"><br>
年龄:<input type="text" name="age"><br>
<button type="submit">注册</button>
</form>
</body>
</html>
user文件
package com;
public class user {
private String username;
private String password;
private int gender;
private int age;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getGender() {
return gender;
}
public void setGender(int gender) {
this.gender = gender;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
register文件
<jsp:useBean id="user" class="com.user" scope="request"></jsp:useBean>
<jsp:setProperty name="user" property="*"></jsp:setProperty>
<%
Class.forName("com.mysql.jdbc.Driver");
String url= "jdbc:mysql://localhost:3306/book?user=root&password=root";
Connection connection= DriverManager.getConnection(url);
String sql="insert into user (username,password,gender,age) values('"+user.getUsername()+"','"+user.getPassword()+"',1,23)";
Statement stmt=connection.createStatement();
int count=stmt.executeUpdate(sql);
if(count==0){
out.println("注册失败");
}else{
out.println("注册成功");
}
%>
<jsp:getProperty name="user" property="username"/>