java - 如何从类内部引用非最终变量 "out"?

标签 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。在这种情况下我该怎么办?

谢谢

最佳答案

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

关于java - 如何从类内部引用非最终变量 "out"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9672657/

相关文章:

java - 找不到hbase.mapreduce.TableOutputFormat

java - Eclipse/OSGI、Java 11、JAXB 和类加载器

java - 使用 GSON 解析嵌套的 Json

java - 如何将 LocalDate 转换为 ChronoZonedDateTime?

java - 无法迭代列表 - JSTL

java - hibernate 组织. hibernate .LazyInitializationException : failed to lazily initialize a collection of role:

jsp - 指定 JSTL <c :set> value via attribute and body 之间的区别

javascript - 如何使用 html 或 jsp 将数据以 xml 形式发布到服务器

eclipse - 在 Eclipse Helios 中构建工作区时出错

java - 为什么 cookie.getMaxAge() = -1?