Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problems when forwarding to a jsp page from a servlet

Status
Not open for further replies.

greyknight

Programmer
Oct 28, 2003
2
IT
Hi
I hope someone can help me
I've wrote a Servlet that forwards to a jsp page in this way:

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
//Make something
getServletContext().getRequestDispatcher("/Page.jsp").forward(request, response);
}

inside the Page .jsp there is an IMG tag:
<IMG srg=&quot;image.gif&quot;>
if I load directly the Page.jsp with my browser I can see the image but if I invoke the servlet I cannot see the image.
I have checked the HTML source downloaded by the browser and I've seen that it is correct: there is the IMG tag.
Why this occur? Why the browser downloads correctly the html code but doesn't display it?
 
Let say the context path to your servlet is &quot;/servlet/yourServlet&quot;. When the servlet forward the request to the JSP page, &quot;/Page.jsp&quot;, the browser still think the page is return from URL /servlet/yourServlet. The forward is happened at server side, the broswer has no knowledge of the page is serving from URL /Page.jsp instead of URL &quot;/servlet/yourServlet&quot;. If the image src is defined relative to /Page.jsp, e.g.

<img src=&quot;myImage.gif&quot;/>

The browser will try to load the image from &quot;/servlet/myImage.gif instead of /myImage.gif&quot;

To fix this you have two options:

1. change the image src relative to &quot;/servlet/yourServlet&quot; i.e.

<img src=&quot;../myImage.gif&quot;/>

2. Or add a <base> tag in the HTML source code to tell the browser the base href should be &quot;/&quot; instead. i.e. Add this the very begining of the HTML code:

<base href=&quot; />
 
I've tried both the solutions and they work properly. Thank you
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top