transferred from: http://www.cnblogs.com/nayitian/archive/2013/03/04/2942537.html
Problem
The project requires STRUTS2 and servlets to coexist, that is, struts requests are sent to struts, and the servlet requests are sent to the servlet for processing. Currently the Web. xml file should be similar to the following configuration:
Experiments have been made to modify the constant variable so that the action is processed only for the specified suffix of the interception, so that the servlet can be used normally and the pro-test is valid. Try other times when needed.
<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 app is requested, STRUTS2 will intercept all requests and will not respond properly to the servlet request because STRUTS2 treats the servlet as action because the servlet and the action have no suffixes.
Solution (Four ways) 1. Modify the relevant configuration of the servlet and unify the ". servlet" Behind the servlet
1). Modify the Web. XML configuration file as follows:
<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 called, as follows:
<% String Path = Request.getcontextpath (); %> ... $.ajax ({ URL: ' <%=path%>/servlet/jqueryajax.servlet ', ...
This will allow the servlet's request to be handled normally. This situation is appropriate for small-scale use of servlets.
2. Modify the Interception page configuration to configure the relevant interception of struts
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</url-pattern> </filter-mapping> < filter-mapping> <filter-name>struts2</filter-name> <url-pattern>*.jsp</ url-pattern> </filter-mapping> <filter-mapping> <filter-name>struts2</ filter-name> <url-pattern>/user/*</url-pattern> </filter-mapping>
This also allows the servlet's request to be handled normally. This situation may require you to modify this configuration file again as the project expands.
3. Modify the suffix mappings in the Struts.xml file
<constant name= "Struts.action.extension" value= "action" ></constant>
4. Custom Filter Implementation filtering
1). Create the implementation class with the following code reference:
Package Com.clzhang.sample.struts2;import Java.io.ioexception;import Java.util.arraylist;import java.util.Arrays; Import Javax.servlet.filter;import Javax.servlet.filterchain;import Javax.servlet.filterconfig;import Javax.servlet.requestdispatcher;import Javax.servlet.servletexception;import Javax.servlet.ServletRequest;import Javax.servlet.servletresponse;import javax.servlet.http.httpservletrequest;/** * This is the filter for a filtered servlet is to intercept the servlet's processing request and turn itself to processing instead of struts. * There are two kinds of filtering methods, one is to determine whether the URI contains "/servlet/"; * Another is that all servlets are specified in the initialization parameters. * needs to be configured in Web. xml: <filter> <filter-name>redisp</filter-name> <filter-class>com.clz Hang.sample.struts2.filterservlet</filter-class> <!--If you filter using the second method, you need the following code <init-param> <param-name>includeServlets</param-name> <param-value>jqueryajax,jsonview</param-valu e> </init-param> </filter> <filter-mapping> ≪filter-name>redisp</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> And this filter must be above the STRUTS2 filter. * @author Administrator * */public class Filterservlet implements Filter {public void Destroy () {} public V OID DoFilter (servletrequest req, Servletresponse resp, Filterchain chain) throws IOException, Servletexception {HttpServletRequest request = (httpservletrequest) req; The first way (option one) String target = Request.getrequesturi (); target = Target.lastindexof ("?") > 0? Target.substring (Target.lastindexof ("/") + 1, target.lastindexof ("?")-Target.lastindexof ( "/")): Target substring (target.lastindexof ("/") + 1); The IF (Request.getrequesturi (). IndexOf ("/servlet/") > 0) {//Request.getrequesturi () format should look like this:/st/servlet/jquerya Jax,//Where St is the project name, the servlet is the prefix added by all servlets to be able to determine the servlet. If only the request URI is judgedContains the/servlet/, if included, the processing; RequestDispatcher RDSP = Request.getrequestdispatcher (target); Rdsp.forward (req, resp); } else {Chain.dofilter (req, resp); }//The second way (optional)/** if (This.includes.contains (target)) {//target takes out the value is directly Jqueryajax, in W Configuration in Eb.xml. If the last part of the request URI is included in the configuration file, if it is included, the RequestDispatcher RDSP = Request.getrequestdispatcher (target) is processed; Rdsp.forward (req, resp); } else {Chain.dofilter (req, resp); } */} private arraylist<string> includes = new arraylist<string> (); public void init (Filterconfig config) throws servletexception {//If you filter using the second method, you need the following code//This.includes.addAl L (Arrays.aslist (Config.getinitparameter (//"Includeservlets"). Split (",")); }}
2). Modify Web. XML, similar to the following format:
<?xml version= "1.0" encoding= "UTF-8"? ><web-app version= "2.5" xmlns= "Http://java.sun.com/xml/ns/javaee" Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http ://java.sun.com/xml/ns/javaee/web-app_2_5.xsd "> <filter> <filter-name>redisp</filter-name> <filter-class>com.clzhang.sample.struts2.FilterServlet</filter-class> <!--How to filter using the second method, you need the following generation Codes <init-param> <param-name>includeServlets</param-name> <param-value> jqueryajax,jsonview</param-value> </init-param> </filter> <filter-mapp ing> <filter-name>redisp</filter-name> <url-pattern>/*</url-pattern> </filt er-mapping> <filter> <filter-name>struts2</filter-name> <filter-class>org.apac He.struts2.dispatcher.ng.filter.Strutsprepareandexecutefilter</filter-class> </filter> <filter-mapping> <filter-name>st ruts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <SERVL Et> <servlet-name>jqueryAjaxServlet</servlet-name> <servlet-class>com.clzhang.sampl E.struts2.servlet.jqueryajaxservlet</servlet-class> </servlet> <servlet-mapping> < Servlet-name>jqueryajaxservlet</servlet-name> <url-pattern>/servlet/jqueryajax</url-pattern& Gt </servlet-mapping> <servlet> <servlet-name>jsonViewServlet</servlet-name> < ;servlet-class>com.clzhang.sample.struts2.servlet.jsonviewservlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>jsonViewServlet</servlet-name> <url-pattern>/ser vlet/jsonview</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</wel Come-file> <welcome-file>index.html</welcome-file> </welcome-file-list></web-app>
Note that the,<filter-name>redisp</filter-name> must be placed before <filter-name>struts2</filter-name> This ensures that they can be processed in a normal order, or it will go wrong.
3). In the case where the servlet needs to be referenced, a normal reference can be invoked, as in a JSP page:
<% String Path = Request.getcontextpath (); %> ... $.ajax ({ URL: ' <%=path%>/servlet/jqueryajax ', ...
Action and servlet Coexistence (GO)