1. First in order to be able to visualize the content of the session in the JSP page, I used the JSTL expression, first introduced in the Pom.xml jstl dependency
<!--the dependency required by the Jstl - <Dependency> <groupId>Jstl</groupId> <Artifactid>Jstl</Artifactid> <version>1.2</version> </Dependency>
2. Write login interface and login successful interface
<%@ Page ContentType="text/html; Charset=utf-8"pageencoding="Utf-8"%> <!DOCTYPE HTML> <HTML> <Head> <MetaCharSet= "Utf-8"> <title>User Login</title> </Head> <Body> <formAction= "Login"Method= "POST"> <inputtype= "text"name= "Uname" /><BR/><BR/> <inputtype= "Password"name= "pwd" /><BR/><BR/> <inputtype= "Submit"value= "Login"><BR/><BR/> </form> </Body> </HTML>
Login Success Screen
<%@ Page ContentType="text/html; Charset=utf-8"pageencoding="Utf-8"%> <%@taglib URI="Http://java.sun.com/jsp/jstl/core"prefix="C" %><!--Introducing the JSTL library - <!DOCTYPE HTML> <HTML> <Head> <MetaCharSet= "Utf-8"> <title>Success</title> </Head> <Body> <H1>${SESSIONSCOPE.USER.UNAME} Login Successful!!!</H1> <H2>Welcome, ${sessionscope.uname}</H2> </Body> </HTML>
3. Writing request processing classes and entity classes
Packagecom.yc.entity; Public classUser {PrivateString uname; PrivateString pwd; PublicString Getuname () {returnuname; } Public voidsetuname (String uname) { This. uname =uname; } PublicString getpwd () {returnpwd; } Public voidsetpwd (String pwd) { This. PWD =pwd; } PublicString toString () {return"User [uname=" + uname + ", pwd=" + pwd + "]"; } }
Request Processing Class
Packagecom.yc.controllers; ImportOrg.springframework.stereotype.Controller; ImportOrg.springframework.ui.ModelMap; ImportOrg.springframework.web.bind.annotation.ModelAttribute; Importorg.springframework.web.bind.annotation.RequestMapping; Importorg.springframework.web.bind.annotation.SessionAttributes; ImportCom.yc.entity.User; @Controller @SessionAttributes ({"User", "uname"}) Public classLogincontroller {@RequestMapping ("/login") PublicString Login (User user,modelmap map) {//user will inject himself into the session//put the uname into the session scope so that the forwarding page can also fetch this data. Map.addattribute ("uname", User.getuname ()); return"Loginsuccess"; } }
4. Run the test
OK, by now we can use the session implementation of the user login is OK
Note: Reference link: http://blog.csdn.net/qq_26624661/article/details/52094721
Springmvc using session for simple login