201819102013倪永剑
index:
<html>
<head>
<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>
register:
<html>
<head>
<title></title>
</head>
<body>
<jsp:useBean id="user" class="com.media.bean.UserBean" scope="application"></jsp:useBean>
<jsp:setProperty name="user" property="*"></jsp:setProperty>
<%
//UserBean user1=new UserBean();
//user.setUsername(request.getParameter("username"));
//out.print(user.getUsername());
//加载驱动程序
Class.forName("com.mysql.jdbc.Driver");
//建立数据库连接
String url = "jdbc:mysql://127.0.0.1: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.print("注册失败");
}else {
out.print("注册成功");
}
%>
<jsp:getProperty name="user" property="username"/>
</body>
</html>
user:
package com.media.bean;
public class user {
public String getUsernanme() {
return usernanme;
}
public void setUsernanme(String usernanme) {
this.usernanme = usernanme;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getGender() {
return gender;
}
public void setGender(int gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
private String usernanme;
private String password;
private int gender;
private int age;
}
main:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<jsp:useBean id="user" class="com.media.bean.UserBean" scope="request"></jsp:useBean>
<%
out.print(user.getUsername());
%>
</body>
</html>
运行结果: