0%

JavaWeb编程题

JavaWeb

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<!--addUser.jsp-->
<%@ page language="java" contentType="text/html;charset=UTF-8" %>
<html>
<head>
<title>addUser</title>
</head>
<body>
<h3>添加用户</h3>
<form action="UserServlet" method="post">
名称:<input type="text" name="name" /><br/>
密码:<input type="text" name="password" /><br/>
邮箱:<input type="text" name="email" /><br/>
<input type="submit" value="添加" />
<input type="reset" value="重置" />
</form>
</body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
<!--addUserSuccess.jsp-->
<%@ page language="java" contentType="text/html;charset=UTF-8" %>
<html>
<head>
<title>addUserSuccess</title>
</head>
<body>
<h3>
添加用户成功!<a href="addUser.jsp">继续添加</a>
</h3>
</body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//UserServlet.java
@WebServlet("/UserServlet")
public class UserServlet extends HttpServlet{
protected void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
request.setCharacterEncoding("UTF-8");

UserBean user = new UserBean();
user.setName(request.getParameter("name"));
user.setPassword(request.getParameter("password"));
user.setEmail(request.getParameter("email"));

UserDao ud = new UserDao();
boolean result = ud.insert(user);
if(result){
response.sendRedirect("addUserSuccess.jsp");
}else{
response.sendRedirect("addUser.jsp");
}
}
protected void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
doGet(request,response);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//UserBean.java
public class UserBean{
private String name;
private String password;
private String email;

public void setName(String name){
this.name=name;
}
public String getName(){
return name;
}
public void setPassword(String password){
this.password=password;
}
public String getPassword(){
return password;
}
public void setEmail(String email){
this.email=email;
}
public String getEmail(){
return email;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
//UserDao.java
public class UsersDao{
//添加用户的操作
public Boolean insert(UserBean user){
try{
//1.获取连接
Connection conn = JDBCUtil.getConnection();
//2.拿到执行者
//Statement stat = conn.creatStatement();
//3.执行sql
String sql = "insert into users values(?,?,?)";
PreparedStatement ps = conn.preparedStatement(sql);
ps.setString(1,user.getName());
ps.setString(2,user.getPassword());
ps.setString(3,user.getEmail());
//4.获取结果
int i = stat.executeUpdate(sql);
//4.判断,返回结果
if(i>0){
return true;
}
return false;
}catch(ServletException e){
e.printStackTrace();
}finally{
//5.关闭资源
JDBDUtil.release(stat,conn);
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
//JDBCUtil.java
public class JDBCUtil{
//获取连接
public static Connection getConnection(){
//1.注册驱动
Class.forName("com.mysql.jdbc.Driver");
//2.获取连接
String url = "jdbc:mysql://localhost:3306/jdbc";
String user = "root";
String password = "123";
Connection conn = DriverManager.getConnection(url,user,password);
//3.将连接返回
return conn;
}
//关闭资源
public static void release(ResultSet rs,Statement stat,Connection conn){
if(rs!=null){
rs.close();
}
if(stat!=null){
stat.close();
}
if(conn!=null){
conn.close();
}
}
public static void release(Statement stat,Connection conn){
if(stat!=null){
stat.close();
}
if(conn!=null){
conn.close();
}
}
}
-------------本 文 结 束 感 谢 您 的 阅 读-------------