使用Spring框架實(shí)現(xiàn)用戶登錄
本文實(shí)例為大家分享了用Spring框架實(shí)現(xiàn)用戶登錄的具體代碼,供大家參考,具體內(nèi)容如下
流程:用戶登錄需求
登錄頁(yè)面login.jsp>>輸入用戶名username和密碼password,如果用戶名和密碼在數(shù)據(jù)庫(kù)中能找到,則實(shí)現(xiàn)登錄成功界面hello.jsp,不成功則跳轉(zhuǎn)到失敗頁(yè)面error.jsp
1.創(chuàng)建項(xiàng)目架構(gòu)
(1)創(chuàng)建Maven項(xiàng)目
Add Maven Property >> Name:archetypeCatalog >> value:internal
(2)添加本地?cái)?shù)據(jù)倉(cāng)庫(kù)
(3)創(chuàng)建項(xiàng)目目錄
1)groupId:指代公司名 >> com.zzx >> artifactId:指代項(xiàng)目名 >> spring_login
2)在項(xiàng)目根目錄底下創(chuàng)建文件夾target;
3)在src >> main >> java 設(shè)置為Sources Root; src >> main >> resources 設(shè)置為Resources Root;
4)加載Pom.xml文件架包,一般此文件在實(shí)際研發(fā)中都是直接由架構(gòu)師來(lái)完成的操作。一般需要注意架包版本的一致性,文件版本。
5)關(guān)于數(shù)據(jù)庫(kù)設(shè)計(jì):tb_user
uid(用戶編號(hào)),username(用戶名),password(用戶密碼),tid(用戶類型)。
resources >> db.properties
model、entity >> bean >> User
Spring核心配置文件applicationContext.xml
2.以下為具體代碼:
Spring核心配置文件 applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" ? ? ? ?xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ? ? ? ?xmlns:context="http://www.springframework.org/schema/context" ? ? ? ?xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd ? ? ? ?http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> ? ? <!-- 1.加載db.properties取到有效參數(shù) --> ? ? <context:property-placeholder location="classpath:db.properties" /> ? ? <!-- 2.加載數(shù)據(jù)源 --> ? ? <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> ? ? ? ? <property name="driverClassName" value="${driverClass}" /> <!-- EL表達(dá)式,JSTL --> ? ? ? ? <property name="url" value="${url}" /> ? ? ? ? <property name="username" value="${user}" /> ? ? ? ? <property name="password" value="${password}" /> ? ? </bean> ? ? <!-- 3.創(chuàng)建jdbcTemplate對(duì)象 --> ? ? <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> ? ? ? ? <property name="dataSource" ref="dataSource" /> ? ? </bean> ? ? <!-- 4.開(kāi)啟注解作用 --> ? ? <context:component-scan base-package="com.zzx" /> ? ? <bean id="userDao" class="com.zzx.dao.impl.UserDaoImpl" /> ? ? <bean id="userService" class="com.zzx.service.impl.UserServiceImpl" /> </beans>
連接數(shù)據(jù)庫(kù) db.properties
driverClass=com.mysql.jdbc.Driver url=jdbc:mysql://127.0.0.1:3306/db_java1ssm?useSSL=true&characterEncoding=utf-8 user=root password=123456?
webapp下配置web.xml文件
<!DOCTYPE web-app PUBLIC ? ? ? ? "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" ? ? ? ? "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> ? ? <display-name>Archetype Created Web Application</display-name> ? ? <!-- 1.配置applicationContext.xml --> ? ? <context-param> ? ? ? ? <param-name>contextConfigLocation</param-name> ? ? ? ? <param-value>classpath*:applicationContext.xml</param-value> ? ? </context-param> ? ? <!-- 2.監(jiān)聽(tīng)器 --> ? ? <listener> ? ? ? ? <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> ? ? </listener> </web-app>
jsp頁(yè)面
<%--login.jsp--%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> ? ? <title>用戶登錄</title> </head> <body> <div id="content" align="center"> ? ? ? ? <h3>用戶登錄</h3> ? ? ? ? <hr> ? ? ? ? <form action="loginServlet.do" method="post"> ? ? ? ? <table border="1" cellpadding="0" cellspacing="0" width="300px"> ? ? ? ? ? ? <tr> ? ? ? ? ? ? ? ? <td><label for="username">用戶名:</label></td> ? ? ? ? ? ? ? ? <td><input type="text" id="username" name="username"/></td> ? ? ? ? ? ? </tr> ? ? ? ? ? ? <tr> ? ? ? ? ? ? ? ? <td><label for="password">用戶密碼:</label></td> ? ? ? ? ? ? ? ? <td><input type="text" id="password" name="password"/></td> ? ? ? ? ? ? </tr> ? ? ? ? ? ? <tr> ? ? ? ? ? ? ? ? <td colspan="2"> <input type="submit" name="submit" id="submit" value="登錄"/></td> ? ? ? ? ? ? </tr> ? ? ? ? </table> ? ? </form> </div> </body> </html> <%--hello.jsp--%> <%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %> <html> <head> ? ? <title>登錄成功頁(yè)面</title> </head> <body> <font>歡迎您,${user.username}</font> </body> </html> <%--error.jsp--%> <%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %> <html> <head> ? ? <title>登錄失敗頁(yè)面</title> </head> <body> <font>登錄失?。≌?qǐng)你重新登錄!</font> </body> </html>
bean層User類
public class User { ? ? private int uid; ? ? private String username; ? ? private String password; ? ? private int tid; ? ? public int getUid() { ? ? ? ? return uid; ? ? } ? ? public void setUid(int uid) { ? ? ? ? this.uid = uid; ? ? } ? ? 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; ? ? } ? ? public int getTid() { ? ? ? ? return tid; ? ? } ? ? public void setTid(int tid) { ? ? ? ? this.tid = tid; ? ? } ? ? public User() { ? ? } ? ? public User(int uid, String username, String password, int tid) { ? ? ? ? this.uid = uid; ? ? ? ? this.username = username; ? ? ? ? this.password = password; ? ? ? ? this.tid = tid; ? ? } ? ? public User(String username, String password, int tid) { ? ? ? ? this.username = username; ? ? ? ? this.password = password; ? ? ? ? this.tid = tid; ? ? } ? ? @Override ? ? public String toString() { ? ? ? ? return "User{" + ? ? ? ? ? ? ? ? "uid=" + uid + ? ? ? ? ? ? ? ? ", username='" + username + '\'' + ? ? ? ? ? ? ? ? ", password='" + password + '\'' + ? ? ? ? ? ? ? ? ", tid=" + tid + ? ? ? ? ? ? ? ? '}'; ? ? } }
dao層UserDao
@Component public interface UserDao { ? ? //用戶登錄 ? ? User doLogin(String username,String password); }
UserDaoImpl
@Component public class UserDaoImpl implements UserDao { ? ? @Autowired ? ? private JdbcTemplate jdbcTemplate; ? ? public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { ? ? ? ? this.jdbcTemplate = jdbcTemplate; ? ? } ? ? @Override ? ? public User doLogin(String username, String password) { ? ? ? ? String sql = "select * from tb_user where username=? and password=?"; ? ? ? ? BeanPropertyRowMapper<User> mapper = new BeanPropertyRowMapper<>(User.class); ? ? ? ? User user = null; ? ? ? ? user = jdbcTemplate.queryForObject(sql, mapper, username, password); ? ? ? ? return user; ? ? } }
service層UserService
@Service public interface UserService { ? ? //用戶登錄 ? ? User doLogin(String username, String password); }
UserServiceImpl
@Service public class UserServiceImpl implements UserService { ? ? @Autowired ? ? private UserDao userDao ; ? ? public void setUserDao(UserDao userDao) { ? ? ? ? this.userDao = userDao; ? ? } ? ? @Override ? ? public User doLogin(String username, String password) { ? ? ? ? return userDao.doLogin(username,password); ? ? } }
LoginServlet
@WebServlet("*.do") public class LoginServlet extends javax.servlet.http.HttpServlet { ? ? private static final long serialVersionUID =1L; ? ? // 報(bào)錯(cuò)是因?yàn)檫@個(gè)里面少tomcat - 加載tomcat ? ? protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException { ? ? ? ? // 中文亂碼問(wèn)題 : 國(guó)際標(biāo)準(zhǔn)化 ? ? ? ? request.setCharacterEncoding("utf-8"); ? ? ? ? response.setContentType("text/html;charset=UTF-8"); ? ? ? ? // applicationContext.xml ? ? ? ? ApplicationContext ioc = WebApplicationContextUtils.getWebApplicationContext(getServletContext()); ? ? ? ? // 獲取用戶名 ? ? ? ? String username = request.getParameter("username"); ? ? ? ? String password = request.getParameter("password"); ? ? ? ? UserService userService = (UserService) ioc.getBean("userService"); ? ? ? ? User user = userService.doLogin(username, password); ? ? ? ? request.setAttribute("user",user); ? ? ? ? if(user==null){ ? ? ? ? ? ? // 失敗 ? ? ? ? ? ? request.getRequestDispatcher("error.jsp").forward(request,response); ? ? ? ? }else{ ? ? ? ? ? ? request.getRequestDispatcher("hello.jsp").forward(request,response); ? ? ? ? } ? ? } ? ? protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException { ? ? ? ? this.doPost(request,response); ? ? } }
最后配置一下Tomcat服務(wù)器運(yùn)行
下面是我的運(yùn)行截圖
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Springboot+SpringSecurity+JWT實(shí)現(xiàn)用戶登錄和權(quán)限認(rèn)證示例
- Springboot+Shiro記錄用戶登錄信息并獲取當(dāng)前登錄用戶信息的實(shí)現(xiàn)代碼
- SpringMVC 實(shí)現(xiàn)用戶登錄實(shí)例代碼
- springmvc+spring+mybatis實(shí)現(xiàn)用戶登錄功能(上)
- springmvc+spring+mybatis實(shí)現(xiàn)用戶登錄功能(下)
- 利用Spring IOC技術(shù)實(shí)現(xiàn)用戶登錄驗(yàn)證機(jī)制
- Spring mvc 實(shí)現(xiàn)用戶登錄的方法(攔截器)
- spring aop action中驗(yàn)證用戶登錄狀態(tài)的實(shí)例代碼
- 詳解使用Spring3 實(shí)現(xiàn)用戶登錄以及權(quán)限認(rèn)證
- Spring Cloud Gateway 使用JWT工具類做用戶登錄校驗(yàn)功能
相關(guān)文章
Java二進(jìn)制操作(動(dòng)力節(jié)點(diǎn)Java學(xué)院整理)
這篇文章給大家介紹了java二進(jìn)制操作技巧,包括移位、位運(yùn)算操作符等相關(guān)知識(shí)點(diǎn),非常不錯(cuò),感興趣的朋友參考下吧2017-03-03mybatis-plus?執(zhí)行insert(),實(shí)體的id自動(dòng)更新問(wèn)題
這篇文章主要介紹了mybatis-plus?執(zhí)行insert(),實(shí)體的id自動(dòng)更新問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12Spring Boot使用JSR-380進(jìn)行校驗(yàn)的示例
這篇文章主要介紹了Spring Boot使用JSR-380進(jìn)行校驗(yàn),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03實(shí)例解決Java異常之OutOfMemoryError的問(wèn)題
在本篇文章中,我們給大家分享了關(guān)于解決Java異常之OutOfMemoryError的問(wèn)題的方法,有此需要的朋友們學(xué)習(xí)下。2019-02-02Java File類的簡(jiǎn)單使用教程(創(chuàng)建、刪除、遍歷與判斷是否存在等)
這篇文章主要給大家介紹了關(guān)于Java File類的簡(jiǎn)單使用(創(chuàng)建、刪除、遍歷與判斷是否存在等)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12maven <repositories>標(biāo)簽和<pluginRepositories>標(biāo)簽的使用
這篇文章主要介紹了maven <repositories>標(biāo)簽和<pluginRepositories>標(biāo)簽的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07Java Hibernate對(duì)象(瞬時(shí)態(tài),持久態(tài),脫管態(tài))詳解
這篇文章主要介紹了Java Hibernate對(duì)象(瞬時(shí)態(tài),持久態(tài),脫管態(tài))詳解的相關(guān)資料,這里對(duì)Java Hibernate對(duì)象進(jìn)行了介紹及總結(jié),需要的朋友可以參考下2016-11-11SpringMVC+MyBatis 事務(wù)管理(實(shí)例)
本文先分析編程式注解事務(wù)和基于注解的聲明式事務(wù)。對(duì)SpringMVC+MyBatis 事務(wù)管理的相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧2017-08-08MyBatis注解方式之@Update/@Delete使用詳解
這篇文章主要介紹了MyBatis注解方式之@Update/@Delete使用詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-11-11