Spring登录实例

Spring登录实例

  • 上一个教程已经完成了Spring验证码实例,本章我们来完善登录实例。

项目结构

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

dir

导入Jar包

  • 工欲善必先利其器,导入一下Jar包,对应的jar包我已经分享在百度云网盘,大家可以下载,网址如下:
链接:https://pan.baidu.com/s/1m5NCIQeiKZn5llosp7Iv6A 
提取码:s5l2 

spring_jar

配置文件

web.xml

  • 配置 web.xml配置文件,如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
	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">
	<!-- 上下文参数 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<!-- spring配置文件 -->
		<param-value>classpath:applicationContext.xml</param-value>
	</context-param>
	
	<!-- 封装了一个监听器,帮助加载Spring的配置文件 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
</web-app>

applicationContext.xml

  • 创建applicationContext.xml配置文件,如下:
    • 获取数据源,数据库相关配置(注意:要配置自己的数据库相关信息),beanID:dataSource。
    • 创建 SqlSessionFactory,beanID:factory。
    • 扫描器配置,扫描接口,并创建接口对象。
    • 由spring管理service实现类,beanID:userService。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	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">
	<!-- 数据源封装类,数据源:获取数据库连接 -->
	<bean id="dataSouce"	class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName"
			value="com.mysql.jdbc.Driver"></property>
		<property name="url"
			value="jdbc:mysql://localhost:3306/spring"></property>
		<property name="username" value="root"></property>
		<property name="password" value="123456"></property>
	</bean>
    <!--spring帮助创建SqlSessionFactory-->
	<bean id="factory"
class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSouce"></property>
	</bean>
    
	<!-- 扫描器相当于mybatis.xml中的mappers下package标签 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<!-- 要扫描哪个包 -->
		<property name="basePackage" value="com.spring.mapper"></property>
		<!-- 和factory产生关系 -->
		<property name="sqlSessionFactory" ref="factory"></property>
	</bean>
    
	<!-- 由spring管理service实现类 -->
	<bean id="userService"
		class="com.spring.service.impl.UsersServiceImpl">
		<property name="usersMapper" ref="usersMapper"></property>
	</bean>
</beans>

创建数据库

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

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);
	}
	
	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);
			
		}	
	}
}

测试实例

  • 输入验证码错误,如下:
    login_validcode01

  • 报错页面,如下:

login_validcode02

  • 输入用户名或者密码错误,如下:

login_failed01

  • 报错页面,如下:
    login_failed02

  • 成功页面,如下:

login_success

  • 9
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 8
    评论
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

i白

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值