JavaWeb_(Struts2框架)使用Servlet实现用户的登陆
JavaWeb_(Struts2框架)使用Struts框架实现用户的登陆 传送门
JavaWeb_(Struts2框架)Servlet与Struts区别 传送门
MySQL数据库中存在Gary用户,密码为123;第一次登陆时输入错误的密码1234后页面重定向,并输出错误的提示信息,第二次登陆时输入正确的密码,页面跳转到index.jsp中

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:///strutstest</property>
<property name="user">root</property>
<property name="password">123456</property>
<property name="initialPoolSize">5</property>
<property name="maxPoolSize">20</property>
</default-config> <named-config name="oracel">
<property name="driverClass">oracle.jdbc.driver.OracleDriver</property>
<property name="jdbcUrl">jdbc:oracle:thin:@//192.168.40.128/orcl</property>
<property name="user">scott</property>
<property name="password">scott</property>
</named-config> </c3p0-config>
c3p0-config.xml
package com.Gary.web; import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException; import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.apache.commons.beanutils.BeanUtils; import com.Gary.domain.User;
import com.Gary.service.UserService; @WebServlet("/login")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L; public LoginServlet() {
super();
// TODO Auto-generated constructor stub
} protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
User user = new User();
//封装user对象
try {
BeanUtils.populate(user, request.getParameterMap());
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
UserService userService = new UserService();
//传递数据,判断数据库是否有user
boolean success = false;
try {
success = userService.findUser(user);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} if(success) {
//存在用户,登陆成功重定向到index.html
response.sendRedirect(request.getContextPath()+"/index.html");
}else {
request.setAttribute("error", "用户名或密码错误!!");
//不存在,转发到login.jsp
request.getRequestDispatcher("/login.jsp").forward(request, response);
}
} protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response);
} }
LoginServlet.java
package com.Gary.web; import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException; import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.apache.commons.beanutils.BeanUtils; import com.Gary.domain.User;
import com.Gary.service.UserService; @WebServlet("/login")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L; public LoginServlet() {
super();
// TODO Auto-generated constructor stub
} protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
User user = new User();
//封装user对象
try {
BeanUtils.populate(user, request.getParameterMap());
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
UserService userService = new UserService();
//传递数据,判断数据库是否有user
boolean success = false;
try {
success = userService.findUser(user);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} if(success) {
//存在用户,登陆成功重定向到index.html
response.sendRedirect(request.getContextPath()+"/index.html");
}else {
request.setAttribute("error", "用户名或密码错误!!");
//不存在,转发到login.jsp
request.getRequestDispatcher("/login.jsp").forward(request, response);
}
} protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response);
} }
UserService.java
package com.Gary.domain;
public class User {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
User.java
package com.Gary.dao; import java.sql.SQLException; import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler; import com.Gary.domain.User;
import com.yl.lain.utils.C3p0DataSourceUtils; public class UserDao { public User findUser(User user) throws SQLException { QueryRunner runner = new QueryRunner(C3p0DataSourceUtils.getDataSource());
String sql = "select * from user where username = ? and password = ?";
return runner.query(sql, new BeanHandler<User>(User.class),user.getUsername(),user.getPassword()); } }
UserDao.java
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html> <head>
<meta charset="UTF-8"> <link rel="stylesheet" href="css/head.css" />
<link rel="stylesheet" type="text/css" href="css/login.css" />
</head> <body>
<div class="dvhead">
<div class="dvlogo">
<a href="index.html">你问我答</a>
</div>
<div class="dvsearch">10秒钟注册账号,找到你的同学</div>
<div class="dvreg">
已有账号,立即 <a href="login.html">登录</a>
</div>
</div>
<section class="sec">
<form action="${pageContext.request.contextPath }/login" method="post">
<div class="register-box">
<label for="username" class="username_label"> 用 户 名 <input maxlength="20" name="username" type="text" placeholder="您的用户名和登录名" />
</label>
<div class="tips"></div>
</div>
<div class="register-box">
<label for="username" class="other_label"> 设 置 密 码 <input maxlength="20" type="password" name="password" placeholder="建议至少使用两种字符组合" />
</label>
<div class="tips"></div>
</div>
<div class="arguement">
<input type="checkbox" id="xieyi" /> 阅读并同意 <a href="javascript:void(0)">《你问我答用户注册协议》</a> <a href="register.html">没有账号,立即注册</a>
<div class="tips" style="color: red">${error }</div>
</div>
<div class="submit_btn">
<button type="submit" id="submit_btn">立 即 登录</button>
</div>
</form>
</section>
<script src="js/index.js" type="text/javascript" charset="utf-8"></script>
</body>
login.jsp
项目结构

数据库中用户数据

实现过程
通过c3p0-config.xml链接本地数据库
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql:///strutstest</property>
<property name="user">root</property>
<property name="password">123456</property>
web层接受由login.jsp页面的表单/login请求
<form action="${pageContext.request.contextPath }/login" method="post">
<div class="register-box">
<label for="username" class="username_label"> 用 户 名 <input maxlength="20" name="username" type="text" placeholder="您的用户名和登录名" />
</label>
<div class="tips"></div>
</div>
<div class="register-box">
<label for="username" class="other_label"> 设 置 密 码 <input maxlength="20" type="password" name="password" placeholder="建议至少使用两种字符组合" />
</label>
<div class="tips"></div>
</div>
<div class="arguement">
<input type="checkbox" id="xieyi" /> 阅读并同意 <a href="javascript:void(0)">《你问我答用户注册协议》</a> <a href="register.html">没有账号,立即注册</a>
<div class="tips" style="color: red">${error }</div>
</div>
<div class="submit_btn">
<button type="submit" id="submit_btn">立 即 登录</button>
</div>
</form>
在domain层中创建用户User实体
private String username;
private String password;
package com.Gary.domain;
public class User {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
User.java
web层LoginServlet.java处理用户登陆逻辑,由servlet层去调用dao层返回数据库中查询用户信息
public User findUser(User user) throws SQLException {
QueryRunner runner = new QueryRunner(C3p0DataSourceUtils.getDataSource());
String sql = "select * from user where username = ? and password = ?";
return runner.query(sql, new BeanHandler<User>(User.class),user.getUsername(),user.getPassword());
}
dao层得到用户信息后传递给servlet层,servlet层获得user对象后传递给web层
web层中判断是否有用户信息,如果有,那么用户登陆成功后将页面重定向到index.html,否则跳转到login.jsp并显示错误的提示信息(将error封装到request域中在login.jsp中使用${error}标签将信息提示出来)
web层接受到login的dopost请求后调用doGet请求的逻辑处理
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
User user = new User();
//封装user对象
try {
BeanUtils.populate(user, request.getParameterMap());
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
UserService userService = new UserService();
//传递数据,判断数据库是否有user
boolean success = false;
try {
success = userService.findUser(user);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(success) {
//存在用户,登陆成功重定向到index.html
response.sendRedirect(request.getContextPath()+"/index.html");
}else {
request.setAttribute("error", "用户名或密码错误!!");
//不存在,转发到login.jsp
request.getRequestDispatcher("/login.jsp").forward(request, response);
}
}
JavaWeb_(Struts2框架)使用Servlet实现用户的登陆的更多相关文章
- JavaWeb_(Struts2框架)使用Struts框架实现用户的登陆
JavaWeb_(Struts2框架)使用Servlet实现用户的登陆 传送门 JavaWeb_(Struts2框架)Servlet与Struts区别 传送门 MySQL数据库中存在Gary用户,密码 ...
- JavaWeb_(Struts2框架)Servlet与Struts区别
JavaWeb_(SSH)使用Servlet实现用户的登陆 传送门 JavaWeb_(SSH)使用Struts框架实现用户的登陆 传送门 MySQL数据库中存在Gary用户,密码为123:第一次登陆时 ...
- JavaWeb_(Struts2框架)拦截器interceptor
此系列博文基于同一个项目已上传至github 传送门 JavaWeb_(Struts2框架)Struts创建Action的三种方式 传送门 JavaWeb_(Struts2框架)struts.xml核 ...
- JavaWeb_(Struts2框架)Ognl小案例查询帖子
此系列博文基于同一个项目已上传至github 传送门 JavaWeb_(Struts2框架)Struts创建Action的三种方式 传送门 JavaWeb_(Struts2框架)struts.xml核 ...
- JavaWeb_(Struts2框架)参数传递之接收参数与传递参数
此系列博文基于同一个项目已上传至github 传送门 JavaWeb_(Struts2框架)Struts创建Action的三种方式 传送门 JavaWeb_(Struts2框架)struts.xml核 ...
- JavaWeb_(Struts2框架)struts.xml核心配置、动态方法调用、结果集的处理
此系列博文基于同一个项目已上传至github 传送门 JavaWeb_(Struts2框架)Struts创建Action的三种方式 传送门 JavaWeb_(Struts2框架)struts.xml核 ...
- JavaWeb_(Struts2框架)Action中struts-default下result的各种转发类型
此系列博文基于同一个项目已上传至github 传送门 JavaWeb_(Struts2框架)Struts创建Action的三种方式 传送门 JavaWeb_(Struts2框架)struts.xml核 ...
- JavaWeb_(Struts2框架)Log4j的配置以及解决中文乱码
此系列博文基于同一个项目已上传至github 传送门 JavaWeb_(Struts2框架)Struts创建Action的三种方式 传送门 JavaWeb_(Struts2框架)struts.xml核 ...
- JavaWeb_(Struts2框架)Struts创建Action的三种方式
此系列博文基于同一个项目已上传至github 传送门 JavaWeb_(Struts2框架)Struts创建Action的三种方式 传送门 JavaWeb_(Struts2框架)struts.xml核 ...
随机推荐
- PowerBI 实现不同角色看到内容不同支持动态权限管理
首先,在PowerBIDesktop中进行设计,先设计一个权限表: 具体权限如下: 也就是说,这些用户账号在PowerBIService登录时,会分别代表这些用户,接下来会使用一个很重要的动态函数:U ...
- opencv 模板匹配, 已解决模板过大程序不工作的bug
#include <opencv2/opencv.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv ...
- java后台读取配置文件
前几天开发时遇到一个问题,在后台读取配置文件的时候无法读取属性值,于是上网查了查,现在在这分享给大家: 先附上代码吧: package com.shafei.util; import java.io. ...
- Spring讲解-----------表达式语言
转自:https://blog.csdn.net/u011225629/article/details/47143083 5.1 概述5.1.1 概述 Spring表达式语言全称为“S ...
- <%%> <%! %> <%=%> <%-- --%> jsp中jstl一些运用
<%%> 这里面可以添加java代码片段<%! %> 这里添加java方法 主要是用来声明变量的 <%=%> 将变量或表达式值输出到页面<%-- --%> ...
- C# 使用Quartz.Net
//首先在Nuget上下载 Quartz包 但是由于我睿智Nuget 怎么也没法用 于是找到了这个 解决方法: 1.点击右侧的设置按钮, 2.弹出窗中左侧树形结构选择“程序包源”,再点击右上方的添加按 ...
- vue项目图标
项目图标iconfont 网址:http://www.iconfont.cn
- Linux命令详解——vmstat
Vmstat命令详解 一.前言 vmstat命令: 用来获得有关进程.虚存.页面交换空间及 CPU活动的信息.这些信息反映了系统的负载情况 二.虚拟内存运行原理 在系统中运行的每个进程都需要使用到内 ...
- centos 7 安装 Docker Engine-CentOS 社区版
获取Docker Engine-CentOS社区: https://docs.docker.com/install/linux/docker-ce/centos/ 1.操作系统要求 1.1 要安装 D ...
- 蓝牙App漏洞系列分析之一CVE-2017-0601
蓝牙App漏洞系列分析之一CVE-2017-0601 0x01 概要 2017年5月的 Android 安全公告修复了我们提交的一个蓝牙提权中危漏洞,这个漏洞尽管简单,但比较有意思,能够使本地恶意 A ...