今天在tomcat(7.0.8)上运行一本书的JSP源代码时,出现如下错误:
1
org.apache.jasper.JasperException:
Unable to compile class for JSP:
2
3 An error occurred at line: 23 in the generated java file
4 The method getJspApplicationContext(ServletContext) is undefined for the type JspFactory
5
6 Stacktrace:
7 org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java: 95 )
8 org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java: 330 )
9 org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java: 457 )
10 org.apache.jasper.compiler.Compiler.compile(Compiler.java: 367 )
11 org.apache.jasper.compiler.Compiler.compile(Compiler.java: 345 )
12 org.apache.jasper.compiler.Compiler.compile(Compiler.java: 332 )
13 org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java: 594 )
14 org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java: 342 )
15 org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java: 391 )
16 org.apache.jasper.servlet.JspServlet.service(JspServlet.java: 334 )
17 javax.servlet.http.HttpServlet.service(HttpServlet.java: 722 )
18
19
2
3 An error occurred at line: 23 in the generated java file
4 The method getJspApplicationContext(ServletContext) is undefined for the type JspFactory
5
6 Stacktrace:
7 org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java: 95 )
8 org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java: 330 )
9 org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java: 457 )
10 org.apache.jasper.compiler.Compiler.compile(Compiler.java: 367 )
11 org.apache.jasper.compiler.Compiler.compile(Compiler.java: 345 )
12 org.apache.jasper.compiler.Compiler.compile(Compiler.java: 332 )
13 org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java: 594 )
14 org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java: 342 )
15 org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java: 391 )
16 org.apache.jasper.servlet.JspServlet.service(JspServlet.java: 334 )
17 javax.servlet.http.HttpServlet.service(HttpServlet.java: 722 )
18
19
查看Tomcat的Log,发现如下错误:
1
SEVERE: Failed to initialize end point associated with ProtocolHandler
[
"http-apr-8088"
]
2 java.lang.Exception: Socket bind failed: [ 730048 ] Only one usage of each socket address (protocol/network address/port) is normally permitted.
3 at org.apache.tomcat.util.net.AprEndpoint.bind(AprEndpoint.java: 408 )
4 at org.apache.tomcat.util.net.AbstractEndpoint.init(AbstractEndpoint.java: 511 )
5 at org.apache.coyote.AbstractProtocolHandler.init(AbstractProtocolHandler.java: 345 )
6 at org.apache.catalina.connector.Connector.initInternal(Connector.java: 910 )
7 at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java: 101 )
8 at org.apache.catalina.core.StandardService.initInternal(StandardService.java: 559 )
9 at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java: 101 )
10 at org.apache.catalina.core.StandardServer.initInternal(StandardServer.java: 778 )
11 at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java: 101 )
12 at org.apache.catalina.startup.Catalina.load(Catalina.java: 572 )
13 at org.apache.catalina.startup.Catalina.load(Catalina.java: 595 )
14 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
15 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java: 39 )
16 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java: 25 )
17 at java.lang.reflect.Method.invoke(Method.java: 597 )
18 at org.apache.catalina.startup.Bootstrap.load(Bootstrap.java: 263 )
19 at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java: 431 )
2 java.lang.Exception: Socket bind failed: [ 730048 ] Only one usage of each socket address (protocol/network address/port) is normally permitted.
3 at org.apache.tomcat.util.net.AprEndpoint.bind(AprEndpoint.java: 408 )
4 at org.apache.tomcat.util.net.AbstractEndpoint.init(AbstractEndpoint.java: 511 )
5 at org.apache.coyote.AbstractProtocolHandler.init(AbstractProtocolHandler.java: 345 )
6 at org.apache.catalina.connector.Connector.initInternal(Connector.java: 910 )
7 at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java: 101 )
8 at org.apache.catalina.core.StandardService.initInternal(StandardService.java: 559 )
9 at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java: 101 )
10 at org.apache.catalina.core.StandardServer.initInternal(StandardServer.java: 778 )
11 at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java: 101 )
12 at org.apache.catalina.startup.Catalina.load(Catalina.java: 572 )
13 at org.apache.catalina.startup.Catalina.load(Catalina.java: 595 )
14 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
15 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java: 39 )
16 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java: 25 )
17 at java.lang.reflect.Method.invoke(Method.java: 597 )
18 at org.apache.catalina.startup.Bootstrap.load(Bootstrap.java: 263 )
19 at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java: 431 )
开始以为是Server的配置有问题,网上搜了一下,发现都说是Tomcat开了多个造成,或者是JSP的code有问题。
但是自己又试了试Tomcat自带的JSP,人家的jsp运行的好好的。
所以觉得Tomcat应该没有什么配置上的问题。
后来查到这本书带的源代码中的build.xml文件,是这样设置的:
1
<
property
environment
="env"
/>
2 < property name ="src" value ="WEB-INF/src" />
3 < property name ="classes" value ="WEB-INF/classes" />
4 < property name ="lib" value ="WEB-INF/lib" />
5 < property name ="dist" value ="dist" />
6
7 < path id ="task.classpath" >
8 < pathelement location ="${classes}" />
9 < pathelement location ="${lib}" />
10
11 <!-- Tomcat 5.0.16 Servlet 2.4 API -->
12 < pathelement location ="${lib}/servlet-api.jar" />
13
14 <!-- Tomcat 5.0.16 JSP 2.0 API -->
15 < pathelement location ="${lib}/jsp-api.jar" />
16
17 </ path >
2 < property name ="src" value ="WEB-INF/src" />
3 < property name ="classes" value ="WEB-INF/classes" />
4 < property name ="lib" value ="WEB-INF/lib" />
5 < property name ="dist" value ="dist" />
6
7 < path id ="task.classpath" >
8 < pathelement location ="${classes}" />
9 < pathelement location ="${lib}" />
10
11 <!-- Tomcat 5.0.16 Servlet 2.4 API -->
12 < pathelement location ="${lib}/servlet-api.jar" />
13
14 <!-- Tomcat 5.0.16 JSP 2.0 API -->
15 < pathelement location ="${lib}/jsp-api.jar" />
16
17 </ path >
这Tomcat的版本比我的要老的多啊。
后来用tomcat的Lib目录下的这两个jar包替换掉之后,就好了。 JSP就可以编译运行了。
总结一下,“ Unable to compile class for JSP ” 八成和编译用的 “ jsp-api.jar ”是有关系的。