How to use servlets
together with Struts2
?
-
Why don't you buy a book like 'Struts2 in Action'. Check out http://www.vaannila.com/struts-2/struts-2-tutorial/struts-2-tutorial.html. When you have specific problem ask a question here. – Chakra Mar 14 '11 at 06:38
-
Both the question and answer seemed clear to me. – Quaternion Mar 14 '11 at 22:22
4 Answers
I assume you want to know how to use a servlet in conjunction with Struts2 when you have mapped everything to the Struts2 filter.
You can use the following in your struts.xml
:
<constant name="struts.action.excludePattern" value="/YourServlet"/>
You can exclude multiple patterns by separating them with a comma, such as:
<constant name="struts.action.excludePattern" value="/YourServlet,/YourOtherServlet"/>
More Information

- 1
- 1

- 10,936
- 3
- 39
- 50
-
here is the offical link: https://struts.apache.org/docs/static-content.html read: "Preventing Struts from Handling a Request" section – thekosmix Jan 29 '16 at 08:57
There are three ways to resolve this problem:
add constant tag in struts.xml
<constant name="struts.action.excludePattern" value="/YourServlet,/YourOtherServlet"/>
add suffix in servlet configuration in web.xml
<servlet-mapping>
<servlet-name>Authcode</servlet-name>
<url-pattern>/authcode.servlet</url-pattern>
</servlet-mapping>
Because in struts 2, it will only intercept all the request end with .action, if this request do not have any suffix, it will automatically add it. When we make our servlet url-pattern have a suffix, then struts 2 will not intercept it anymore.
implement a user-defined filter

- 79
- 2
-
1Not true - *Because in struts 2, it will only intercept all the request end with .action*. – Aleksandr M Apr 27 '15 at 07:45
-
which one is not true? no.2? servlet can work together with struts2, you can try it. – rgc May 12 '15 at 08:53
-
1This part is wrong - *Because in struts 2, it will only intercept all the request end with .action.* – Aleksandr M May 12 '15 at 09:07
-
Servlets technology is more low level architectural layer than Struts2. Even more Struts2 is embedded to your project as a filter (that is part of servlet technology).
So to add one more servlet just add to web.xml registration:
<servlet>
<servlet-name>MyServlet</servlet-name>
<servlet-class>class.MyServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

- 23,277
- 13
- 73
- 121
If you need multi mapping servlet you can using:
<constant name="struts.action.excludePattern" value="/Servletname1, /Servletname2" />
But in struts, you should not using servlet url because it not unity. You can use ajax:
$.ajax({
url : "nameAction.action?param="+id,
type : "post",
data : {
'id' : id
},
success : function(data) {
// $('#result').html(data);
},
error : function(jqXHR, textStatus, errorThrown) {
$('#result').html("Error");
}
});

- 1,655
- 1
- 12
- 10