If the path to the receive request for the STRUTS2 that we configured in Web. XML is:
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
It is possible to use the servlet in Struts2, except that the STRUTS2 action must be forwarded to the address in the servlet's Url-pattern. Our simple example is as follows:
For example, a servlet is defined:
<servlet>
<servlet-name>Snippet</servlet-name>
<servlet-class>com.social.util.Snippet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Snippet</servlet-name>
<url-pattern>/Snippet</url-pattern>
</servlet-mapping>
If you use the servlet in a page, you will use the action to forward the path to <url-pattern>/Snippet</url-pattern>/snippet.
If you have a element on your page to display the image, use
Package com.social.action;
Import com.opensymphony.xwork2.Action;
public class Getimg implements action{
Public String execute () {
return SUCCESS;
}
}
Then configure the action in the Struts2.
<action name= "image" class= "COM.SOCIAL.ACTION.GETIMG" >
<result>/Snippet</result>
</action>
In the end, the IMG uses So that the servlet can be used.
In the same project, if you use both the servlet and the struts framework, running the project may not work
Servlet, the solution is as follows:
The class is defined as a filter that, when used with struts2 useful to the servlet,
All servlets must be filtered with this filter, otherwise the servlet will not function properly. Also in Web. xml
, add the filter in front of the core controller that defines struts, adding the following code:
<filter>
<filter-name>redisp</filter-name>
<filter-class>filters. Redispatcherfilter (class name of the filter) </filter-class>
<init-param>
<param-name>includeServlets</param-name>
<param-value> servlet names for all configurations </param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>redisp</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Package filters;
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;
public class Redispatcherfilter implements Filter {
public void Destroy () {
TODO auto-generated Method Stub
}
public void DoFilter (ServletRequest req, Servletresponse resp,
Filterchain chain) throws IOException, Servletexception
{
HttpServletRequest request = (httpservletrequest) req;
String target = Request.getrequesturi ();
target=
Target.lastindexof ("?") >0?target.substring (Target.lastindexof ("/") +1,target.lastindexof ("?") -target
. LastIndexOf ("/")): Target.substring (Target.lastindexof ("/") +1);
SYSTEM.OUT.PRINTLN (target);
if (This.includes.contains (target))
{
RequestDispatcher RDSP = Request.getrequestdispatcher (target);
System.out.println ("Go ........ +rdsp");
Rdsp.forward (req, resp);
}
Else
Chain.dofilter (req, resp);
}
Private arraylist<string> includes = new arraylist<string> (); public void init (Filterconfig config) throws servletexception {
This.includes.addAll (Arrays.aslist (Config.getinitparameter ("Includeservlets"). Split (","));
}
}
The configuration in the final Web. XML is as follows:
<?xml version= "1.0" encoding= "UTF-8"?>
<web-app version= "2.4"
Xmlns= "HTTP://JAVA.SUN.COM/XML/NS/J2EE"
Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
Xsi:schemalocation= "Http://java.sun.com/xml/ns/j2ee
Http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd ">
<servlet>
<servlet-name>Snippet</servlet-name>
<servlet-class>com.social.util.Snippet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Snippet</servlet-name>
<url-pattern>/Snippet</url-pattern>
</servlet-mapping>
<filter>
<filter-name>redisp</filter-name>
<filter-class>filters. Redispatcherfilter</filter-class>
<init-param>
<param-name>includeServlets</param-name>
<param-value>Snippet</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>redisp</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>struts2</filter-name>
<!--Specify the actual physical class for the filter, which can be found in the core Code library jar package for STRUTS2
<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>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>