在tomcat5下jsp中出现此错误一般都是在jsp中使用了输出流(如输出图片验证码,文件下载等),
没有妥善处理好的原因。具体的原因就是 在tomcat中jsp编译成servlet之后在函数_jspService(HttpServletRequest request, HttpServletResponse response)的最后 有一段这样的代码
finally {
if (_jspxFactory != null) _jspxFactory.releasePageContext(_jspx_page_context);
}
这里是在释放在jsp中使用的对象,会调用response.getWriter(),因为这个方法是和
response.getOutputStream()相冲突的!所以会出现以上这个异常。
开始我用的是直接img 标签的src属性里直接放一个地址从ftp直接取图片流,但是循环取多张图片的时候会出现这个异常,单个图片不会有问题
ajax循环的话也不行,之后大佬帮我写了一个方法用的 Image()对象onload一个个载入就好了
html页面
<c:forEach items="${imgUrlList}" var="imgUrl">
<div>
<input type="hidden" id="${imgUrl.id}" imgname="${imgUrl.name}" name="recordAddress" value="${imgUrl.recordAddress}"/>
</div>
<hr>
</c:forEach>
js代码
function onloadImg(){
$('input[name=recordAddress]').each(function(index){
if(index == 0){
var obj=new Image();
var id = $(this).attr("id");
var imgname = $(this).attr("imgname");
obj.src='${ctx}/bm/student/bmStudents/getPhoto?photoURL='+$(this).val();
obj.onload=function(){
$('#'+id).after("<label>"+imgname+"</label><br><img style='width: auto;height: auto;max-width: 70%;max-height: 70%;' src='"+obj.src+"' />");
$("#"+id).remove();
onloadImg();
}
}
});
}
document.ready后调用就可以了
本文探讨了在Tomcat5环境下使用JSP时遇到的getOutputStream()hasalreadybeencalledforthisresponse异常现象,主要原因是输出流处理不当。文章详细解释了异常产生的根本原因,并提供了一种使用Image对象逐个加载图片的解决方案。
4628

被折叠的 条评论
为什么被折叠?



