繁体   English   中英

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

[英]How to refer to non final variable “out” from inside class?

我已经看到了与该主题相关的问题,但我找不到满意的答案。 请帮助我使用我的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。 在这种情况下我该怎么办?

谢谢

<%
        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.
        }
    });
  %>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM