Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

img src="..." has problem when using "forward".

 
Frank Sikuluzu
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a servlet "TestServlet" doing the following:
RequestDispatcher rd;
ServletContext sc = this.getServletContext();
rd = sc.getRequestDispatcher("/test.jsp");
rd.forward(req, res);
in test.jsp it has the following line
<a href="/myapp/servlet/TestServlet?param=a" ><img src="images/pic.gif" alt="" ></a>
The servlet is put in "myapp/WEB-INF/classes/" folder and the jsp is in
"myapp" folder. Then I open a browser and go to "http://localhost ort/myapp/servlet/TestServlet?param=..", what I find is the picture doesn't show up. However, when I open "http://localhost ort/myapp/test.jsp" the picture shows there. So somehow the problem is from "forward". But I don't see anything wrong here. Please help !
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65111
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The src url for your image tag needs to be server-relative. For example, let's say your web app is loaded at context 'xyz':
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65111
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It doesn't really have anything to do with the forward, but with the URI that the browser is trying to use as the base reference.
Another option is to use the <base> tag to establish an artificial relative base.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic