11

How to use servlets together with Struts2?

exebook
  • 32,014
  • 33
  • 141
  • 226
Lohit
  • 891
  • 5
  • 14
  • 26
  • 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 Answers4

21

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

Community
  • 1
  • 1
Steven Benitez
  • 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
4

There are three ways to resolve this problem:

  1. add constant tag in struts.xml

    <constant name="struts.action.excludePattern" value="/YourServlet,/YourOtherServlet"/>

  2. 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.

  3. implement a user-defined filter

rgc
  • 79
  • 2
1

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>
Dewfy
  • 23,277
  • 13
  • 73
  • 121
0

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");
            }
        });
Vu Truong
  • 1,655
  • 1
  • 12
  • 10