2013-02-19 28 views
0

我ENV碼頭8.1ServletRequest中的NoSuchMethodError

public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException { 
    //this line prints org.eclipse.jetty.server.Request 
    System.out.println("class is "+request.getClass().getName()); 

    org.eclipse.jetty.server.Request jettyRequest = (org.eclipse.jetty.server.Request)request; 
    //request.getServletContext();//this line fails with noSuchMethodError 
    jettyRequest.getServletContext(); // this line works 

    ... 

任何人都可以解釋這一點....?

回答

2

你的web應用沒有使用Servlet API的3.0。

這樣,如果你正在使用的Servlet API 2.5(例如)失敗。

確保您的web應用程序的WEB-INF/web.xml已設置爲Servlet API 3.0,並確保刪除了您在WEB-INF/lib中可能具有的任何servlet-api jar。

+0

參考:http://docs.oracle.com/javaee/6/api/javax/servlet/ServletRequest.html#getServletContext() – madth3 2013-02-20 00:51:26

+0

爲什麼該函數調用工作,如果我像在代碼中那樣轉換對象以上? – root1982 2013-02-20 05:44:25

+0

你就鑄造'org.eclipse.jetty.server.Request'這已經多年,方法,因爲它是提供基本的servlet支持一個有用的方法(內部爲碼頭類)。然而,servlet的API SPEC最近纔將其添加到該servlet 3.0 API。 (請參閱從madth3 javadoc的鏈接) – 2013-02-20 13:40:48