简体   繁体   中英

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

I have seen questions related to the topic, but i couldn't find a satisfactory answer. Please help me with my JSP code:

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

since out is a jsp variable, i cant define it as a final as well. What do i do in this case?

Thanks

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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