Spring登录实例

  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">

  创建数据库

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

  

database

  数据库信息为: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);

  }

  }

  }

原文地址:https://www.cnblogs.com/djw12333/p/12067318.html