I have problem with dispaying image, that loaded via serlvet from database. I use this class
public class ImageServlet extends HttpServlet {
private static final int DEFAULT_BUFFER_SIZE = 10240;
@EJB
private GoodsDAO goodsDAO;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String stringImageId = request.getParameter("id");
if (stringImageId == null) {
response.sendError(HttpServletResponse.SC_NOT_FOUND); // 404.
return;
}
int imageId = Integer.parseInt(stringImageId);
Goods goods = goodsDAO.find(imageId);
if (goods == null) {
response.sendError(HttpServletResponse.SC_NOT_FOUND); // 404.
return;
}
response.reset();
response.setBufferSize(DEFAULT_BUFFER_SIZE);
response.setContentType("image/jpeg");
response.setContentLength(goods.getImage().length);
response.setHeader("Expires", "Thu, 15 Apr 2010 20:00:00 GMT");
BufferedOutputStream output = null;
try {
output = new BufferedOutputStream(response.getOutputStream(), DEFAULT_BUFFER_SIZE);
output.write(goods.getImage());
} finally {
close(output);
}
}
private static void close(Closeable resource) {
if (resource != null) {
try {
resource.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
my faces-config
<faces-config 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-facesconfig_2_0.xsd"
version="2.0">
<navigation-rule>
<navigation-case>
<from-outcome>listAllGoods</from-outcome>
<to-view-id>/pages/protected/user/listAllGoods.xhtml</to-view-id>
</navigation-case>
</navigation-rule>
<navigation-rule>
<navigation-case>
<from-outcome>createGoods</from-outcome>
<to-view-id>/pages/protected/admin/createGoods.xhtml</to-view-id>
</navigation-case>
</navigation-rule>
<navigation-rule>
<navigation-case>
<from-outcome>createOrder</from-outcome>
<to-view-id>/pages/protected/user/createOrder.xhtml</to-view-id>
</navigation-case>
</navigation-rule>
<application>
<resource-bundle>
<base-name>messages</base-name>
<var>msgs</var>
</resource-bundle>
</application>
my web.xml
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>ClothesJSF</display-name>
<welcome-file-list>
<welcome-file>pages/protected/user/listAllGoods.xhtml</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
<url-pattern>*.jsf</url-pattern>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>imageServlet</servlet-name>
<servlet-class>com.servlet.ImageServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>imageServlet</servlet-name>
<url-pattern>/image/*</url-pattern>
</servlet-mapping>
<filter>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
<init-param>
<param-name>thresholdSize</param-name>
<param-value>51200</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<servlet-name>Faces Servlet</servlet-name>
</filter-mapping>
I load my image with this code:
<h:graphicImage id="zoomImage" value="image?id=#{goods.id}"
style="cursor:pointer" width="70" />
I have problem with displaying image on none-home pages. When I open page:
http://localhost:8080/ClothesJSF/
all my images normally were loaded from database. If i open page
http://localhost:8080/ClothesJSF/pages/protected/user/listAllGoods.xhtml
that equal to my home page my image won't be loaded. This problem also occured on all others pages.
I think that I have problem with image-servlet settings
<servlet-mapping>
<servlet-name>imageServlet</servlet-name>
<url-pattern>/image/*</url-pattern>
</servlet-mapping>
and maybe I need other regular expression in url-pattern, but I couldn't figure out it. I will be glad to any advice. Thanks you.
UPDATE If i will use path like this in all image:
<h:graphicImage id="zoomImage" value="http://localhost:8080/ClothesJSF/image?id=#
{goods.id}" style="cursor:pointer" width="70" />
It will work, but if i will use #{request.contextPath}/ this won't work. Maybe I need made some preference changes if I want #{request.contextPath}/image?id=# would work.
If the <h:graphicImage value>
does not start with a scheme or /
, then it's relative to the current request URL. Imagine that you're opening the page by http://localhost:8080/ClothesJSF/faces/page.xhtml
and the page in question has a
<h:graphicImage value="image?id=1" />
then JSF will generate a
<img src="image?id=1" />
which effectively becomes relative to http://localhost:8080/ClothesJSF/faces/
and the webbrowser will attempt to download the actual image from http://localhost:8080/ClothesJSF/faces/image?id=1
, which thus only ends up in an error. If you have paid attention to webbrowser's builtin HTTP traffic montitor, you'd have noticed that.
You need to let it start with /
to make it relative to the context path.
<h:graphicImage value="/image?id=1" />
This way JSF will generate
<img src="/ClothesJSF/image?id=1" />
Which is right. Note that the #{request.contextPath}
is not necessary. The <h:graphicImage>
already takes care of it transparently. You only need that in "plain HTML" resource elements like <a>
, <img>
, <link>
, <iframe>
, etc.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With