如果我们在web.xml中配置的struts2的接收请求的路径为:
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
是可以用struts2中使用servlet的,只是必须让struts2的action来转发到servlet的url-pattern中的地址就可以了。我们简单的举例如下:
例如定义了一个servlet:
<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>
其中Snippet是显示图片的servlet,代码如下:
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Snippet extends HttpServlet{
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
this.doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html; charset=UTF-8");
resp.setContentType("image/jpeg"); //设置图片格式格式,这里可以忽略
FileInputStream fis = new FileInputStream("E:/tomcat/temp/1350558335484.jpg");
OutputStream os = resp.getOutputStream();
try {
int count = 0;
byte[] buffer = new byte[1024*1024];
while ( (count = fis.read(buffer)) != -1 )
os.write(buffer, 0, count);
} catch (IOException e){
e.printStackTrace();
}finally {
if(os!=null)
os.close();
if(fis != null)
fis.close();
}
}
}
如果你在页面中使用该servlet,则要用action转发到<url-pattern>/Snippet</url-pattern>的路径/Snippet.
如你的页面里有个<img>元素来显示图片,使用<img src="Snippet" width="60" height="20"/>是肯定不行的,你必须定义一个action 如:
package com.social.action;
import com.opensymphony.xwork2.Action;
public class GetImg implements Action{
public String execute(){
return SUCCESS;
}
}
然后在该struts2中配置该Action
<action name="image" class="com.social.action.GetImg">
<result>/Snippet</result>
</action>
最终那个img中这么使用<img src="data:image" width="60" height="20"/>这样就可以用servlet了。
在同一个项目中,如果既用到servlet又用了struts框架,运行项目时可能无法正常使用
servlet,解决办法如下:
该类是定义了一个过滤器,当既用到struts2有用到servlet时,
必须用该过滤器对所有的servlet做一次过滤,否则servlet将无法正常运行。 同时在web.xml
中,在定义struts的核心控制器前面添加该过滤器,添加代码如下:
<filter>
<filter-name>redisp</filter-name>
<filter-class>filters.ReDispatcherFilter(该过滤器的类名)</filter-class>
<init-param>
<param-name>includeServlets</param-name>
<param-value>所有配置的servlet名字</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(",")));
}
}
最终web.xml中的配置如下:
<?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>
<!-- 指定过滤器对应的实际物理类,该类在struts2的核心代码库jar包中可以找到 -->
<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>