今天在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
查看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)
開始以為是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>
這Tomcat的版本比我的要老的多啊。
後來用tomcat的Lib目錄下的這兩個jar包替換掉之後,就好了。 JSP就可以編譯運行了。
總結一下,“Unable to compile class for JSP” 八成和編譯用的 “jsp-api.jar”是有關係的。