tomcat-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nath...@lexmark.com
Subject Classloder for a JSP is differnet than a servlet
Date Tue, 04 Apr 2000 15:32:58 GMT
I have a concern that the ClassLoader for a JSP page is different than for a
Servlet.  This can cause different behavior between running with a JSP and
precompiling with JSPC and running the JSP as a Servlet.  The specific problem I
ran into is not being able to load a resource bundle with
ResourceBundle.getResource() from a JSP because the JSP ClassLoader (
org.apache.jasper.runtime.JspLoader) can't find the resources in
WEB-INF\classes.  It does work if you call ResourceBundle.getResource() from a
bean or a Servlet because they use the
org.apache.tomcat.loader.AdaptiveClassLoader ClassLoader.

I couldn't find in the JSP/Servlet specs if a JSP should be able to load
resources from the WEB-INF\classes or WEB-INF\lib directories.  Is this a bug in
Tomcat?

Nathan Klemperer
---------------------- Forwarded by Nathan Klemperer/Lex/Lexmark on 04/04/2000
11:25 AM ---------------------------

Nathan_Klemperer.LEXMARK@lexmta01.notes.lexmark.com on 03/30/2000 01:13:50 PM

Please respond to tomcat-dev%jakarta.apache.org@interlock.lexmark.com

To:   tomcat-dev%jakarta.apache.org@interlock.lexmark.com
cc:    (bcc: Nathan Klemperer/Lex/Lexmark)
Subject:  RE: ResourceBundle.getBundle() throws MissingResourceException in
       JSP



I think I know why you can't load a ResourceBundle from a JSP page.  When a JSP
is compiled by Jasper, it is loaded using the
org.apache.jasper.runtime.JspLoader ClassLoader.  This ClassLoader does not
implement getResourceAsStream(), which is used by ResourceBundle.getBundle().
Beans and Servlets are loaded by org.apache.tomcat.loader.AdaptiveClassLoader
which does implement getResourceAsStream().  Also, if you precompile your JSP
using JSPC, it works because it is now a servlet and loaded by
org.apache.tomcat.loader.AdaptiveClassLoader.

Is this a bug?  I think that JSP's should behave the same as precompiled JSP's.

Nathan Klemperer




jfrederic.clere%fujitsu.siemens.es@interlock.lexmark.com on 03/30/2000 12:15:28
PM

Please respond to tomcat-dev%jakarta.apache.org@interlock.lexmark.com

To:   tomcat-dev%jakarta.apache.org@interlock.lexmark.com
cc:    (bcc: Nathan Klemperer/Lex/Lexmark)
Subject:  RE: ResourceBundle.getBundle() throws MissingResourceException in  JSP



jakarta-tomcat/src/share/org/apache/tomcat/loader/AdaptiveClassLoader.java..
.
I get a problem in jserv and it dispairs after a fixe (not from me!):

--- ./src/java/org/apache/java/lang/AdaptiveClassLoader.java.org  Wed
Dec  8 21:33:25 1999
+++ ./src/java/org/apache/java/lang/AdaptiveClassLoader.java      Fri Feb  4
13:05:33 2000
@@ -656,18 +656,28 @@
      */
     private InputStream loadResourceFromZipfile(File file, String name) {
         ZipFile zipfile = null;
+    InputStream resourceStream = null;
         try {
             zipfile = new ZipFile(file);
             ZipEntry entry = zipfile.getEntry(name);

             if (entry != null) {
-                return zipfile.getInputStream(entry);
+                long length = entry.getSize();
+                resourceStream = zipfile.getInputStream(entry);
+                byte[] data = loadBytesFromStream(resourceStream,
(int)length);
+                return new ByteArrayInputStream(data);
             } else {
                 return null;
             }
         } catch(IOException e) {
             return null;
         } finally {
+            if ( resourceStream != null ) {
+                try {
+                    resourceStream.close();
+                } catch ( IOException ignored ) {
+                }
+            }
             if ( zipfile != null ) {
                 try {
                     zipfile.close();

Any way the code we use looks bad (the close closes the InputStream... do
not it?).
 <<patch.help.notmine>>

> -----Original Message-----
> From:   nathank@lexmark.com [SMTP:nathank@lexmark.com]
> Sent:   Thursday, March 30, 2000 5:39 PM
> To:     tomcat-dev@jakarta.apache.org
> Subject:     ResourceBundle.getBundle() throws MissingResourceException
> in JSP
>
> When I try to get a ResourceBundle from a JSP, I get a
> MissingResourceException.
> But if I do it in a Bean called from the JSP, it works.  It also works
> from a
> servlet.  I have the properties file in the WEB-INF\classes directory.
> Anyone
> know why?  I think it has something to do with the ClassLoader.  I'm using
> the
> 20000327 build of Tomcat
>
> Nathan Klemperer
>
>
> Here is the JSP file:
>
> <%@ page language="java" %>
> <%@ page import="java.util.*" %>
>
> <HTML>
> <HEAD>
> <TITLE>
> Test
> </TITLE>
> </HEAD>
>
> <%
>    try
>    {
>       out.println("<p>Try to get ResourceBundle from
> ResourceBundle.getBundle()");
>
>       ResourceBundle rb = ResourceBundle.getBundle("LocalStrings");
>       out.println("<p>Success: " + rb.getString("helloworld.title"));
>    }
>    catch (MissingResourceException e)
>    {
>       out.println("<p>Failed: " + e);
>    }
>
>    try
>    {
>       out.println("<p>Try to get ResourceBundle from Bean");
>
>       ResourceBundle rb = new RB("LocalStrings").rb;
>       out.println("<p>Success: " + rb.getString("helloworld.title"));
>    }
>    catch (MissingResourceException e)
>    {
>       out.println("<p>Failed: " + e);
>    }
> %>
>
>
> Here is the Bean:
>
> public class RB
> {
>    public java.util.ResourceBundle rb;
>
>    public RB(String name)
>    {
>       rb = java.util.ResourceBundle.getBundle(name);
>    }
> }
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tomcat-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tomcat-dev-help@jakarta.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-dev-help@jakarta.apache.org







---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-dev-help@jakarta.apache.org






Mime
View raw message