我是靠谱客的博主 乐观鞋子,最近开发中收集的这篇文章主要介绍Servlet学习(三)——实例:用户登录并记录登陆次数,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

1、前提:在Mysql数据库下建立数据库web13,在web13下创建一张表user,插入几条数据如下:

2、创建HTML文件,命名为login,作为登录界面(以post方式提交)

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4 <meta charset="UTF-8">
 5 <title>Insert title here</title>
 6 </head>
 7 <body>
 8
<form action="/WEB13_sevrlet/login" method="post">
 9
用户名:<input type="text" name="username"><br/>
10
密码:<input type="password" name="password"><br/>
11
<input type="submit" value="登录"><br/>
12
</form>
13 </body>
14 </html>

3、创建连接池

c3p0-config.xml文件如下:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <c3p0-config>
 3
 4
<default-config>
 5
<property name="driverClass">com.mysql.jdbc.Driver</property>
 6
<property name="jdbcUrl">jdbc:mysql:///web13</property>
 7
<property name="user">root</property>
 8
<property name="password">12345</property>
 9
</default-config>
10
11 </c3p0-config>

 DataSourceUtils.java文件如下:(可作为套用模板)

 1 package com.itheima.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
private static DataSource dataSource = new ComboPooledDataSource();
14
private static ThreadLocal<Connection> tl = new ThreadLocal<Connection>();
15
16
// 直接可以获取一个连接池
17
public static DataSource getDataSource() {
18
return dataSource;
19 
}
20
21
// 获取连接对象
22
public static Connection getConnection() throws SQLException {
23
Connection con = tl.get();
24
if (con == null) {
25
con = dataSource.getConnection();
26 
tl.set(con);
27 
}
28
return con;
29 
}
30
31
// 开启事务
32
public static void startTransaction() throws SQLException {
33
Connection con = getConnection();
34
if (con != null) {
35
con.setAutoCommit(false);
36 
}
37 
}
38
39
// 事务回滚
40
public static void rollback() throws SQLException {
41
Connection con = getConnection();
42
if (con != null) {
43 
con.rollback();
44 
}
45 
}
46
47
// 提交并且 关闭资源及从ThreadLocall中释放
48
public static void commitAndRelease() throws SQLException {
49
Connection con = getConnection();
50
if (con != null) {
51
con.commit(); // 事务提交
52
con.close();// 关闭资源
53
tl.remove();// 从线程绑定中移除
54 
}
55 
}
56
57
// 关闭资源方法
58
public static void closeConnection() throws SQLException {
59
Connection con = getConnection();
60
if (con != null) {
61 
con.close();
62 
}
63 
}
64
65
public static void closeStatement(Statement st) throws SQLException {
66
if (st != null) {
67 
st.close();
68 
}
69 
}
70
71
public static void closeResultSet(ResultSet rs) throws SQLException {
72
if (rs != null) {
73 
rs.close();
74 
}
75 
}
76 }

4、编写Servlet实现类

User类代码如下:

 1 package com.itheima.domain;
 2
 3 public class User {
 4
private int id;
 5
private String name;
 6
private String password;
 7
 8
public User(){
 9
10 
}
11
12
public int getId() {
13
return id;
14 
}
15
16
public void setId(int id) {
17
this.id = id;
18 
}
19
20
public String getName() {
21
return name;
22 
}
23
24
public void setName(String name) {
25
this.name = name;
26 
}
27
28
public String getPassword() {
29
return password;
30 
}
31
32
public void setPassword(String password) {
33
this.password = password;
34 
}
35 }

LoginServlet.java代码如下:

 1 package com.itheima.login;
 2
 3 import java.io.IOException;
 4 import java.sql.SQLException;
 5
 6 import javax.servlet.ServletContext;
 7 import javax.servlet.ServletException;
 8 import javax.servlet.http.HttpServlet;
 9 import javax.servlet.http.HttpServletRequest;
10 import javax.servlet.http.HttpServletResponse;
11
12 import org.apache.commons.dbutils.QueryRunner;
13 import org.apache.commons.dbutils.handlers.BeanHandler;
14
15 import com.itheima.domain.User;
16 import com.itheima.utils.DataSourceUtils;
17
18 public class LoginServlet extends HttpServlet {
19
20 
@Override
21
public void init() throws ServletException{
22
//在Servletcontext域中存一个数据count
23
int count=0;
24
this.getServletContext().setAttribute("count", count);
25 
}
26
27
public void doGet(HttpServletRequest request, HttpServletResponse response)
28
throws ServletException, IOException {
29
30
//1.获得用户名和密码
31
String username=request.getParameter("username");
32
String password=request.getParameter("password");
33
34
//2.从数据库中验证该用户名和密码是否正确
35
QueryRunner runner=new QueryRunner(DataSourceUtils.getDataSource());
36
String sql="select * from user where username=? and password=?";
37
User user=null;
38
try {
39
user=runner.query(sql, new BeanHandler<User>(User.class),username,password);
40
} catch (SQLException e) {
41 
e.printStackTrace();
42 
}
43
//3.根据返回的结果给用户不同显示信息 
44
if(user!=null){
45
//从Servletcontext中取出count进行++运算
46
ServletContext context=this.getServletContext();
47
Integer count=(Integer)context.getAttribute("count");
48
count++;
49
//用户登录成功
50
response.getWriter().write(user.toString()+"--you are success login person"+count);
51
context.setAttribute("count", count);
52
}else{
53
//用户登录失败
54
response.getWriter().write("Sorry,your username or password is wrong!");
55 
}
56 
}
57
58
public void doPost(HttpServletRequest request, HttpServletResponse response)
59
throws ServletException, IOException {
60 
doGet(request, response);
61 
}
62 }

5、编写Servlet配置文件,代码如下:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
 3
<display-name>WEB13_sevrlet</display-name>
 4
<context-param>
 5
<param-name>driver</param-name>
 6
<param-value>com.mysql.jdbc.Driver</param-value>
 7
</context-param>
 8
 9
<welcome-file-list>
10
<welcome-file>index.html</welcome-file>
11
<welcome-file>index.htm</welcome-file>
12
<welcome-file>index.jsp</welcome-file>
13
<welcome-file>default.html</welcome-file>
14
<welcome-file>default.htm</welcome-file>
15
<welcome-file>default.jsp</welcome-file>
16
</welcome-file-list>
17
18
<servlet>
19
<description></description>
20
<display-name>LoginServlet</display-name>
21
<servlet-name>LoginServlet</servlet-name>
22
<servlet-class>com.itheima.login.LoginServlet</servlet-class>
23
</servlet>
24
<servlet-mapping>
25
<servlet-name>LoginServlet</servlet-name>
26
<url-pattern>/login</url-pattern>
27
</servlet-mapping>
28
29
<servlet>
30
<description></description>
31
<display-name>ContextServlet</display-name>
32
<servlet-name>ContextServlet</servlet-name>
33
<servlet-class>com.itheima.context.ContextServlet</servlet-class>
34
</servlet>
35
<servlet-mapping>
36
<servlet-name>ContextServlet</servlet-name>
37
<url-pattern>/context</url-pattern>
38
</servlet-mapping>
39
40 </web-app>

6、测试,访问“http://localhost:8080/WEB13_sevrlet/login.html”

分别输入“Tom”,“123”,登录结果如下:

分别输入“Lucy”,“1234”,登录结果如下:

分别输入“Jack”,“123”,登录结果如下:

转载于:https://www.cnblogs.com/cxq1126/p/7598769.html

最后

以上就是乐观鞋子为你收集整理的Servlet学习(三)——实例:用户登录并记录登陆次数的全部内容,希望文章能够帮你解决Servlet学习(三)——实例:用户登录并记录登陆次数所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(54)

评论列表共有 0 条评论

立即
投稿
返回
顶部