RE: Trouble with IMG tag, servlets, and JSPs
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:[EMAIL PROTECTED]] Sent: February 27, 2002 00:13 To: [EMAIL PROTECTED] 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 [EMAIL PROTECTED] Master-Mind Consulting Services http://www.mastermind.com http://www.mastermind.com/ Ph - 570-688-9600 Fx - 208-275-2301 -- To unsubscribe: mailto:[EMAIL PROTECTED] For additional commands: mailto:[EMAIL PROTECTED] Troubles with the list: mailto:[EMAIL PROTECTED]
RE: Trouble with IMG tag, servlets, and JSPs
I chose to use your first suggestion; a Servlet-Mapping, and it worked great and looks much more elegant. All I did was to include the following mapping in my web.xml: servlet-mapping servlet-nameMyServlet/servlet-name url-pattern/MyServlet/url-pattern /servlet-mapping Now, I simply call the servlet from the browser without the servlet keyword and everything works as expected. Thanks again for the suggestion. Ken Ramirez - Principal/CTO [EMAIL PROTECTED] Master-Mind Consulting Services http://www.mastermind.com Ph - 570-688-9600 Fx - 208-275-2301 -Original Message- From: Greg Trasuk [mailto:[EMAIL PROTECTED]] Sent: Wednesday, February 27, 2002 9:01 AM To: 'Tomcat Users List' Subject: RE: Trouble with IMG tag, servlets, and JSPs 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:[EMAIL PROTECTED]] Sent: February 27, 2002 00:13 To: [EMAIL PROTECTED] 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