2012-07-15 71 views
0

我做錯了什麼?最終變量不工作在jsp

我運行的Apache Tomcat 7中獲勝服務7

我的JSP代碼[... \的webapps \測試\的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代碼[.. \ web應用\測試\ WEB-INF \類\ 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

您確定Counter類已更新在你的服務器的webapp? – SJuan76 2012-07-15 17:07:58

+0

我編譯java文件後重新啓動了tomcat服務器 我該怎麼辦? – Soumy 2012-07-15 17:10:49

+0

我測試了這個,它工作正常。雖然w/Tomcat 6 + JDK 1.6。你重建你的戰爭,包括get_id()? – Reimeus 2012-07-15 17:24:45

回答

1

你已經解決了你的問題,但這裏有相應的選項讓所有的類和JSP文件重新加載或重新編譯,這可能有助於理解你的情況:

刷新Java類 - 確保在對其應用更改後重新編譯您的類。對於您的Web應用程序上下文,請參閱reloadable選項(默認爲false)。如果你想卡塔利娜監視/ WEB-INF /班/和/ WEB-INF/lib目錄類的變化,並自動如果檢測到變化重新加載web應用

設置爲true。


刷新JSP文件 - 請參閱Tomcat Jasper docs有關development可變信息(默認爲true所以我想這是它在你的情況值)和modificationTestInterval變量(默認爲4 seconds)您可以設置JSP文件檢查更改的時間間隔,並在必要時進行編譯。兩者通常設置爲$CATALINA_BASE/conf/web.xml

Jasper是否在開發模式中使用?如果爲true,則可以通過modifyTestInterval參數指定檢查JSP進行修改的頻率.true或false,默認爲true。

+0

非常感謝答案。 這條指令正是我所缺乏的 – Soumy 2012-07-17 18:05:44

0

我認爲它不是問題,您code.I認爲你的IDE不正確構建項目不能正常產生 即.class文件(或不產生)。因此請嘗試手動編譯此文件,並將生成的.class文件放置到項目的相應文件夾中,然後生成並重新啓動服務器。檢查是否構建其新的.JAR文件

+0

我還沒有使用IDE來獲得對這個過程的理解。但感謝您的回覆 – Soumy 2012-07-17 18:03:05

+0

我們歡迎你的朋友!!! – 2012-07-18 09:10:28