London guy
London guy

Reputation: 28012

Image not displayed when passed into a JSP from Servlet

This is my jsp code:

<%@ page language="java" %>
<% String name = (String)request.getAttribute("name1"); %>
<html>
<head>
</head>
<body>
<%=name%>
<img src=<%=name%>></img>

</body>
</html>

When I come to the jsp page by writing the following code in my servlet:

request.getRequestDispatcher("test.jsp").forward(request, response); 

I see that the path of the image is correctly displayed for

<%=name>

in the body portion, but the following < img > doesn't get executed and the image is not shown even though it exists in the displayed path.

Upvotes: 0

Views: 1387

Answers (4)

Phalgun D
Phalgun D

Reputation: 21

put the scriptlet element in double quote("").

<img src="<%=name%>"></img>

Upvotes: 0

Dima
Dima

Reputation: 1794

<img src=<%=name%>> missing the quotes <img src="<%=name%>">

Upvotes: 1

Hemang Rami
Hemang Rami

Reputation: 338

I think there might be some path issue. Try this.

<img src="<%=request.getContextPath()%>/images/<%=name%>"/>

Upvotes: 0

Moritz Petersen
Moritz Petersen

Reputation: 13057

I think it's a problem with the < and > inside of a HTML tag. Did you try JSP El?

<img src="${name}"/>

Upvotes: 2

Related Questions