RequestContextHolder in Spring MVC

RequestContextHolder is a spring API for setting the values to any of three scopes request, session or global session. Note that in some situations, we may not be able to get the actual request from the request, we can simply use RequestContextHolder to get the request attributes and set the values. We have to explicitly pass the scope id as the parameter. RequestContextHolder has the following three methods:

  • currentRequestAttributes() – Return the RequestAttributes currently bound to the thread.
  • getRequestAttributes() – Return the RequestAttributes currently bound to the thread.
  • resetRequestAttributes() – Reset the RequestAttributes for the current thread.
  • setRequestAttributes(RequestAttributes attributes) – Bind the given RequestAttributes to the current thread, not exposing it as inheritable for child threads.
  • setRequestAttributes(RequestAttributes attributes, boolean inheritable) – Bind the given RequestAttributes to the current thread.

Lets look at the example.

1. Spring MVC Controller

SpringMVCController.java

package javabeat.net.spring.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;

@Controller
public class SpringMVCController {

	@RequestMapping(value = "/springmvctest", method = RequestMethod.GET)
	public String redirectTest() {

		//Setting value to HttpSession object
		RequestContextHolder.getRequestAttributes().setAttribute("KEY_VALUE",
				"TEST_VALUE", RequestAttributes.SCOPE_SESSION);
		return "hello";
	}
}

2. Views

hello.jsp

<html>
<body>
	<h1>JavaBeat Spring MVC - RequestContextHolder</h1>
	<h2>Session Value : ${KEY_VALUE}</h2>
</body>
</html>

3. Spring Configurations

spring-dispatcher-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd


http://www.springframework.org/schema/mvc


http://www.springframework.org/schema/mvc/spring-mvc.xsd


http://www.springframework.org/schema/context


http://www.springframework.org/schema/context/spring-context-3.0.xsd">

	<context:component-scan base-package="javabeat.net.spring.controller" />
	<bean id="jspViewResolver"
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/jsp/" />
		<property name="suffix" value=".jsp" />
	</bean>
</beans>

4. Deployment Descriptor

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"

id="WebApp_ID" version="3.0">
<display-name>Spring MVC Web Application</display-name>
	<servlet>
		<servlet-name>spring-dispatcher</servlet-name>
		<servlet-class>
			org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>spring-dispatcher</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
</web-app>

5. RequestContextHolder Demo

RequestContextHolder in Spring MVC

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Speak Your Mind

*