登录验证码代码,一个jsp,在Tomcat下没有问题,但放到weblogic下,每次请求都会报错:
java.io.IOException: response already committed
但使用没有问题。
作如下修改即可不报错:
加上response.reset();
Java代码
// 图象生效
g.dispose();
//解决WebLogic下报错java.io.IOException: response already committed
response.reset();
注释:
Java代码
// 输出图象到页面
ImageIO.write(image, "JPEG", response.getOutputStream());
//解决java.lang.IllegalStateException: getOutputStream() has already been called for this response
//WebLogic下要注释掉下面两句
//out.clear();
//out = pageContext.pushBody();
实例代码(TOMCAT下面的正确代码)
<%@page contentType="image/jpeg" import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*" pageEncoding="UTF-8" %>
<%!
Color getRandColor(int cc,int bb)
{
Random random = new Random();
if(cc>255) cc=255;
if(bb>255) bb=255;
int r=cc+random.nextInt(bb-cc);
int g=cc+random.nextInt(bb-cc);
int b=cc+random.nextInt(bb-cc);
return new Color(r,g,b);
} //获取随机颜色
%>
<%
response.setHeader("Pragma","No-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires", 0);
int width=80; //定义验证码图片的长度
int height=30; //定义验证码图片的宽度
BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
Random random = new Random();
g.setColor(getRandColor(200,250));
g.fillRect(0, 0, width, height);
g.setFont(new Font("Times New Roman",Font.PLAIN,18));
//定义字体形式
g.setColor(getRandColor(160,200));
for (int i=0;i<155;i++)
{
int i_x = random.nextInt(width);
int i_y = random.nextInt(height);
int i_xl = random.nextInt(12);
int i_yl = random.nextInt(12);
g.drawLine(i_x,i_y,i_x+i_xl,i_y+i_yl);
}
//用线条画背景
String s_Rand="";
for (int i=0;i<4;i++)
{
String rand=String.valueOf(random.nextInt(10));
s_Rand+=rand;
g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));
g.drawString(rand,13*i+6,16);
}
//产生4位随机码
session.setAttribute("rand",s_Rand);
//将验证码存入Session中
g.dispose();
ImageIO.write(image, "JPEG", response.getOutputStream());
//输出验证图片
out.clear();
out = pageContext.pushBody();
%>
weblogic下面的代码
<%@page contentType="image/jpeg" import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*" pageEncoding="UTF-8" %>
<%!
Color getRandColor(int cc,int bb)
{
Random random = new Random();
if(cc>255) cc=255;
if(bb>255) bb=255;
int r=cc+random.nextInt(bb-cc);
int g=cc+random.nextInt(bb-cc);
int b=cc+random.nextInt(bb-cc);
return new Color(r,g,b);
} //获取随机颜色
%>
<%
response.setHeader("Pragma","No-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires", 0);
int width=80; //定义验证码图片的长度
int height=30; //定义验证码图片的宽度
BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
Random random = new Random();
g.setColor(getRandColor(200,250));
g.fillRect(0, 0, width, height);
g.setFont(new Font("Times New Roman",Font.PLAIN,18));
//定义字体形式
g.setColor(getRandColor(160,200));
for (int i=0;i<155;i++)
{
int i_x = random.nextInt(width);
int i_y = random.nextInt(height);
int i_xl = random.nextInt(12);
int i_yl = random.nextInt(12);
g.drawLine(i_x,i_y,i_x+i_xl,i_y+i_yl);
}
//用线条画背景
String s_Rand="";
for (int i=0;i<4;i++)
{
String rand=String.valueOf(random.nextInt(10));
s_Rand+=rand;
g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));
g.drawString(rand,13*i+6,16);
}
//产生4位随机码
session.setAttribute("rand",s_Rand);
//将验证码存入Session中
g.dispose();
response.reset();
ImageIO.write(image, "JPEG", response.getOutputStream());
//输出验证图片
//out.clear();
//out = pageContext.pushBody();
%>
(网上有的说下面的这种写法不行,不能分行导入JAR包,经测试发现完全正常可用。)
<%@page import=" java.io.*"%>
<%@page import="java.awt.image.BufferedImage"%>
<%@page import=" javax.imageio.*"%>
<%@page import=" com.sun.image.codec.jpeg.*"%>
<%@page import=" com.htsoft.core.util.CreateFile"%>
<%@page pageEncoding="UTF-8" %>
<%
String path=request.getParameter("path");
String file = CreateFile.getFilepath()+path;
InputStream imageIn=null;
try{
response.reset();
response.setContentType("image/jpeg;charset=UTF-8");
OutputStream output=response.getOutputStream();
//得到图片的文件流
imageIn = new FileInputStream(new File(file));
//得到输入的编码器,将文件流进行jpg格式编码
JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(imageIn);
//得到编码后的图片对象
BufferedImage image = decoder.decodeAsBufferedImage();
//得到输出的编码器
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(output);
encoder.encode(image);//对图片进行输出编码
imageIn.close();//关闭文件流
output.close();
//out.clear();
//out = pageContext.pushBody();
}catch(FileNotFoundException e){
System.out.println("!新的图片流输出方式 :"+file);
}finally{
if(imageIn!=null){
imageIn.close();
}
}
%>
相关推荐
tomcat 调用weblogic EJB tomcat 调用weblogic EJB
服务器tomcat和weblogic区别[参考].pdf
Tomcat和weblogic中文乱码问题解决方案
weblogic项目移植到tomcat,配置说明
1、 介绍安装步骤,重点注意事项; 2、 集群规划,搭建主要步骤,重点注意事项; 3、 编制简单脚本进行优化,参数调优。 4、 最全面、最实用 5、 教程适用于各种版本 ...脚本进行优化,tomcat参数调优
项目框架从tomcat到weblogic
此文档描述了tomcat、weblogic、jboss的区别,并且根据不同的环境运用不同的容器,做了解析。
tomcat和weblogic共2页.pdf.zip
修正sitemesh3.0.1在tomcat和weblogic下使用Servlet3.1环境时出现装配文件不完整的BUG,亲测可用
tomcat、weblogic、jboss的区别,容器的作用
图文并茂,详细描述pinpoint安装部署、监控tomcat、weblogic、webSphere 实际操作,自己编写
搭建Eclipse3.2.2+MyEclipse5.5+Tomcat+Weblogic
自定义的zabbix tomcat及weblogic监控模板,包含图形、监控项、告警等配置,比默认的齐全
tomcat和weblogic的jmx开通方式。。
tomcat_Jboss_weblogic区别、容器的作用
weblogic和tomcat 下载附件乱码问题 weblogic和tomcat 下载附件乱码问题
在LINUX 下配置 WEBLOGIC及 TOMCAT服务器SSL协议的具体步骤。
tomcat工程迁移至weblogic文档,很详细的文档,恶意参考进行配置
以tomcat-5.0.28为例,改造后的目录结构。 运行\tomcat-5.0.28\user_projects\domains\TOMCAT_7302\startTomcat.cmd后,地址栏中输入http://localhost:7302/TEST-WEB,看到“hello world!”表示启动成功。
liunx下安装java tomcat weblogic 非常好,看看了