時間:2017年11月10日 21:08:51
最近學習了Servlet,為了鞏固一下知識,用它來實作一個使用者登入頁面。
=========================================================================
實作原理:
1.使用者通過login.html頁面進行表單送出。
2.通過Servlet擷取使用者送出的表單,使用DBUtils技術擷取資料庫中的資料并進行校驗
3.向頁面輸出校驗結果
實作過程:
1.使用IDEA建立一個Web項目并配置好web.xml如下:
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
LoginServlet
com.liweijian.login.LoginServlet
LoginServlet
/login
用于表單送出的虛拟位址
2. 建立繼承于HttpServlet的類 LoginServlet如下:
public class LoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
3.導入DBUtils所需要的jar包并加入項目中
c3p0-0.9.1.2.jar
commons-dbutils-1.4.jar
mysql-connector-java-5.0.4-bin.jar
4.使用DBUtils工具類(此類需要自行提取封裝)
package com.liweijian.utils;
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;
public class DataSourceUtils {
private static DataSource dataSource = new ComboPooledDataSource();
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();
}
}
}
5. 配置c3p0-config.xml,此xml檔案中定義了資料庫的登入名和密碼、資料庫注冊驅動以及需要連接配接的資料庫名稱
root
qq352642663
com.mysql.jdbc.Driver
jdbc:mysql:///web13
6.在doGet或者doPost方法中實作校驗操作。
public class LoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.獲得使用者的使用者名和密碼
String username = request.getParameter("username");
String password = request.getParameter("password");
User user;
//2.從資料庫判斷使用者名和密碼
QueryRunner queryRunner = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "select * from User where username = ? and password = ?";
try {
user = queryRunner.query(sql, new BeanHandler(User.class), username, password);
//3.輸出驗證結果
if (user!= null){
response.getWriter().write(user.toString());
}else {
response.getWriter().write("sorry, your username or password is wrong!");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
在代碼中使用query進行資料庫操作時,需要建立與資料庫字段所對應的實體類,即User,該類中包含相應的屬性,且必須提供getter和setter
代碼如下:
package com.liweijian.domain;
public class User {
private int id;
private String username;
private String password;
private String emain;
public void setId(int id) {
this.id = id;
}
public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}
public void setEmain(String emain) {
this.emain = emain;
}
public int getId() {
return id;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
public String getEmain() {
return emain;
}
@Override
public String toString() {
return getUsername() + "," + "welcome";
}
}
7. 頁面代碼如下:
使用者登入
使用者名:
密碼: