Servlet学习(三)——实例:用户登录并记录登陆次数

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

2、创建HTML文件,命名为login,作为登录界面(以post方式提交)
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="/WEB13_sevrlet/login" method="post">
用户名:<input type="text" name="username"><br/>
密码:<input type="password" name="password"><br/>
<input type="submit" value="登录"><br/>
</form>
</body>
</html>
3、创建连接池
c3p0-config.xml文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config> <default-config>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql:///web13</property>
<property name="user">root</property>
<property name="password">12345</property>
</default-config> </c3p0-config>
DataSourceUtils.java文件如下:(可作为套用模板)
package com.itheima.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();
}
}
}
4、编写Servlet实现类
User类代码如下:
package com.itheima.domain;
public class User {
private int id;
private String name;
private String password;
public User(){
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
LoginServlet.java代码如下:
package com.itheima.login; import java.io.IOException;
import java.sql.SQLException; import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler; import com.itheima.domain.User;
import com.itheima.utils.DataSourceUtils; public class LoginServlet extends HttpServlet { @Override
public void init() throws ServletException{
//在Servletcontext域中存一个数据count
int count=0;
this.getServletContext().setAttribute("count", count);
} public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { //1.获得用户名和密码
String username=request.getParameter("username");
String password=request.getParameter("password"); //2.从数据库中验证该用户名和密码是否正确
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),username,password);
} catch (SQLException e) {
e.printStackTrace();
}
//3.根据返回的结果给用户不同显示信息
if(user!=null){
//从Servletcontext中取出count进行++运算
ServletContext context=this.getServletContext();
Integer count=(Integer)context.getAttribute("count");
count++;
//用户登录成功
response.getWriter().write(user.toString()+"--you are success login person"+count);
context.setAttribute("count", count);
}else{
//用户登录失败
response.getWriter().write("Sorry,your username or password is wrong!");
}
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
5、编写Servlet配置文件,代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<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">
<display-name>WEB13_sevrlet</display-name>
<context-param>
<param-name>driver</param-name>
<param-value>com.mysql.jdbc.Driver</param-value>
</context-param> <welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list> <servlet>
<description></description>
<display-name>LoginServlet</display-name>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.itheima.login.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping> <servlet>
<description></description>
<display-name>ContextServlet</display-name>
<servlet-name>ContextServlet</servlet-name>
<servlet-class>com.itheima.context.ContextServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ContextServlet</servlet-name>
<url-pattern>/context</url-pattern>
</servlet-mapping> </web-app>
6、测试,访问“http://localhost:8080/WEB13_sevrlet/login.html”
分别输入“Tom”,“123”,登录结果如下:


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

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

Servlet学习(三)——实例:用户登录并记录登陆次数的更多相关文章
- 云服务器 ECS Linux 保存用户登录操作命令记录
转载自 : https://help.aliyun.com/knowledge_detail/41210.html 云服务器 ECS Linux 如果要保存用户登录操作记录,则可以通过在 /etc/p ...
- 基于Servlet的MVC模式用户登录实例
关于MVC模式的简单解释 M Model,模型层,例如登录实例中,用于处理登录操作的类: V View,视图层,用于展示以及与用户交互.使用html.js.css.jsp.jQuery等前端技术实现: ...
- Cookie的应用——Servlet实现三天免登录
1.工程结构: 2.Servlet的运用: (1)登录界面: protected void doGet(HttpServletRequest request, HttpServletResponse ...
- mybatis 和servlet使用MVC实现用户登录,注册,退出
普通实现: USerMapper.java: package com.bjsxt.mapper; import org.apache.ibatis.annotations.Param; import ...
- DBCP(MySql)+Servlet+BootStrap+Ajax实现用户登录与简单用户管理系统
目 录 简介 本次项目通过Maven编写 本文最后会附上代码 界面截图 登录界面 注册界面 登录成功进入主页 增加用户操作 删除用户操作 修改用户操作 主要代码 Dao层代码 DBCP代码 Se ...
- Servlet课程0426(十二)Servlet MV模式下用户登录及查看用户表中所有用户
Welcome.java //登录界面 package com.tsinghua; import javax.servlet.http.*; import java.io.*; import java ...
- linux学习(三)-----linux用户管理、实用指令
用户管理 基本介绍 说明: 1.Linux 系统是一个多用户多任务的操作系统,任何一个要使用系统资源的用户,都必须首先向 系统管理员申请一个账号,然后以这个账号的身份进入系统. 2.Linux 的用户 ...
- Servlet学习三——传输文件
最先在考虑传输文件时,想通过java写一个文件上传案例,传给Servlet,Servlet再保存至数据库中,但苦于一直没找到实例,听说Flex有实际的例子,就直接用Flex例子来测试了.本文的顺序为: ...
- XMPP学习——2、用户登录
最近在学习XMPP的使用,打算完成一个完整较为完整地Demo示例,通过这个示例掌握xmpp的使用与开发.同时打算在这个示例中学习使用一下其他的开源类库,在此作为记录学习. 包括服务器端--Openfi ...
随机推荐
- Spring《五》集合的注入方式
List.Set.Map.Properties 1.List <property name="msg"> <list> <value>gf< ...
- layui的多文件列表上传功能前端代码
html页面的代码(注意:引入layui相关的css): <div class="layui-upload" style="margin-left: 130px&q ...
- <Android Framework 之路>Android5.1 Camera Framework(四)——框架总结
前言 从之前的几篇文件,可以基本弄清楚 Camera从APK,经过framework的衔接,与HAL层进行交互,最终通过驱动完成Camera的一些动作. Camera层次分析 APP层 Framewo ...
- X Macro
30年前我念大学时从一个朋友那里学来的一个技巧. 它是汇编语言的一个宏,但很容易转换为C语言宏. 我一直在使用它,但有意思的是我还从没在别人的代码中看到过.现在该我把这个小技巧传递下去了. 让我们举个 ...
- Dynamic programming language
动态改变运行时结构 Dynamic programming language, in computer science, is a class of high-level programming la ...
- 带你学C带你飞!
C语言免费课程推荐:带你学C带你飞! 想学习C语言,首先就要了解什么是C语言: C语言是一门通用计算机编程语言,应用广泛.C语言的设计目标是提供一种能以简易的方式编译.处理低级存储器.产生少量的机器码 ...
- Pyhton学习——Day11
# Python中的内部模块# 函数学习的意义:抽取重复代码# 模块:不用重复写,模块及py文件,提高了代码的可维护性,其次,编写代码不必从零开始,当一个模块编写完毕,不必再重复编写# import ...
- 密信(MeSince),将取代传统电子邮件
电子邮件发展至今已经有几十年的历史,但仍然是最重要的现代互联网应用之一.在全球范围内,每小时发送的非垃圾邮件数量超过30亿封,从工作场景的使用到个人生活,电子邮件都扮演着不可或缺的角色.但是由于明文电 ...
- 12、Camel: Content-Aware and Meta-path Augmented Metric Learning for Author Identification----作者识别
摘自:https://blog.csdn.net/me_yundou/article/details/80459341 具体看上面链接 一.摘要: 这篇文章主要介绍的是作者识别(author iden ...
- [读书笔记] R语言实战 (十三) 广义线性模型
广义线性模型扩展了线性模型的框架,它包含了非正态的因变量分析 广义线性模型拟合形式: $$g(\mu_\lambda) = \beta_0 + \sum_{j=1}^m\beta_jX_j$$ $g( ...