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">
已有账号,立即&nbsp;<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实现用户的登陆的更多相关文章

  1. JavaWeb_(Struts2框架)使用Struts框架实现用户的登陆

    JavaWeb_(Struts2框架)使用Servlet实现用户的登陆 传送门 JavaWeb_(Struts2框架)Servlet与Struts区别 传送门 MySQL数据库中存在Gary用户,密码 ...

  2. JavaWeb_(Struts2框架)Servlet与Struts区别

    JavaWeb_(SSH)使用Servlet实现用户的登陆 传送门 JavaWeb_(SSH)使用Struts框架实现用户的登陆 传送门 MySQL数据库中存在Gary用户,密码为123:第一次登陆时 ...

  3. JavaWeb_(Struts2框架)拦截器interceptor

    此系列博文基于同一个项目已上传至github 传送门 JavaWeb_(Struts2框架)Struts创建Action的三种方式 传送门 JavaWeb_(Struts2框架)struts.xml核 ...

  4. JavaWeb_(Struts2框架)Ognl小案例查询帖子

    此系列博文基于同一个项目已上传至github 传送门 JavaWeb_(Struts2框架)Struts创建Action的三种方式 传送门 JavaWeb_(Struts2框架)struts.xml核 ...

  5. JavaWeb_(Struts2框架)参数传递之接收参数与传递参数

    此系列博文基于同一个项目已上传至github 传送门 JavaWeb_(Struts2框架)Struts创建Action的三种方式 传送门 JavaWeb_(Struts2框架)struts.xml核 ...

  6. JavaWeb_(Struts2框架)struts.xml核心配置、动态方法调用、结果集的处理

    此系列博文基于同一个项目已上传至github 传送门 JavaWeb_(Struts2框架)Struts创建Action的三种方式 传送门 JavaWeb_(Struts2框架)struts.xml核 ...

  7. JavaWeb_(Struts2框架)Action中struts-default下result的各种转发类型

    此系列博文基于同一个项目已上传至github 传送门 JavaWeb_(Struts2框架)Struts创建Action的三种方式 传送门 JavaWeb_(Struts2框架)struts.xml核 ...

  8. JavaWeb_(Struts2框架)Log4j的配置以及解决中文乱码

    此系列博文基于同一个项目已上传至github 传送门 JavaWeb_(Struts2框架)Struts创建Action的三种方式 传送门 JavaWeb_(Struts2框架)struts.xml核 ...

  9. JavaWeb_(Struts2框架)Struts创建Action的三种方式

    此系列博文基于同一个项目已上传至github 传送门 JavaWeb_(Struts2框架)Struts创建Action的三种方式 传送门 JavaWeb_(Struts2框架)struts.xml核 ...

随机推荐

  1. XML转换成DataTable

    #region XML转dataset //str 是xml字符串 public static DataTable GetResultXMLToDataTable (string str,string ...

  2. (三)创建基于maven的javaFX+springboot项目创建

    创建基于maven的javaFx+springboot项目有两种方式,第一种为通过非编码的方式来设计UI集成springboot:第二种为分离用户界面(UI)和后端逻辑集成springboot,其中用 ...

  3. Centos7:Redis的安装,配置及使用

    安装依赖与环境 yum install gcc-c++ 解压缩redis 编译,进入redis源码目录 make 安装 make install PREFIX=/usr/local/redis 注:P ...

  4. SSH远程连接工具汇总

    1)Xshell 常见问题: 1) 终端中的字体横向显示 字体中带有@的均为横向字体, 只要选择一个不带@的字体即可 2)putty 常见问题: 1)putty中编辑脚本,文字呈现蓝色,辨识度较差,需 ...

  5. 初识JavaScript对象

    JavaScript对象语法.类型.属性 属性描述符(getOwnPropertyDescriptor().defineProperty()) [[Get]].[[Put]].Getter.Sette ...

  6. httpclient 上传附件实例

    httpclient 单附件上传实例  (扩展多附件上传实例,点我) /** * 上传附件 * @param host * @param uri * @param filePath 文件路径 * @p ...

  7. JDBC及PreparedStatement防SQL注入

    概述 JDBC在我们学习J2EE的时候已经接触到了,但是仅是照搬步骤书写,其中的PreparedStatement防sql注入原理也是一知半解,然后就想回头查资料及敲测试代码探索一下.再有就是我们在项 ...

  8. Windows环境中编译opencv3.0同时加入OPENCV_contrib库及解决遇到相关问题[contrib 必须要3.1以上的opencv才支持了]

    更新:现在contrib库必须要opencv3.1以上才能支持编译通过了. 方法和步骤还是和本篇文章一样. ############################################## ...

  9. 网络协议相关面试问题-https加密算法相关面试问题

    密钥: 密钥是一种参数,它是在使用密码cipher算法过程中输入的参数,同一个明文在相同的密码算法和不同的密钥计算下会产生不同的密文.所以说算法既使公开了但是密钥参数不同其结果也是不同的,其中关于明文 ...

  10. php一些常用单词(供初学者背)

    > property         性质 特性 财产 所有权> oriented         导向> summarize     概括 总结> register      ...