本文为转载--使Struts2与Servlet并存解决办法
本文转载自:http://www.cnblogs.com/jifeng/archive/2010/08/28/1810706.html
在此感谢作者的友情分享,首次备用。
解决办法:在Struts2过滤器前增加一个过滤器,使用过滤器转发到servlet上 web.xml配置:view sourceprint?01 <filter> 02 <filter-name>JSONRPCServlet</filter-name> 03 <filter-class>filters.ReDispatcherFilter</filter-class> 04 <init-param> 05 <param-name>includeServlets</param-name> 06 <param-value>JRPC</param-value> 07 <!--注:JRPC为下文配置的Servlet JSONRPCServlet过滤的请求配置 --> 08 </init-param> 09 </filter> 10 <filter-mapping> 11 <filter-name>JSONRPCServlet</filter-name> 12 <url-pattern>/*</url-pattern> 13 </filter-mapping> 14 15 16 <filter> 17 <filter-name>struts2</filter-name> 18 <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> 19 </filter> 20 <filter-mapping> 21 <filter-name>struts2</filter-name> 22 <url-pattern>*.action</url-pattern> 23 </filter-mapping> 24 25 <filter-mapping> 26 <filter-name>struts2</filter-name> 27 <url-pattern>/*</url-pattern> 28 </filter-mapping> 29 30 <!--注:这里同样需要Servlet配置,Filter会转到该Servlet上--> 31 <servlet> 32 <servlet-name>JSONRPCServlet</servlet-name> 33 <servlet-class> 34 jcore.jsonrpc.servlet.JSONRPCServlet 35 </servlet-class> 36 <load-on-startup>2</load-on-startup> 37 </servlet> 38 <servlet-mapping> 39 <servlet-name>JSONRPCServlet</servlet-name> 40 <url-pattern>/JRPC</url-pattern> 41 </servlet-mapping> 过滤器的实现类: view sourceprint?01 package filters; 02 03 import java.io.IOException; 04 import java.util.ArrayList; 05 import java.util.Arrays; 06 07 import javax.servlet.Filter; 08 import javax.servlet.FilterChain; 09 import javax.servlet.FilterConfig; 10 import javax.servlet.RequestDispatcher; 11 import javax.servlet.ServletException; 12 import javax.servlet.ServletRequest; 13 import javax.servlet.ServletResponse; 14 import javax.servlet.http.HttpServletRequest; 15 16 public class ReDispatcherFilter implements Filter { 17 18 public void destroy() { 19 // TODO Auto-generated method stub 20 21 } 22 23 public void doFilter(ServletRequest req, ServletResponse resp, 24 FilterChain chain) throws IOException, ServletException { 25 26 HttpServletRequest request = (HttpServletRequest) req; 27 String target = request.getRequestURI(); 28 target = target.lastIndexOf("?") > 0 ? target.substring(target 29 .lastIndexOf("/") + 1, target.lastIndexOf("?") 30 - target.lastIndexOf("/")) : target.substring(target 31 .lastIndexOf("/") + 1); 32 33 //System.out.println(target); 34 if (this.includes.contains(target)) { 35 RequestDispatcher rdsp = request.getRequestDispatcher(target); 36 37 //System.out.println("go..............." + rdsp); 38 rdsp.forward(req, resp); 39 } else 40 chain.doFilter(req, resp); 41 } 42 43 private ArrayList<String> includes = new ArrayList<String>(); 44 45 public void init(FilterConfig config) throws ServletException { 46 47 this.includes.addAll(Arrays.asList(config.getInitParameter( 48 "includeServlets").split(","))); 49 50 } 51