Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7015111
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:36:35+00:00 2026-05-27T22:36:35+00:00

I have a webpage (htm) that has a textbox (going to change it to

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T22:36:36+00:00Added an answer on May 27, 2026 at 10:36 pm

    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?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have webpage where there is textbox with some default value. I have to
have a webpage that has an image in it. in the CSS, i define
I have a webpage that pulls information from a database, converts it to .csv
I have a webpage that is taking way too long and need to optimize
I have a webpage on my site that displays a table, reloads the XML
I have a webpage that implements a set of tabs each showing different content.
I have a webpage that I use h1 tags multiple times within various DIVs
I have a single webpage that is shared on 9 different domains. I want
I have a webpage that takes 10 minutes to run one query against a
I have a webpage that reads data from an external source. It can take

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.