1. 程式人生 > >使用Servlet來完成使用者名稱和密碼的登入驗證以及登入人數的增加

使用Servlet來完成使用者名稱和密碼的登入驗證以及登入人數的增加

登入頁面

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    <form action="login">
        姓名:<input type="text" name="name"><br>
        密碼:<input type="password" name="password">
<br> <input type="submit" value="登入" > </form> </body> </html>

建立Servlet

public class LoginServlet extends HttpServlet {
    @Override //重寫init方法 該方法是通過ServletContext上下文物件設定count值來完成登入人數的增加功能
    public void init() throws ServletException {
        int count = 0
; this.getServletContext().setAttribute("count", count); //設定域屬性 } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //獲得請求中的屬性值 String name = request.getParameter("name"); String password = request.getParameter("password"
); //從資料庫中獲取使用者名稱的值 這裡用了C3P0連線池 QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource()); String sql = "select * from user where name =? and password=?"; User user_bean = null; try { User user = queryRunner.query(sql, new BeanHandler<User>(User.class), name,password); user_bean = user; ServletContext servletContext = this.getServletContext(); if(user_bean!=null) { //獲得域屬性 Integer count = (Integer) servletContext.getAttribute("count"); count++; //count值自增 servletContext.setAttribute("count", count); //設定域屬性 response.getWriter().write("good "+user_bean+"<br>"+"是第"+count+"位"); } else { response.getWriter().write("bad"); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }

C3P0連線池的使用

使用前需要先匯入三個jar包
這裡寫圖片描述

之後將C3P0配置檔案寫入 注意要放入src目錄下 這個是必須的 名稱必須為c3p0-config.xml

//C3P0配置檔案
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>

  <default-config>
    <property name="driverClass">com.mysql.jdbc.Driver</property>
    <property name="jdbcUrl">jdbc:mysql:///</property>
    <property name="user">root</property>
    <property name="password">root</property>
    <property name="initialPoolSize">5</property>
    <property name="maxPoolSize">20</property>
  </default-config>

  <named-config name="oracle"> 
    <property name="driverClass">com.mysql.jdbc.Driver</property>
    <property name="jdbcUrl">jdbc:mysql:///</property>
    <property name="user">root</property>
    <property name="password">root</property>
  </named-config>


</c3p0-config>

寫完配置檔案後 寫C3P0Utils工具類

public class C3P0Utils {
    public static ComboPooledDataSource pd = new ComboPooledDataSource();

    public static DataSource getDataSource() {
        return pd;
    }

    public static Connection getConnection() {
        try {
            return pd.getConnection();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public static void close(Connection conn,PreparedStatement pst,ResultSet rs){ 
        if(rs!=null){ 
            try { 
                rs.close(); 
            } catch (SQLException e) { 
                e.printStackTrace(); 
            } 
        } 
        if(pst!=null){ 
            try { 
                pst.close(); 
            } catch (SQLException e) { 
               e.printStackTrace(); 
            } 
        } 

        if(conn!=null){ 
            try { 
                conn.close(); 
            } catch (SQLException e) { 
                e.printStackTrace(); 
            } 
        } 
    } 
}

查詢操作需要封裝一個實體類物件 在這裡用的User

public class C3P0Utils {
    public static ComboPooledDataSource pd = new ComboPooledDataSource();

    public static DataSource getDataSource() {
        return pd;
    }

    public static Connection getConnection() {
        try {
            return pd.getConnection();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public static void close(Connection conn,PreparedStatement pst,ResultSet rs){ 
        if(rs!=null){ 
            try { 
                rs.close(); 
            } catch (SQLException e) { 
                e.printStackTrace(); 
            } 
        } 
        if(pst!=null){ 
            try { 
                pst.close(); 
            } catch (SQLException e) { 
               e.printStackTrace(); 
            } 
        } 

        if(conn!=null){ 
            try { 
                conn.close(); 
            } catch (SQLException e) { 
                e.printStackTrace(); 
            } 
        } 
    } 
}