-1

what i am trying to do is fetching the session attribute from servlet, This attribute contains the path of my desired image which i want to display on jsp page. if i put this attribute like

<%=session.getAttribute("imageurl")%>

it give me perfect path. but when i do

<%String img=(String)session.getAttribute("imageurl");%>
<img src="img" alt=""  width="100" height="100"/>

it shows nothing... please help regarding this scenario

this is my code from servlet

 String name=rs.getString("name");
    String image=rs.getString("ppicture");
    String loc = "G:/Friendbook/pictures/";
    String imageurl=loc+image;imageurl=imageurl.trim();
    HttpSession session=req.getSession(); 
    session.setAttribute("name",name);
    session.setAttribute("imageurl",imageurl);
    req.setAttribute("imageurl", imageurl);
    req.getRequestDispatcher("home.jsp").forward(req, res);
  • Hint: what does the source of the generated page look like? – Matt Jacob Sep 12 '15 at 05:41
  • 1
    @MattJacob sir, its just simple jsp page where i need to display the image as a profile picture.. i store the image on particular location and stored its name in database. then concat both and add it in session.. that session object is "imgurl" in my code. – Anand Deshmukh Sep 12 '15 at 05:53
  • I'm telling you to look at the source of the generated page. It's going to show `src="img"` because you haven't interpolated the variable into the template. – Matt Jacob Sep 12 '15 at 06:06

2 Answers2

1

In your case src="img" will take soruce as "img".You can use Expression Language in jsp page as follow

<img src=${sessionScope.imageurl} alt=""  width="100" height="100"/>
Zaheer Khorajiya
  • 466
  • 3
  • 11
  • it wont work and i already interpolated the variable into the template – Anand Deshmukh Sep 12 '15 at 06:27
  • 1
    @AnandDeshmukh no, you didn't. The additional problem is that you're trying to use a file path instead of an http URL. You can't do that. Look at the generated HTML. It will contain something like ``. Assuming this was accepted by the browser (and it isn't): how can you know that the visitor of your web app is using windows, and has a G: drive containing these directories and files? Read http://stackoverflow.com/questions/2340406/how-to-retrieve-and-display-images-from-a-database-in-a-jsp-page – JB Nizet Sep 12 '15 at 07:02
  • Your image path should contain http URL , otherwise it wont work. – Zaheer Khorajiya Sep 12 '15 at 07:27
  • 1
    @JBNizet Thanks a lot for your valuable guideline. – Anand Deshmukh Sep 12 '15 at 08:05
  • i change my uploadServlet and set the location into my workspace inside WebContent and now both upload and download working properly... – Anand Deshmukh Sep 12 '15 at 08:07
1

Error in img tag:

Do something like this, may it help:

<img src=<%= img %> alt=""  width="100" height="100"/>
Sikandar Sahab
  • 638
  • 3
  • 10
  • 27