Spring登录实例

  项目结构

  首先看一下整个项目的目录结构,如下:

  导入Jar包

  工欲善必先利其器,导入一下Jar包

  配置文件

  web.xml

  配置 web.xml配置文件,如下:

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

  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

  http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

  contextConfigLocation

  classpath:applicationContext.xml

  org.springframework.web.context.ContextLoaderListener

  applicationContext.xml

  创建applicationContext.xml配置文件,如下:

  获取数据源,数据库相关配置(注意:要配置自己的数据库相关信息),beanID:dataSource。

  创建 SqlSessionFactory,beanID:factory。

  扫描器配置,扫描接口,并创建接口对象。

  由spring管理service实现类,beanID:userService。

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

  xsi:schemaLocation="http://www.springframework.org/schema/beans

  http://www.springframework.org/schema/beans/spring-beans.xsd">

  value="com.mysql.jdbc.Driver">

  value="jdbc:mysql://localhost:3306/spring">

  class="org.mybatis.spring.SqlSessionFactoryBean">

  class="com.spring.service.impl.UsersServiceImpl">

  创建数据库

  创建对应的用户表,如下:

  

  数据库信息为:spring库,users表。

  代码实现

  创建Users类

  先创建一个用户类Users,如下:

  public class Users {

  private int user;

  private String username;

  private String password;

  public int getUser() {

  return user;

  }

  public void setUser(int user) {

  this.user = user;

  }

  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;

  }

  }

  创建UsersMapper类

  创建UsersMapper类,如下:

  public interface UsersMapper {

  @Select("select * from users where username=#{username} and password=#{password}")

  Users selByUsersPwd(Users users);

  }

  创建UsersService接口

  创建UsersService类,如下:

  public interface UsersService {

  /**

  * 登录

  * @param users

  * @return

  */

  Users login(Users users);

  }

  创建UsersServiceImpl类

  创建UsersService接口实现类,如下:

  public class UsersServiceImpl implements UsersService {

  private UsersMapper usersMapper;

  public UsersMapper getUsersMapper() {

  return usersMapper;

  }

  public void setUsersMapper(UsersMapper usersMapper) {

  this.usersMapper = usersMapper;

  }

  @Override

  public Users login(Users users) {

  // TODO Auto-generated method stub

  return usersMapper.selByUsersPwd(users);

  }

  }

  创建LoginServlet类

  创建LoginServlet类,如下:

  @WebServlet("/login")

  public class LoginServlet extends HttpServlet {

  private UsersService usersService;

  //从spring中取出UsersServiceImpl

  public void init()throws ServletException{

  ApplicationContext ac = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());

  usersService = ac.getBean("usersService",UsersServiceImpl.class);

  }郑州妇科咨询网站 http://www.zzkdfk120.com/

  protected void service(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException {

  req.setCharacterEncoding("utf-8");

  //获取验证码

  String code = req.getParameter("code");

  //到session中获取验证码

  String codeSession = req.getSession().getAttribute("code").toString();

  //判断验证码是否正确

  if(codeSession.equals(code)) {

  //获取用户名

  String username = req.getParameter("username");

  //获取密码

  String password = req.getParameter("password");

  //创建users对象

  Users users = new Users();

  users.setUsername(username);

  users.setPassword(password);

  //登录

  Users user = usersService.login(users);

  if(user!=null) {

  resp.sendRedirect("main.jsp");

  }else {

  req.setAttribute("error", "用户名或密码不正确!");

  req.getRequestDispatcher("index.jsp").forward(req, resp);

  }

  }else {

  req.setAttribute("error", "验证码不正确");

  req.getRequestDispatcher("index.jsp").forward(req, resp);

  }

  }

  }

最新文章

  1. jquery的.html(),.text()和.val()方法
  2. Javascript、Jquery获取浏览器和屏幕各种高度宽度
  3. Node实践之一
  4. eclipse安装插件
  5. dictionary 添加数据
  6. The entity type <type> is not part of the model for the current context
  7. C# 插件
  8. Codeforces Round #271 (Div. 2) F ,E, D, C, B, A
  9. Ajax学习笔记(1)
  10. java多线层同时运行的解决,同步代码块synchronized
  11. Markdown的基本语法记录
  12. 【PAT】反转链表
  13. 易语言Dns缓存
  14. 《DSP using MATLAB》Problem 6.15
  15. 【转】C# 的 IDisposable 接口
  16. 动态权限<二>之淘宝、京东、网易新闻 权限申请交互设计对比分析
  17. Django 基础篇章
  18. WPF MVVM 之理解(数据绑定)
  19. 微软智能云的核心DNA
  20. 余弦相似性计算及python代码实现

热门文章

  1. 多线程CGD调度组原理
  2. 挂载system.img并提取文件
  3. 密度峰值聚类算法(DPC)
  4. MongoDB Java(七)
  5. USACO Corn Fields
  6. 牛客小白月赛18 Forsaken给学生分组
  7. GitHub 注册失败的原因 以及解决 。
  8. 冒泡排序法(C语言)
  9. [学习笔记] 舞蹈链(DLX)入门
  10. 第02组 Beta冲刺(1/5)