Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy image is not showed on JSP page
    text
    copied!<p>I'm a bit lost with following scenario: </p> <ol> <li>user upload image - upload.jsp (multipart/form-data)</li> <li>servlet do all dirty job (save image, get name, save name, redirect to display.jsp)</li> <li>on the display.jsp, just uploaded image should be presented</li> </ol> <p>Unfortunately the display.jsp page is empty. When I look at source page under firefox, everything seems to be fine, valid link to the image is provided. </p> <pre><code>&lt;img src="/UploadTest/avatar/55_445194458350473498.png" border=0 width="48px" height="48px"/&gt; </code></pre> <p>But under Media info I can see some weird infos: </p> <pre><code>Location: http://localhost:8084/UploadTest/avatar/55_445194458350473498.png Type: text/html Size: Unknown (not cached) Dimensions: 0px x 0px (scaled to 0px x 16px) </code></pre> <p>Here is code used for uploading, processing and displaying picture: </p> <p><strong>upload.jsp</strong></p> <pre><code>&lt;form action="Upload" method="post" enctype="multipart/form-data"&gt; &lt;label for="file"&gt;File:&lt;/label&gt; &lt;input type="file" id="file" name="file"&gt; &lt;input type="submit" value="submit"&gt; &lt;/form&gt; </code></pre> <p><strong>Upload.java</strong></p> <p>(The MultipartMap servlet belongs to BalusC, <a href="http://balusc.blogspot.com.au/2009/12/uploading-files-in-servlet-30.html" rel="nofollow">http://balusc.blogspot.com.au/2009/12/uploading-files-in-servlet-30.html</a>)</p> <pre><code>package test; import java.io.File; import java.io.IOException; import java.util.Arrays; import javax.servlet.ServletException; import javax.servlet.annotation.MultipartConfig; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpSession; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import test.MultipartMap; @WebServlet(urlPatterns = { "/Users/Thomas/NetBeansProjects/UploadTest/web/Upload" }) @MultipartConfig(location = "/Users/Thomas/NetBeansProjects/UploadTest/web/avatar", maxFileSize = 10485760L) // 10MB. public class UploadServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { MultipartMap map = new MultipartMap(request, this); File file = map.getFile("file"); String filename = file.getName(); HttpSession session = request.getSession(); session.setAttribute("filename", filename); request.getRequestDispatcher("/display.jsp").forward(request, response); } } </code></pre> <p><strong>display.jsp</strong> </p> <pre><code>&lt;%@page contentType="text/html" pageEncoding="UTF-8"%&gt; &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt; &lt;title&gt;JSP Page&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;div&gt; &lt;img src="${pageContext.request.contextPath}/avatar/${filename}" border=0 width="48px" height="48px"/&gt; &lt;div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>If I replace, in display.jsp, ${filename} with static name of the particular image uploaded before, there is no problem with displaying so I suppose the images are processed correctly just something is missing on forward side? </p> <p>By the way: when debugger is active everything is OK but when switch off the problem is back. </p> <p>Cheers,</p> <p>Thomas</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload