I have a webpage (htm) that has a textbox (going to change it to a drop-down selection).
I write there a name of picture (.png, .jpg, .gif) and hit a submit button
A servlet is invoked and i am trying to display an image having that name which the user had typed in webpage.
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 <img> tag (by writing it to PrintWriter’s stream) from servlet, I get resource not available.
I manually also tried to locate the few images using localhost:8080/webAppName/imgName but still resource isn’t found.
I get false also when i try to use exists() method of File class.
The servlet works fine, I have displayed a some text as a response to client but its unable to locate the image.
Servlet is invoked as HTTP GET.
Here is the servlet 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("<html>");
pw.println("<body>");
//if((new File("./"+imgName)).exists())
pw.println("<img src=\""+imgName+"\">");
//else
//pw.println("<h1>Image doesnt exist</h1>");
pw.println("</body>");
pw.println("</html>");
pw.println("<br>Sample Text");
}
}
I know we can display a image by setting content type as image/jpeg but i want to display it using the img tag.
And by the way as you would have seen, I am extending my servlet to HttpServlet.
The image should be in the web directory–the same place you put any other publicly-accessible resource, like an HTML file (but not under
WEB-INF).The client has zero clue about classpath resources. Unless you map an image name to a classpath resources and stream back the bytes, images on the classpath are invisible to the client.
How will a user know what the name of an image is?