session.getAttribute and session.setAttribute in Spring MVC
Here is an example of how to use session.setAttribute and session.getAttribute, the same way we use in Servlets. (for Spring mvc simple form handling example-click here and for Multiple submit buttons in spring mvc form – click here).
In this example we are going to see how to use “session.setAttribute” and “session.getAttribute”. We are going to get username in page1.jsp and then control passes to page2.jsp and we are going to display the username in page3.jsp using session attributes.
employee.java
package com.javainfinite.pojo; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; @Component @Scope("session") public class employee { String ename; public employee(String ename) { this.ename = ename; } public employee() { } public String getEname() { return ename; } public void setEname(String ename) { this.ename = ename; } }
page1.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%> <%@taglib uri="http://www.springframework.org/tags/form" prefix="s"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Employee Name</title> </head> <body> <s:form method="POST"> <table> <tr> <td> Username: </td> <td><input type="text" name="ename"/></td> </tr> <tr> <td colspan="2"><input type="submit" name="Success" value="Login"/></td> </tr> </table> </s:form> </body> </html>
page2.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%> <%@taglib uri="http://www.springframework.org/tags/form" prefix="s"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Lets Go!</title> </head> <body> <s:form method="POST"> <input type="submit" value="Check Session" name="check"> </s:form> </body> </html>
page3.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Show Session</title> </head> <body> Session Attribute that is set in first page is: ${uname} </body> </html>
econtroller.java
package com.javainfinite.controller; import com.javainfinite.pojo.employee; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.portlet.ModelAndView; @Controller @RequestMapping(value="/page1") public class econtroller { ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml"); @RequestMapping(method=RequestMethod.GET) public ModelAndView Mod() { return new ModelAndView("page1"); } @RequestMapping(params="Success", method=RequestMethod.POST) public String sess(@ModelAttribute ("employee") employee emp, HttpServletRequest request, HttpSession session) { session.setAttribute("uname", emp.getEname()); return "page2"; } @RequestMapping(value="/page2",method=RequestMethod.GET) public ModelAndView mod() { return new ModelAndView("page2"); } @RequestMapping(params="check", method=RequestMethod.POST) public String Mod1(ModelMap modelmap,HttpSession session, HttpServletRequest request) { String uname=(String)request.getSession().getAttribute("uname"); modelmap.put("uname", uname); return "page3"; } }
applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <bean id="employee" class="com.javainfinite.pojo.employee"/> </beans>
dispatcher-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/> <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="index.htm">indexController</prop> </props> </property> </bean> <context:component-scan base-package="com.javainfinite"/> <mvc:annotation-driven/> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" /> <!-- The index controller. --> <bean name="indexController" class="org.springframework.web.servlet.mvc.ParameterizableViewController" p:viewName="index" /> </beans>
Output