Servlet 案例(模擬登錄功能)

一.新建一個數據庫java

在數據庫中存入一個數據web

二.編寫一個簡單的用戶登錄的界面sql

三.編寫java代碼,鏈接數據庫(此處使用DBUtils),驗證用戶登錄。
            3.1 編寫鏈接池封裝類用於鏈接數據庫數據庫

(注意此處須要導入jar包,而且須要將c3p0-config.xml 配置文件,放在web項目的src目錄下)apache

https://pan.baidu.com/s/1eev43QR5TAFKi_ZRqIn_PQ   提取碼:ebcw(此處是jar包和xml配置文件)瀏覽器

package servlet;this

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement
import javax.sql.DataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource;
spa

public class DataSourceUtils {
線程

private static DataSource dataSource = new ComboPooledDataSource();
code

private static ThreadLocal tl = new ThreadLocal();

// 直接能夠獲取一個鏈接池
public static DataSource getDataSource() {
return dataSource;
}

// 獲取鏈接對象
public static Connection getConnection() throws SQLException {

Connection con = tl.get();
  if (con == null) {
     con = dataSource.getConnection();
     tl.set(con);
  }
  return con;
複製代碼

}

// 開啓事務
public static void startTransaction() throws SQLException {
Connection con = getConnection();
if (con != null) {
con.setAutoCommit(false);
}
}

// 事務回滾
public static void rollback() throws SQLException {
Connection con = getConnection();
if (con != null) {
con.rollback();
}
}

// 提交而且 關閉資源及從ThreadLocall中釋放
public static void commitAndRelease() throws SQLException {
Connection con = getConnection();
if (con != null) {
con.commit(); // 事務提交
con.close();// 關閉資源
tl.remove();// 從線程綁定中移除
}
}

// 關閉資源方法
public static void closeConnection() throws SQLException {
Connection con = getConnection();
if (con != null) {
con.close();
}
}

public static void closeStatement(Statement st) throws SQLException {
if (st != null) {
st.close();
}
}

public static void closeResultSet(ResultSet rs) throws SQLException {
if (rs != null) {
rs.close();
}
}

}

3.2 編寫JavaBean類用於存儲從數據庫獲取到的信息

package servlet;

public class User {

private String user;
private String password;
private String email;


public String getUser() {
    return user;
}

public void setUser(String user) {
    this.user = user;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}
複製代碼

}

3.3 編寫log_in_verification類用於,獲取用戶信息,並在數據庫中進行驗證,同時將結果響應在瀏覽器。

package servlet;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class log_in_verification extends HttpServlet {

public void doGet(HttpServletRequest req, HttpServletResponse resp){
    this.doPost(req,resp);
}
public void doPost(HttpServletRequest req,HttpServletResponse resp){

    //獲取用戶提交的用戶名和密碼
    String name=req.getParameter("user");
    String password=req.getParameter("password");


    //從數據庫中驗證密碼是否正確
    QueryRunner runner=new QueryRunner(DataSourceUtils.getDataSource());
    String sql="select * from user where username=? and password=?";
    User user=null;
    try{
        user=runner.query(sql,new BeanHandler<User>(User.class),name,password);
        if(user!=null){
            user.setUser(name);
            resp.getWriter().write("greet"+user.getUser());
        }else{
            resp.getWriter().write("login failure");
        }
    }catch (Exception e){
        e.printStackTrace();
    }



}
複製代碼

}

四.展現效果 http://39.108.158.0:8080/mp4/123.mp4

相關文章
相關標籤/搜索