我做错了什么?
我在win 7中运行Apache tomcat 7作为服务
我的jsp代码[... \ webapps \ test \ index.jsp]
<jsp:useBean id="Counter" scope="session" class="aaa.Counter" />
<html>
<title>sfcsv</title>
<%
try{
int x = Counter.read_count(),z = Counter.get_id();
if(x%2==0)
out.println(x + " = even");
else
out.println(x + " = odd");
out.println(z);
}catch(Exception e){
out.println(e);
}
%>
</html>
java代码[.. \ webapps \ test \ WEB-INF \ classes \ aaa \ Counter.java]
package aaa;
public class Counter {
private int count;
private static int instance_counter;
private final int id;
public Counter(){
instance_counter ++;
id = instance_counter;
count = 0;
}
public int read_count(){
return count++;
}
public int get_id(){
return id;
}
}
预期产出:
24 = even 1
我得到的输出:
24 = even
或
HTTP Status 500 - Unable to compile class for JSP: An error occurred at line: 8 in the jsp file: /index.jsp The method get_id() is undefined for the type Counter 5: 6: <% 7: try{ 8: int x = Counter.read_count(),z = Counter.get_id(); 9: if(x%2==0) 10: out.println(x + " = even"); 11: else Stacktrace:
type Exception report
message Unable to compile class for JSP: An error occurred at line: 8 in the jsp file: /index.jsp The method get_id() is undefined for the type Counter 5: 6: <% 7: try{ 8: int x = Counter.read_count(),z = Counter.get_id(); 9: if(x%2==0) 10: out.println(x + " = even"); 11: else Stacktrace:
description The server encountered an internal error (Unable to compile class for JSP: An error occurred at line: 8 in the jsp file: /index.jsp The method get_id() is undefined for the type Counter 5: 6: <% 7: try{ 8: int x = Counter.read_count(),z = Counter.get_id(); 9: if(x%2==0) 10: out.println(x + " = even"); 11: else Stacktrace:) that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: Unable to compile class for JSP:
An error occurred at line: 8 in the jsp file: /index.jsp
The method get_id() is undefined for the type Counter
5:
6: <%
7: try{
8: int x = Counter.read_count(),z = Counter.get_id();
9: if(x%2==0)
10: out.println(x + " = even");
11: else
Stacktrace:
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:102)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:331)
org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:469)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:378)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:353)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:340)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:646)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:357)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.29 logs.
Apache Tomcat/7.0.29
编辑:重新启动计算机时问题消失
答案 0 :(得分:1)
您已经解决了问题,但是这里有适当的选项可以让您重新加载或重新编译所有类和JSP文件,这可能有助于了解您的案例中发生了什么:
刷新Java类 - 确保在对类应用更改后重新编译类。另请参阅Web应用程序上下文的reloadable
选项(默认为false
)。
如果您希望Catalina监视/ WEB-INF / classes /和/ WEB-INF / lib中的类以进行更改,则设置为true;如果检测到更改,则自动重新加载Web应用程序。
刷新JSP文件 - 有关development
变量的详细信息,请参阅Tomcat Jasper docs(默认为true
,所以我猜这是你的价值所在)和modificationTestInterval
变量(默认为4 seconds
),您可以使用该变量设置检查JSP文件的更改间隔,并在必要时进行编译。两者通常都设置在$CATALINA_BASE/conf/web.xml
。
Jasper是否在开发模式中使用?如果为true,则可以通过modificationTestInterval parameter.true或false指定检查JSP以进行修改的频率,默认为true。
答案 1 :(得分:0)
我认为你的代码没问题。我认为你的IDE没有正确构建你的项目 即.class文件未正确生成(或未生成)。所以尝试手动编译这个文件并将生成的.class文件放到项目的相应文件夹中,然后构建并重新启动你的服务器。检查是否构建它构建新的.JAR文件