Note that there are some explanatory texts on larger screens.

plurals
  1. POImage not found for displaying in servlet using img tag
    primarykey
    data
    text
    <p>I have a webpage (htm) that has a textbox (going to change it to a drop-down selection).</p> <p>I write there a name of picture (.png, .jpg, .gif) and hit a submit button</p> <p>A servlet is invoked and i am trying to display an image having that name which the user had typed in webpage.</p> <p>The images are present in the same directory as the .class files of servlet are present, but when I try to display it using the <code>&lt;img&gt;</code> tag (by writing it to PrintWriter's stream) from servlet, I get resource not available.</p> <p>I manually also tried to locate the few images using <code>localhost:8080/webAppName/imgName</code> but still resource isn't found.</p> <p>I get false also when i try to use <code>exists()</code> method of <code>File</code> class.</p> <p>The servlet works fine, I have displayed a some text as a response to client but its unable to locate the image.</p> <p>Servlet is invoked as HTTP GET.</p> <p>Here is the servlet code:</p> <pre><code>import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class Expt16_3PassImg2Servlet extends HttpServlet { String IPMsg; String imgName=""; public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException { response.setContentType("text/html"); PrintWriter pw = response.getWriter(); imgName=request.getParameter("imgNameEntry"); boolean imgExists=true; pw.println("&lt;html&gt;"); pw.println("&lt;body&gt;"); //if((new File("./"+imgName)).exists()) pw.println("&lt;img src=\""+imgName+"\"&gt;"); //else //pw.println("&lt;h1&gt;Image doesnt exist&lt;/h1&gt;"); pw.println("&lt;/body&gt;"); pw.println("&lt;/html&gt;"); pw.println("&lt;br&gt;Sample Text"); } } </code></pre> <p>I know we can display a image by setting content type as image/jpeg but i want to display it using the img tag.</p> <p>And by the way as you would have seen, I am extending my servlet to HttpServlet.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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