Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在struts2中使用servlet_Java_Struts2_Servlet Filters - Fatal编程技术网

Java 如何在struts2中使用servlet

Java 如何在struts2中使用servlet,java,struts2,servlet-filters,Java,Struts2,Servlet Filters,我有一个struts2 web应用程序,我想在我的项目中使用servlet,但是struts2的过滤器不允许调用servlet 我已经研究了解决方案,但不幸的是,它没有成功,问题仍然存在 有什么想法吗 servlet代码: protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.p

我有一个struts2 web应用程序,我想在我的项目中使用servlet,但是struts2的过滤器不允许调用servlet 我已经研究了解决方案,但不幸的是,它没有成功,问题仍然存在

有什么想法吗

servlet代码:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.print("morteza");
            OutputStream o = response.getOutputStream();
            InputStream is = new FileInputStream(new File("d:/Desert.jpg"));
            byte[] buf = new byte[32 * 1024]; 
            int nRead = 0;
            while( (nRead=is.read(buf)) != -1 ) {
                o.write(buf, 0, nRead);
            }
            o.flush();
            o.close();
            return; 
    }
struts.xml:

<struts>

    <constant name="struts.enable.DynamicMethodInvocation" value="true" />
    <constant name="struts.devMode" value="false" />
    <constant name="struts.custom.i18n.resources" value="ApplicationResources" />
    <constant name="struts.action.excludePattern" value="Ser"/>
</struts>

web.xml:

      <servlet>
    <description></description>
    <display-name>Ser</display-name>
    <servlet-name>Ser</servlet-name>
    <servlet-class>Ser</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Ser</servlet-name>
    <url-pattern>/Ser</url-pattern>
  </servlet-mapping>

Ser
Ser
Ser
Ser
/Ser
网页:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
fap
<img src="Ser" height="200" width="400"/>


</body>
</html>

在此处插入标题
fap

您可以使用struts2 Stream Result来显示图像

 public class ImageAction extends ActionSupport{

        public InputStream getImage() throws FileNotFoundException{
            return new FileInputStream("f://test.jpg");
        }

    }


    <action name="getImage" class="com.project.ImageAction">
                <result name="success" type="stream">
                    <param name="contentType">image/jpeg</param>
                    <param name="inputName">Image</param>
                    <param name="contentDisposition">attachment;filename="image"            </param>
                    <param name="bufferSize">1024</param>
                </result>
    </action>
公共类ImageAction扩展了ActionSupport{
公共InputStream getImage()引发FileNotFoundException{
返回新的FileInputStream(“f://test.jpg”);
}
}




什么不起作用?精心设计。如果你不想让Struts过滤器拦截所有内容,为什么要将它映射到所有内容?我的项目在Struts框架中,我已经排除了这个servlet。是的,我理解这一点。当你尝试这个的时候发生了什么。有例外吗?您的servlet的映射是什么?您在struts.xml文件中键入了什么?为什么不使用Struts操作而不是这个servlet呢?Struts操作可以完成servlet所做的一切。如何通过struts2显示图像?使用流结果。看见或者执行与servlet中相同的操作:将图像数据写入响应输出流,并从操作返回ActionSupport.NONE或null,如下所述:
<img src="http://localhost:8080/project/getImage" height="200" width="400"/> 
<img src="getImage" height="200" width="400"/>