tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Greg Trasuk" <stratus...@on.aibn.com>
Subject RE: Trouble with IMG tag, servlets, and JSPs
Date Wed, 27 Feb 2002 14:00:48 GMT
Hi Ken:

	Are you accessing your servlets through the servlet invoker (i.e.
http://host/MyApp/servlet/myserv ?)  If so, then remember that from the
browser's point of view, the 'current directory' of the page is
'http://host/MyApp/servlet'.  When the browser sees a relative path, like
'images/myimage.jsp', it tries to load that relative to the servlet's path,
so it comes up with 'http://host/MyApp/servlet/images/myimage.jsp', which
doesn't exist.  That's your 404 error in the logs.  Same thing holds for
style sheets and other resources.

Solutions:
	1- If you want to keep using the path '/servlet' (i.e. you don't want to
define servlet mappings, or you need the path prefix to make sure Apache
sends the request to Tomcat) then change your image links to
'../images/myimage.jsp'
	2- Use the getContextPath() call.
	3- Define servlet mappings in your web.xml so that you reference the
servlet with something like 'http://host/MyApp/myserv.srv', which puts the
current path at your application root.
	4- Put a hook into the top of the jsp page to forward the request to the
servlet if the request came directly from the browser.  Then the browser
will simply load the url by 'http://host/MyApp/page.jsp' and the jsp
forwards to the servlet by doing a <jsp:forward ...> directive.  This is
actually the way I do it, since it gets around some difficulties I have in
my Apache configuration which make it hard to map servlets generically
(nothing wrong with Apache or Tomcat; I just have a wierd virtual host
configuration for other reasons), but easy to get JSP's called.  I simply
have the servlet drop an attribute called 'fromServlet' into the request
object.  If the jsp doesn't see this attribute, it forwards to the servlet,
which then does its thing, drops in the 'fromServlet' attribute and forwards
back to the same jsp.  Then the jsp sees the attribute and handles the
request itself.

Good luck,

Greg Trasuk, President
StratusCom Manufacturing Systems Inc. - We use information technology to
solve business problems on your plant floor.
http://stratuscom.ca

> -----Original Message-----
> From: Ken Ramirez [mailto:kramirez@mastermind.com]
> Sent: February 27, 2002 00:13
> To: tomcat-user@jakarta.apache.org
> Subject: Trouble with IMG tag, servlets, and JSPs
>
>
> Hope someone can help:
>
> I'm having a problem loading images from a JSP page when the page is
> called from a servlet.  I'm performing a
> forward from the servlet to the JSP, which then loads the
> images from a
> subdirectory in the app's directory as follows:
>
> <TOMCAT_HOME>/webapps/MyApp/*.jsp
>
> <TOMCAT_HOME>/webapps/MyApp/images/*.img
>
> The images are loaded from the JSP using the img tag as follows:
>
> <img src="images/myimage.jpg" >
>
> I've also tried src="/images/myimage.jpg" and this doesn't work.
> However, if I call the jpg directly, everything shows up fine.
> Of course in the real world, I need to get the forward to
> work so that I
> can pass data from the servlet to the JSP.  I found
> some information in the mailing list's archive regarding this problem
> and someone suggested the following:
>
> <IMG SRC="<%=request.getContextPath()%>/images/myimage.jpg">
>
> This does work, but it just seems odd that you would have to do this,
> especially given the fact that there is a context entry for
> the app in the server.xml.  Isn't the purpose of the Context entry to:
>
> 	1.  Create and associate a Context object with the App and
> 	2.  Establish the base path for the app.
>
> Seems kind of redundent that you would have to again retrieve the
> ContextPath yourself, when it seems that Tomcat should do
> this for us or the Browser should receive the problem path so that it
> knows where to get the images and CSS files from.  Instead,
> what is actually sent back to the browser is the following
> path for the
> image:
>
> "images/myimage.jpg"
>
> Now what's interesting is that I looked in one of Tomcat's
> log files and
> found the following exception for the images:
>
> "StandardWrapper[/MasterMind:org.apache.catalina.INVOKER.theme
> ]: Marking
> servlet org.apache.catalina.INVOKER.theme as unavailable
> 2002-02-26 20:53:17 invoker: Cannot allocate servlet instance for path
> /MasterMind/servlet/theme/Master.css
> javax.servlet.ServletException: Wrapper cannot find servlet
> class theme
> or a class it depends on ..."
>
> And in one of the other log files, I found the following error:
>
> 127.0.0.1 - - [26/Feb/2002:20:53:17 -0500] "GET
> /MyApp/servlet/images/myimage.jpg HTTP/1.1" 404 696
>
>
> I'd like to get this to work without the hack I mentioned above.  Does
> anyone have any suggestions?
>
>
> Thanks,
>
> Ken
>
> Ken Ramirez - Principal/CTO
> kramirez@mastermind.com
>
> Master-Mind Consulting Services
> http://www.mastermind.com <http://www.mastermind.com/>
> Ph - 570-688-9600
> Fx - 208-275-2301
>
>
>


--
To unsubscribe:   <mailto:tomcat-user-unsubscribe@jakarta.apache.org>
For additional commands: <mailto:tomcat-user-help@jakarta.apache.org>
Troubles with the list: <mailto:tomcat-user-owner@jakarta.apache.org>


Mime
View raw message