Use Spring IOC technology to realize the verification mechanism for user login and verify users' login.
First, use Spring's automatic assembly mode to inject the User object into the controller, and then match the username and password entered by the user with the username and password of the legal user specified in the system.
When the username and password match successfully, jump to the login successful page; when the username and password do not match, jump to the page where the login failed.
1. Create a User object, define the user name and password attributes, the code is as follows:
package com.importnew;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; }}2. Create the controller TestUtil, inject the User object and log in to verify. The code is as follows:
package com.importnew;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.web.servlet.ModelAndView;import org.springframework.web.servlet.mvc.AbstractController;public class TestUtil extends AbstractController{ private User user; public User getUser() { return user; } public void setUser(User user) { this.user = user; } @Override protected ModelAndView handleRequestInternal(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception { String username = arg0.getParameter("username"); String password = arg0.getParameter("password"); if(username.equals(user.getUsername()) && password.equals(user.getPassword())){ return new ModelAndView("yes"); }else{ return new ModelAndView("Error"); } }}3. Assign values to the properties of the User object in Spring's configuration file applicationContext.xml, and inject the User object into the controller TestUtil using automatic assembly. The code is as follows:
<?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" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" 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 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="viewResolver"> <property name="prefix"> <value>/</value> </property> <property name="suffix"> <value>.jsp</value> </property> </bean> <bean id="user" > <property name="username"> <value>admin</value> </property> <property name="password"> <value>123</value> </property> </bean> <bean autowire="byName" id="testUtil" > <property name="user"> <ref bean="user"/> </property> </bean></beans>
4. Configure the automatic loading of applicationContext.xml in the web.xml file. When the project is started, the program will automatically load the information in the configuration file. The code is as follows:
<!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> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/applicationContext.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>
Remark:
The class AbstractController inherited in TestUtil requires the JAR package support for spring-web-mvc.
////end
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.