我看过与该主题相关的问题,但找不到满意的答案。请帮助我的 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/