JavaBeat

  • Home
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Privacy
  • Contact Us

RequestContextHolder in Spring MVC

May 15, 2014 by Krishna Srinivasan Leave a Comment

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

[code lang=”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";
}
}
[/code]

2. Views

hello.jsp

[code lang=”xml”]
<html>
<body>
<h1>JavaBeat Spring MVC – RequestContextHolder</h1>
<h2>Session Value : ${KEY_VALUE}</h2>
</body>
</html>
[/code]

3. Spring Configurations

spring-dispatcher-servlet.xml

[code lang=”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>
[/code]

4. Deployment Descriptor

web.xml

[code lang=”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>
[/code]

5. RequestContextHolder Demo

RequestContextHolder in Spring MVC

[wpdm_file id=96]

Filed Under: Spring Framework Tagged With: Spring MVC

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.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Follow Us

  • Facebook
  • Pinterest

As a participant in the Amazon Services LLC Associates Program, this site may earn from qualifying purchases. We may also earn commissions on purchases from other retail websites.

JavaBeat

FEATURED TUTORIALS

Answered: Using Java to Convert Int to String

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Copyright © by JavaBeat · All rights reserved