i have a struts2 web app and I want to use a servlet in my project,but struts2's filter does not allow to call servlets I have looked into this solution, but unfortunately it didn't work out and the problem still stands.
Any ideas?
servlet code:
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>
webpage:
<%@ 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>
you can use struts2 Stream Result for display the image
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>
if your project url like http://localhost:8080/project
<img src="http://localhost:8080/project/getImage" height="200" width="400"/>
or
<img src="getImage" height="200" width="400"/>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.