Hello friends,
I want to display an image in my JSP using expressions (<%=abc%>)tag. But, the problem is that the JSP does not show the image.
I store the path retrieved from database (MS SQL) in a "path" variable and then try to display it in JSP using <img> tag.

The code is as follws:
<img src="<%=path%>" mce_src="<%=path%>" width="260" height="261" border="2" >
where, path="temp/" + image; //temp is the folder containing image
//image=image name corresponding to user logged in,since,image is stored with name same as user name.

Is there anything else that should be included in the code?
Is the src path right?
In my other JSPs where i directly give image names, images are displayed. Then why not here? I think the problem is in src="<%=path%>" , but can't figure it out.

Please help.
THANK YOU.

Dani AI

Generated

Common causes when a JSP-generated image URL renders as a broken image are: the browser sees a wrong URL in the generated HTML, the file is not in a web-accessible location, or the servlet context path is missing. As suggested, the first diagnostic step is to inspect the HTML the browser actually receives and the network request the browser makes for the image.

Quick diagnostic checklist:

  • View-source or use DevTools to read the generated src value and copy-paste it into the address bar to see the server response and full URL (404 vs 403 vs other status).
  • Use the Network tab to confirm the image request and HTTP status. A 404 means the path does not map to any served resource; 403 indicates a permission issue.
  • Confirm the image file actually exists under the application’s document root that the webserver can serve, or confirm a servlet is mapped to serve that URL. Files written into the exploded WAR at runtime are often lost on redeploy and might not be where the app looks.

Practical fixes and best practices:

  • Build URLs that include the application context (for example via pageContext.request.contextPath or EL) so paths are absolute to the webapp, not relative to the JSP file location.
  • If images are stored in a database, serve them through a dedicated servlet that streams bytes with the correct Content-Type header and reference that servlet in the src attribute. This avoids relying on filesystem placement inside the war.
  • Ensure forward slashes are used in URLs, verify file permissions, and avoid relying on editor-specific attributes (for example mce_src) to display images.

If the generated src looks correct but the server returns an error, server logs will usually show why the resource could not be served.

Recommended Answers

All 2 Replies

Let me clear it out.
I accept an image from users and store them in byte format in database at the same time i convert the bytes to image and store it in "temp" folder with name - "username+image type".
While retrieving, i specify the image name in "path" variable so as to retrieve it directly from folder and not database.
Yet, the image is not displayed.Please help.

what's actually generated as code and sent to the browser?
Almost certainly the paths shown there are utterly incorrect and have no relation whatsoever to the paths of the images relative to the deployroot of the web application.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.