servlet增删查改demo(一)注册与登录
项目结构
增删查改利用jsp前端页面获取用户在浏览器输入或者操作的数据,利用servlet进行处理,通过jdbc调用数据库,从而完成用户的要求。
注册:
userregister.jsp
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 3 <!DOCTYPE html> 4 <html> 5 <head> 6 <meta charset="utf-8"> 7 <title>用户注册</title> 8 </head> 9 <body> 10 11 <form action="userregister" method="post"> 12 <table align = "center" border="1" style="border-collapse: collapse;"> 13 <tr> 14 <td colspan="2">用户注册</td> 15 </tr> 16 <tr> 17 <td>用户名:</td> 18 <td><input type="text" name="userName" /></td> 19 </tr> 20 <tr> 21 <td>密码:</td> 22 <td><input type="password" name="userPassword" /></td> 23 </tr> 24 <tr> 25 <td class="tdstyle" colspan="2"> 26 <input type="submit" value="注册" /> 27 </td> 28 </tr> 29 </table> 30 </form> 31 32 </body> 33 </html>
userlogin.jsp
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 3 <!DOCTYPE html> 4 <html> 5 <head> 6 <meta charset="ISO-8859-1"> 7 <title>登录页面</title> 8 </head> 9 <body> 10 11 <form action="login" method="post"> 12 <p>用户名: <input type="text" name="username" /></p> 13 <p>密 码: <input type="password" name="password" /></p> 14 <input type = "submit" name="login" value ="登录"/> 15 </form> 16 17 </body> 18 </html>
ServletUserRegister.java
1 package servlet; 2 3 import java.io.IOException; 4 import java.sql.SQLException; 5 6 import javax.servlet.ServletException; 7 import javax.servlet.http.HttpServlet; 8 import javax.servlet.http.HttpServletRequest; 9 import javax.servlet.http.HttpServletResponse; 10 11 import dao.UserDao; 12 import vo.UserEmp; 13 14 public class ServletUserRegister extends HttpServlet{ 15 16 public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException { 17 18 request.setCharacterEncoding("utf8"); 19 response.setCharacterEncoding("utf8"); 20 21 //获取表单中的username,password 22 String username = request.getParameter("userName"); 23 String password = request.getParameter("userPassword"); 24 25 //数据存入VO 26 UserEmp useremp = new UserEmp(); 27 useremp.setUsername(username); 28 useremp.setPassword(password); 29 30 //实例化操作数据库对象 31 UserDao userDao = new UserDao(); 32 //调用增加用户方法 33 try { 34 userDao.insert(useremp); 35 } catch (SQLException e) { 36 e.printStackTrace(); 37 } 38 39 request.getRequestDispatcher("/userlogin.jsp").forward(request, response); 40 41 } 42 43 }
从表单中获取用户输入的数据,存储到数据库中,在把页面转发到登录页面。
ServletUserLogin.java
1 package servlet; 2 3 import java.io.IOException; 4 import java.sql.SQLException; 5 6 import javax.servlet.ServletException; 7 import javax.servlet.http.HttpServlet; 8 import javax.servlet.http.HttpServletRequest; 9 import javax.servlet.http.HttpServletResponse; 10 11 import dao.UserDao; 12 import vo.UserEmp; 13 14 public class ServletUserLogin extends HttpServlet { 15 16 @Override 17 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { 18 19 req.setCharacterEncoding("utf8"); 20 resp.setCharacterEncoding("utf8"); 21 22 String username = req.getParameter("username"); 23 String password = req.getParameter("password"); 24 25 UserEmp userEmp = new UserEmp(); 26 userEmp.setUsername(username); 27 userEmp.setPassword(password); 28 29 UserDao dao = new UserDao(); 30 31 boolean flag = false; 32 try { 33 flag = dao.login(userEmp); 34 } catch (SQLException e) { 35 e.printStackTrace(); 36 } 37 38 if(flag) 39 req.getRequestDispatcher("/userlist.jsp").forward(req, resp); 40 else 41 req.getRequestDispatcher("/userlogin.jsp").forward(req, resp); 42 43 } 44 45 }
从浏览器获取用户输入数据,与底层数据库的数据比较,若用户名密码都相等则页面转到list页面,若不同则重新刷新当前登录页面
UserDao.java
1 package dao; 2 3 import java.sql.Connection; 4 import java.sql.PreparedStatement; 5 import java.sql.ResultSet; 6 import java.sql.SQLException; 7 import java.sql.Statement; 8 9 import vo.UserEmp; 10 11 public class UserDao { 12 13 public boolean login(UserEmp userEmp) throws SQLException { 14 15 Connection connection = dbmanage.Util.getConnection(); 16 Statement statement = connection.createStatement(); 17 18 String sql = "SELECT user_name,user_pawd FROM user_emp "; 19 ResultSet rs = statement.executeQuery(sql); 20 21 String username = null; 22 String password = null; 23 24 while(rs.next()) { 25 username = rs.getString("user_name"); 26 password = rs.getString("user_pawd"); 27 28 if(username.equals(userEmp.getUsername()) && password.equals(userEmp.getPassword())) { 29 return true; 30 } 31 32 } 33 34 dbmanage.Util.closeDB(statement, connection, rs); 35 36 return false; 37 } 38 39 public void delete(UserEmp userEmp) { 40 41 } 42 43 public void update(UserEmp userEmp) { 44 45 } 46 47 public void insert(UserEmp userEmp) throws SQLException { 48 49 Connection connection = dbmanage.Util.getConnection(); 50 51 String sql = "INSERT INTO user_emp (user_name,user_pawd) VALUES(?,?)"; 52 53 PreparedStatement ps = connection.prepareStatement(sql); 54 ps.setString(1, userEmp.getUsername()); 55 ps.setString(2, userEmp.getPassword()); 56 57 ps.executeUpdate(); 58 59 dbmanage.Util.closeDB(ps, connection); 60 61 } 62 63 }
工具类:调用数据库
Util.java
1 package dbmanage; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.ResultSet; 6 import java.sql.SQLException; 7 import java.sql.Statement; 8 9 public class Util { 10 11 private static String driver = "com.mysql.jdbc.Driver"; 12 private static String url = "jdbc:mysql://localhost:3306/my_db?charaterEncoding=utf-8"; 13 private static String username = "root"; 14 private static String password = "123456"; 15 16 public static Connection getConnection() throws SQLException { 17 18 Connection con = null; 19 20 try { 21 Class.forName(driver); 22 con = DriverManager.getConnection(url, username, password); 23 } catch (ClassNotFoundException e) { 24 e.printStackTrace(); 25 } 26 27 return con; 28 29 } 30 31 public static void closeDB(Statement statement,Connection connection,ResultSet resultSet) { 32 try { 33 statement.close(); 34 resultSet.close(); 35 connection.close(); 36 } catch (SQLException e) { 37 e.printStackTrace(); 38 } 39 } 40 41 public static void closeDB(Statement statement,Connection connection) { 42 try { 43 statement.close(); 44 connection.close(); 45 } catch (SQLException e) { 46 e.printStackTrace(); 47 } 48 } 49 50 }
vo:与数据库表对应,通过创建对象把数据存入
UserEmp.java
1 package vo; 2 3 public class UserEmp { 4 5 private String username; 6 private String password; 7 8 public String getUsername() { 9 return username; 10 } 11 public void setUsername(String username) { 12 this.username = username; 13 } 14 public String getPassword() { 15 return password; 16 } 17 public void setPassword(String password) { 18 this.password = password; 19 } 20 21 }
web.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app version="2.5" 3 xmlns="http://java.sun.com/xml/ns/javaee" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 6 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 7 8 <!-- 登录 --> 9 <servlet> 10 <servlet-name>UserLogin</servlet-name> 11 <servlet-class>servlet.ServletUserLogin</servlet-class> 12 </servlet> 13 <servlet-mapping> 14 <servlet-name>UserLogin</servlet-name> 15 <url-pattern>/login</url-pattern> 16 </servlet-mapping> 17 18 <!-- 注册 --> 19 <servlet> 20 <servlet-name>UserRegister</servlet-name> 21 <servlet-class>servlet.ServletUserRegister</servlet-class> 22 </servlet> 23 <servlet-mapping> 24 <servlet-name>UserRegister</servlet-name> 25 <url-pattern>/userregister</url-pattern> 26 </servlet-mapping> 27 28 </web-app>
项目测试:在浏览器地址栏输入项目下的jsp文件即可访问