23. Accessing Request and Session Parameters

PROBLEM

HTML data is typically bound to command objects (domain objects or data transfer objects), however there are occasions where you want to access data directly from the request, response or session objects.

SOLUTION

Spring supports adding parameters to a controller method for accessing various objects. If you add a reference to these objects in a method parameter, Spring will automatically pass these objects to the method at runtime.

HOW IT WORKS

To access the request object, add a input variable of type javax.servlet.http.HttpServletRequest to a web operation. Use the input variable name to reference the request object from within the operation.

The following example shows a URL parameter that assigns a value ("John") to a variable called name.

http://www.myapplication.com/index.html?name=John

The following java snippet (that can be used using the Invoke Java step with inline option) shows how the URL parameter is retrieved and assigned to an operation variable. This example assumes you have an operation variable called username and and operation input variable called request (of type javax.servlet.http.HttpServletRequest).

Example 2.25. Invoke Java (inline) - accessing Request Parameters

username = request.getParameter("name")      

RELATED RECIPES

  1. Creating a Controller for Spring MVC