11

I want to forward a request from Servlet to Action like this using RequestDispacher like this

RequestDispatcher dispatcher=request.getRequestDispatcher("hello.action");
dispatcher.include(request, response);

It's not working. How can I resolve this problem?

Roman C
  • 49,761
  • 33
  • 66
  • 176
Sunil
  • 163
  • 2
  • 2
  • 7

2 Answers2

11

In order to do this you may also need to set the filter to run on FORWARD (and INCLUDE as your code shows, although you state you want a FORWARD):

<filter-mapping>
  <filter-name>struts2</filter-name>
  <url-pattern>/*</url-pattern>
  <dispatcher>REQUEST</dispatcher> 
  <dispatcher>FORWARD</dispatcher>
  <dispatcher>INCLUDE</dispatcher> <!-- If you want includes as well -->
</filter-mapping>
Dave Newton
  • 158,873
  • 26
  • 254
  • 302
10

Use the code in the servlet

getServletContext().getRequestDispatcher("/hello.action").forward(request, response);

You have also configure struts2 filter to accept forward requests

<filter>
  <filter-name>struts2</filter-name>
  <filter-class>
    org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
  </filter-class>
</filter>
<filter-mapping>
  <filter-name>struts2</filter-name>
  <url-pattern>/*</url-pattern>
  <dispatcher>REQUEST</dispatcher>
  <dispatcher>FORWARD</dispatcher>
</filter-mapping>
Roman C
  • 49,761
  • 33
  • 66
  • 176