Struts2 How to use the servlet at the same time, is how to achieve struts and servlet coexistence

Source: Internet
Author: User
problem

The project requires that the STRUTS2 and servlet coexist, that struts requests are sent to struts, and that the servlet request is sent to the servlet for processing. The current Web.xml file should be a configuration similar to the following:

<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>
 </filter-mapping>

When the application is requested, STRUTS2 will intercept all requests and will not be able to respond properly to the servlet request, because struts2 the servlet as an action because the servlet and action have no suffixes. solution (four ways) 1. Modify the related configuration of the servlet and unify the ". servlet" Behind the servlet

1). Modify the Web.xml profile like this:

    <servlet>  
       <servlet-name>jqueryAjaxServlet</servlet-name>  
       <servlet-class> com.clzhang.sample.struts2.servlet.jqueryajaxservlet</servlet-class>  
    </servlet>  

    < servlet-mapping>  
        <servlet-name>jqueryAjaxServlet</servlet-name>  
        <url-pattern>/ Servlet/jqueryajax.servlet</url-pattern>  
    </servlet-mapping>

2. Modify the place where the servlet is invoked, in the form of:

    <%
    String Path = Request.getcontextpath ();
    %> ...
    
    $.ajax ({   
    URL: ' <%=path%>/servlet/jqueryajax.servlet ',   
    ...)

This allows the servlet's request to be handled properly. This situation is appropriate for small-scale use of the servlet. 2. Modify the blocking page configuration, that is, the relevant interception of struts configuration

Modify the Web.xml file as follows:

    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>*.action</ url-pattern>
    </filter-mapping>
    <filter-mapping>
        <filter-name>struts2</ Filter-name>
        <url-pattern>*.do</

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.