Package org.springframework.web.servlet.mvc

# Class ServletForwardingController

java.lang.Object
org.springframework.context.support.ApplicationObjectSupport
org.springframework.web.context.support.WebApplicationObjectSupport
org.springframework.web.servlet.support.WebContentGenerator
org.springframework.web.servlet.mvc.AbstractController
org.springframework.web.servlet.mvc.ServletForwardingController

All Implemented Interfaces:
  `org.springframework.beans.factory.Aware`, `org.springframework.beans.factory.BeanNameAware`, `org.springframework.context.ApplicationContextAware`, `org.springframework.web.context.ServletContextAware`, `Controller`

public class ServletForwardingController extends AbstractController implements org.springframework.beans.factory.BeanNameAware
Spring Controller implementation that forwards to a named servlet, i.e. the "servlet-name" in web.xml rather than a URL path mapping. A target servlet doesn't even need a "servlet-mapping" in web.xml in the first place: A "servlet" declaration is sufficient.

Useful to invoke an existing servlet via Spring's dispatching infrastructure, for example to apply Spring HandlerInterceptors to its requests. This will work even in a minimal Servlet container that does not support Servlet filters.

Example: web.xml, mapping all "/myservlet" requests to a Spring dispatcher. Also defines a custom "myServlet", but without servlet mapping.

     <servlet>
       <servlet-name>myServlet</servlet-name>
       <servlet-class>mypackage.TestServlet</servlet-class>
     </servlet>

     <servlet>
       <servlet-name>myDispatcher</servlet-name>
       <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
     </servlet>

     <servlet-mapping>
       <servlet-name>myDispatcher</servlet-name>
       <url-pattern>/myservlet</url-pattern>
     </servlet-mapping>

Example: myDispatcher-servlet.xml, in turn forwarding "/myservlet" to your servlet (identified by servlet name). All such requests will go through the configured HandlerInterceptor chain (for example, an OpenSessionInViewInterceptor). From the servlet point of view, everything will work as usual.

     <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
       <property name="interceptors">
         <list>
           <ref bean="openSessionInViewInterceptor"/>
         </list>
       </property>
       <property name="mappings">
         <props>
           <prop key="/myservlet">myServletForwardingController</prop>
         </props>
       </property>
     </bean>

     <bean id="myServletForwardingController" class="org.springframework.web.servlet.mvc.ServletForwardingController">
       <property name="servletName"><value>myServlet</value></property>
     </bean>

Since:
  1.1.1
Author:
  Juergen Hoeller
See Also:
  - `ServletWrappingController`
  - `org.springframework.orm.jpa.support.OpenEntityManagerInViewInterceptor`
  - `org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter`

## Field Summary

### Fields inherited from class org.springframework.web.servlet.support.WebContentGenerator

`HEADER_CACHE_CONTROL, METHOD_GET, METHOD_HEAD, METHOD_POST`

### Fields inherited from class org.springframework.context.support.ApplicationObjectSupport

`logger`

## Constructor Summary

Constructors
Constructor
Description
`ServletForwardingController()`

## Method Summary

Modifier and Type
Method
Description
`protected ModelAndView`
`handleRequestInternal(HttpServletRequest request, HttpServletResponse response)`
Template method.
`void`
`setBeanName(String name)`
`void`
`setServletName(String servletName)`
Set the name of the servlet to forward to, i.e.
`protected boolean`
`useInclude(HttpServletRequest request, HttpServletResponse response)`
Determine whether to use RequestDispatcher's `include` or `forward` method.

### Methods inherited from class org.springframework.web.servlet.mvc.AbstractController

`handleRequest, isSynchronizeOnSession, setSynchronizeOnSession`

### Methods inherited from class org.springframework.web.servlet.support.WebContentGenerator

`applyCacheControl, applyCacheSeconds, checkAndPrepare, checkRequest, getAllowHeader, getCacheControl, getCacheSeconds, getSupportedMethods, getVaryByRequestHeaders, isRequireSession, prepareResponse, setCacheControl, setCacheSeconds, setRequireSession, setSupportedMethods, setVaryByRequestHeaders`

### Methods inherited from class org.springframework.web.context.support.WebApplicationObjectSupport

`getServletContext, getTempDir, getWebApplicationContext, initApplicationContext, initServletContext, isContextRequired, setServletContext`

### Methods inherited from class org.springframework.context.support.ApplicationObjectSupport

`getApplicationContext, getMessageSourceAccessor, initApplicationContext, obtainApplicationContext, requiredContextClass, setApplicationContext`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Constructor Details

### ServletForwardingController

public ServletForwardingController()

## Method Details

### setServletName

public void setServletName(String servletName)
Set the name of the servlet to forward to, i.e. the "servlet-name" of the target servlet in web.xml.

Default is the bean name of this controller.

### setBeanName

public void setBeanName(String name)

Specified by:
  `setBeanName` in interface `org.springframework.beans.factory.BeanNameAware`

### handleRequestInternal

@Nullable protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception
Description copied from class: `AbstractController`
Template method. Subclasses must implement this. The contract is the same as for `handleRequest`.

Specified by:
  `handleRequestInternal` in class `AbstractController`
Throws:
  `Exception`
See Also:
  - `AbstractController.handleRequest(jakarta.servlet.http.HttpServletRequest, jakarta.servlet.http.HttpServletResponse)`

### useInclude

protected boolean useInclude(HttpServletRequest request, HttpServletResponse response)
Determine whether to use RequestDispatcher's `include` or `forward` method.

Performs a check whether an include URI attribute is found in the request, indicating an include request, and whether the response has already been committed. In both cases, an include will be performed, as a forward is not possible anymore.

Parameters:
  `request` - current HTTP request
  `response` - current HTTP response
Returns:
  `true` for include, `false` for forward
See Also:
  - `RequestDispatcher.forward(jakarta.servlet.ServletRequest, jakarta.servlet.ServletResponse)`
  - `RequestDispatcher.include(jakarta.servlet.ServletRequest, jakarta.servlet.ServletResponse)`
  - `ServletResponse.isCommitted()`
  - `WebUtils.isIncludeRequest(jakarta.servlet.ServletRequest)`