Springmvc using session for simple login

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.