登录

最近学习了Servlet,为了巩固一下知识,用它来实现一个用户登录页面。

=========================================================================

实现原理:

1.用户通过login.html页面进行表单提交。

2.通过Servlet获取用户提交的表单,使用DBUtils技术获取数据库中的数据并进行校验

3.向页面输出校验结果

 

实现过程:

1.使用IDEA创建一个Web项目并配置好web.xml如下:

 

 
  1. <?xml version="1.0" encoding="UTF-8"?>

  2. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"

  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  4. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"

  5. version="3.1">

  6. <servlet>

  7. <servlet-name>LoginServlet</servlet-name>

  8. <servlet-class>com.liweijian.login.LoginServlet</servlet-class>

  9. </servlet>

  10. <servlet-mapping>

  11. <servlet-name>LoginServlet</servlet-name>

  12. <url-pattern>/login</url-pattern>

  13. </servlet-mapping>

  14. </web-app>

<url-pattern>用于表单提交的虚拟地址

 

 

2. 创建继承于HttpServlet的类 LoginServlet 如下:

 

 
  1. public class LoginServlet extends HttpServlet {

  2. protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {

  3. doGet(request,response);

  4. }

  5.  
  6. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

 

 

3.导入DBUtils所需要的jar包并加入项目中

 

 
  1. c3p0-0.9.1.2.jar

  2. commons-dbutils-1.4.jar

  3. mysql-connector-java-5.0.4-bin.jar

 

 

4.使用DBUtils工具类(此类需要自行提取封装)

 

 
  1. package com.liweijian.utils;

  2.  
  3. import java.sql.Connection;

  4. import java.sql.ResultSet;

  5. import java.sql.SQLException;

  6. import java.sql.Statement;

  7.  
  8. import javax.sql.DataSource;

  9.  
  10. import com.mchange.v2.c3p0.ComboPooledDataSource;

  11.  
  12. public class DataSourceUtils {

  13.  
  14. private static DataSource dataSource = new ComboPooledDataSource();

  15.  
  16. private static ThreadLocal<Connection> tl = new ThreadLocal<Connection>();

  17.  
  18. // 直接可以获取一个连接池

  19. public static DataSource getDataSource() {

  20. return dataSource;

  21. }

  22.  
  23. // 获取连接对象

  24. public static Connection getConnection() throws SQLException {

  25.  
  26. Connection con = tl.get();

  27. if (con == null) {

  28. con = dataSource.getConnection();

  29. tl.set(con);

  30. }

  31. return con;

  32. }

  33.  
  34. // 开启事务

  35. public static void startTransaction() throws SQLException {

  36. Connection con = getConnection();

  37. if (con != null) {

  38. con.setAutoCommit(false);

  39. }

  40. }

  41.  
  42. // 事务回滚

  43. public static void rollback() throws SQLException {

  44. Connection con = getConnection();

  45. if (con != null) {

  46. con.rollback();

  47. }

  48. }

  49.  
  50. // 提交并且 关闭资源及从ThreadLocall中释放

  51. public static void commitAndRelease() throws SQLException {

  52. Connection con = getConnection();

  53. if (con != null) {

  54. con.commit(); // 事务提交

  55. con.close();// 关闭资源

  56. tl.remove();// 从线程绑定中移除

  57. }

  58. }

  59.  
  60. // 关闭资源方法

  61. public static void closeConnection() throws SQLException {

  62. Connection con = getConnection();

  63. if (con != null) {

  64. con.close();

  65. }

  66. }

  67.  
  68. public static void closeStatement(Statement st) throws SQLException {

  69. if (st != null) {

  70. st.close();

  71. }

  72. }

  73.  
  74. public static void closeResultSet(ResultSet rs) throws SQLException {

  75. if (rs != null) {

  76. rs.close();

  77. }

  78. }

  79.  
  80. }

 

 

5. 配置c3p0-config.xml,此xml文件中定义了数据库的登录名和密码、数据库注册驱动以及需要连接的数据库名称

 

 
  1. <?xml version="1.0" encoding="UTF-8"?>

  2. <c3p0-config>

  3. <default-config>

  4. <property name="user">root</property>

  5. <property name="password">qq352642663</property>

  6. <property name="driverClass">com.mysql.jdbc.Driver</property>

  7. <property name="jdbcUrl">jdbc:mysql:///web13</property>

  8. </default-config>

  9. </c3p0-config>


6.在doGet或者doPost方法中实现校验操作。

 

 

 
  1. public class LoginServlet extends HttpServlet {

  2. protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {

  3. doGet(request,response);

  4. }

  5.  
  6. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

  7.  
  8. //1.获得用户的用户名和密码

  9. String username = request.getParameter("username");

  10. String password = request.getParameter("password");

  11. User user;

  12.  
  13. //2.从数据库判断用户名和密码

  14. QueryRunner queryRunner = new QueryRunner(DataSourceUtils.getDataSource());

  15. String sql = "select * from User where username = ? and password = ?";

  16. try {

  17. user = queryRunner.query(sql, new BeanHandler<User>(User.class), username, password);

  18.  
  19. //3.输出验证结果

  20. if (user!= null){

  21. response.getWriter().write(user.toString());

  22. }else {

  23. response.getWriter().write("sorry, your username or password is wrong!");

  24. }

  25. } catch (SQLException e) {

  26. e.printStackTrace();

  27. }

  28.  
  29. }

  30. }

  --------------------- 本文来自 _Liweijian 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/qq352642663/article/details/78503383?utm_source=copy

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值