如何从类内部引用非最终变量“out”?

时间:2012-03-12 18:26:23

标签: java jsp

我见过与该主题相关的问题,但我找不到满意的答案。请帮助我使用我的JSP代码:

 <%
        AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
        Future<Integer> f = asyncHttpClient.prepareGet("http://www.ning.com/")
          .execute(new AsyncCompletionHandler<Integer>(){

        public STATE onStatusReceived(HttpResponseStatus respstat)throws Exception{
                    ***//error occurs in next line***
            out.println(respstat.getStatusText());
            return STATE.CONTINUE;
        }
        @Override
        public Integer onCompleted(Response response) throws Exception{
            // Do something with the Response
            out.println(response.getStatusCode());
            return response.getStatusCode();
        }

        @Override
        public void onThrowable(Throwable t){
            // Something wrong happened.
        }
    });
  %>

因为out是一个jsp变量,我也不能将它定义为final。在这种情况下我该怎么做?

由于

1 个答案:

答案 0 :(得分:4)

<%
        final ServletOutputStream finalOut = out; // create final reference

        AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
        Future<Integer> f = asyncHttpClient.prepareGet("http://www.ning.com/")
          .execute(new AsyncCompletionHandler<Integer>(){

        public STATE onStatusReceived(HttpResponseStatus respstat)throws Exception{
                    ***//error occurs in next line***
            finalOut.println(respstat.getStatusText());  //use final reference
            return STATE.CONTINUE;
        }
        @Override
        public Integer onCompleted(Response response) throws Exception{
            // Do something with the Response
            out.println(response.getStatusCode());
            return response.getStatusCode();
        }

        @Override
        public void onThrowable(Throwable t){
            // Something wrong happened.
        }
    });
  %>