最近学习了Servlet,为了巩固一下知识,用它来实现一个用户登录页面。
=========================================================================
实现原理:
1.用户通过login.html页面进行表单提交。
2.通过Servlet获取用户提交的表单,使用DBUtils技术获取数据库中的数据并进行校验
3.向页面输出校验结果
实现过程:
1.使用IDEA创建一个Web项目并配置好web.xml如下:
-
<?xml version="1.0" encoding="UTF-8"?>
-
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
-
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">
-
<servlet>
-
<servlet-name>LoginServlet</servlet-name>
-
<servlet-class>com.liweijian.login.LoginServlet</servlet-class>
-
</servlet>
-
<servlet-mapping>
-
<servlet-name>LoginServlet</servlet-name>
-
<url-pattern>/login</url-pattern>
-
</servlet-mapping>
-
</web-app>
<url-pattern>用于表单提交的虚拟地址
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<Connection> tl = new ThreadLocal<Connection>();
-
// 直接可以获取一个连接池
-
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文件中定义了数据库的登录名和密码、数据库注册驱动以及需要连接的数据库名称
-
<?xml version="1.0" encoding="UTF-8"?>
-
<c3p0-config>
-
<default-config>
-
<property name="user">root</property>
-
<property name="password">qq352642663</property>
-
<property name="driverClass">com.mysql.jdbc.Driver</property>
-
<property name="jdbcUrl">jdbc:mysql:///web13</property>
-
</default-config>
-
</c3p0-config>
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>(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();
-
}
-
}
-
}
--------------------- 本文来自 _Liweijian 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/qq352642663/article/details/78503383?utm_source=copy