Bug 34465

Summary: jasper2 fails when there is no web.xml
Product: Tomcat 5 Reporter: Yoichi Hirose <Yoichi.Hirose>
Component: JasperAssignee: Tomcat Developers Mailing List <dev>
Severity: normal    
Priority: P2    
Version: 5.5.9   
Target Milestone: ---   
Hardware: PC   
OS: Windows XP   
Attachments: A patch to catch the Exception raised from uri.openStream()
A patch for JspCServletContext#getResource

Description Yoichi Hirose 2005-04-15 06:50:23 UTC
When running jasper2 from ant, and there is no web.xml under root/WEB-INF,
jasper2 fails with a FileNotFoundException. Since, this worked correctly under
5.5.7, it seems to be a bug.

The actual place which causes the problem is in
org.apache.jasper.compiler.JspConfig#processWebDotXml(). The code in 5.5.7 was
            is = ctxt.getResourceAsStream(WEB_XML);
            if (is == null) {
                // no web.xml
and in 5.5.9,
            URL uri = ctxt.getResource(WEB_XML);
            if (uri == null) {
                // no web.xml
ctxt.getResource(WEB_XML) returns a non-null value even if there is no web.xml
existing, so ths next uri.openStream() raises a FileNotFoundException.
Comment 1 Remy Maucherat 2005-04-15 10:14:14 UTC
"ctxt.getResource(WEB_XML) returns a non-null value even if there is no web.xml
existing, so ths next uri.openStream() raises a FileNotFoundException."

This is incorrect.
Comment 2 Yoichi Hirose 2005-04-15 10:36:32 UTC
I'm sorry, maybe my guess of the reason was wrong. But jasper2 actually raises 
a FileNotFoundException somewhere, where it did not in 5.5.7. Please check it 
by yourself. I will show you the stack trace. Some parts are modified, since 
it includes Japanese messages.

  [jasper2] java.io.FileNotFoundException: C:\work\harp\web\portal\root\WEB-
INF\web.xml (Japanese error message comes here)
  [jasper2]     at java.io.FileInputStream.open(Native Method)
  [jasper2]     at java.io.FileInputStream.<init>(FileInputStream.java:106)
  [jasper2]     at java.io.FileInputStream.<init>(FileInputStream.java:66)
  [jasper2]     at sun.net.www.protocol.file.FileURLConnection.connect
  [jasper2]     at sun.net.www.protocol.file.FileURLConnection.getInputStream
  [jasper2]     at java.net.URL.openStream(URL.java:1007)
  [jasper2]     at org.apache.jasper.compiler.JspConfig.processWebDotXml
  [jasper2]     at org.apache.jasper.compiler.JspConfig.init
  [jasper2]     at org.apache.jasper.compiler.JspConfig.isJspPage
  [jasper2]     at org.apache.jasper.JspC.scanFiles(JspC.java:919)
  [jasper2]     at org.apache.jasper.JspC.execute(JspC.java:958)
  [jasper2]     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  [jasper2]     at sun.reflect.NativeMethodAccessorImpl.invoke
  [jasper2]     at sun.reflect.DelegatingMethodAccessorImpl.invoke
  [jasper2]     at java.lang.reflect.Method.invoke(Method.java:585)
  [jasper2]     at org.apache.tools.ant.TaskAdapter.execute
  [jasper2]     at org.apache.tools.ant.UnknownElement.execute
  [jasper2]     at org.apache.tools.ant.Task.perform(Task.java:364)
  [jasper2]     at org.apache.tools.ant.Target.execute(Target.java:341)
  [jasper2]     at org.apache.tools.ant.Target.performTasks(Target.java:369)
  [jasper2]     at org.apache.tools.ant.Project.executeTarget
  [jasper2]     at org.apache.tools.ant.Project.executeTargets
  [jasper2]     at org.apache.tools.ant.Main.runBuild(Main.java:673)
  [jasper2]     at org.apache.tools.ant.Main.startAnt(Main.java:188)
  [jasper2]     at org.apache.tools.ant.launch.Launcher.run(Launcher.java:196)
  [jasper2]     at org.apache.tools.ant.launch.Launcher.main(Launcher.java:55)
Comment 3 Remy Maucherat 2005-04-15 10:42:11 UTC
As I said, this works for me, and I explicitely tested it.
ctxt.getResource(WEB_XML) returns null.

Since you insist, I'll leave your bug open, but will ignore it. I recommend you
look into this "bug" yourself.
Comment 4 Remy Maucherat 2005-04-15 10:48:44 UTC
Acrually, no, I didn't test it properly, because you forgot to tell that you
were precompiling JSPs (something I just saw by looking at the end of the stack
trace right after posting my last reply).

If you precompile with jspc, you need to have a web.xml, as all the new servlet
declarations will be put there. Otherwise, your precompilation will be useless
Comment 5 Yoichi Hirose 2005-04-15 11:01:49 UTC
I'm sorry for forgetting to tell you that I was trying to precompile the JSP's.
I wrote that I was using ant, so I thought that was enough.

> If you precompile with jspc, you need to have a web.xml, as all the new servlet
> declarations will be put there. Otherwise, your precompilation will be useless
> anyway

This is not true. jasper2 has an option called webXmlFragment, which creates
part of web.xml, and what we are doing, is joining these webXmlFragments. It
worked fine on 5.5.7.

Well, I'm working on the code myself now, so I will tell you when I have found a
way to fix the problem.
Comment 6 Remy Maucherat 2005-04-15 11:07:07 UTC
Using jspc means using jspc. Using ant means using ant to compile JSP source.

Anyway, look in org.apache.jasper.servlet.JspCServletContext.
Comment 7 Henri Gomez 2005-04-15 11:22:29 UTC
Well jasper2 could put jsp directive outside web.xml when pre-compiling :

see this ant example: 

		   		 trimSpaces="true" /> 

jasper2 jspc will put the jsp URI in jsp.xml, you could at a later time include
in web.xml using <!ENTITY jsp SYSTEM  "jsp.xml"> and &jsp.xml;

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    "http://java.sun.com/dtd/web-app_2_3.dtd" [ 

<!ENTITY jsp SYSTEM  "jsp.xml">






Be carefull &jsp; should be in the servlet-mapping area of your web.xml
Comment 8 Yoichi Hirose 2005-04-15 11:28:07 UTC
(In reply to comment #6)
> Anyway, look in org.apache.jasper.servlet.JspCServletContext.

As you said, I looked at JspCServletContext#getRealPath and
JspCServletContext#getResource. They just generates a new URL, and does not
check the existents of the actual file.

In 5.5.7, getResourceAsStream was used insted of getResource, and the
implementation of getResourceAsStream is as below.

    public InputStream getResourceAsStream(String path) {

        try {
            return (getResource(path).openStream());
        } catch (Throwable t) {
            return (null);


Since Throwable is caught, FileNotFoundException will not be raised to

Well the easiest way to fix this problem, is modifiy
	    is = uri.openStream();
	    try {
		is = uri.openStream();
	    } catch (java.io.FileNotFoundException ex) {
or maybe catch a Throwable, as it does in getResourceAsStream.

I modified JspConfig.java on my local machine as above, and it correctly worked
as I wanted.
Comment 9 Yoichi Hirose 2005-04-15 12:32:05 UTC
Created attachment 14728 [details]
A patch to catch the Exception raised from uri.openStream()

This is a propsed patch for this bug. Just catch Throwable as
JspCServletContext#getResourceAsStream does so.
Comment 10 Yoichi Hirose 2005-04-22 10:41:55 UTC
Created attachment 14788 [details]
A patch for JspCServletContext#getResource

Maybe a better patch. JspCServletContext#getResource returns null if the file
is not found.