当前位置: 代码迷 >> J2EE >> 关于java.lang.IllegalStateException,该怎么处理
  详细解决方案

关于java.lang.IllegalStateException,该怎么处理

热度:62   发布时间:2016-04-22 00:42:45.0
关于java.lang.IllegalStateException
我用servlet 生成 验证码 但是页面刷新多次 多次访问servlet 就会出现java.lang.IllegalStateException 

servlet代码如下:
public class VerifyCode extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public VerifyCode() {
super();
}

private int width = 80; 
private int height = 60; 
private int codeCount = 4;
private int xx = 0; // xx
private int fontHeight; 
private int codeY; 
char[] codeSequence = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' }; // codeSequence
public void init() throws ServletException {
String strWidth = this.getInitParameter("width");
String strHeight = this.getInitParameter("height");
String strCodeCount = this.getInitParameter("codeCount");

try {
if (strWidth != null && strWidth.length() != 0) {
width = Integer.parseInt(strWidth);
}
if (strHeight != null && strHeight.length() != 0) {
height = Integer.parseInt(strHeight);
}
if (strCodeCount != null && strCodeCount.length() != 0) {
codeCount = Integer.parseInt(strCodeCount);
}
} catch (NumberFormatException e) {
e.printStackTrace();
}

xx = width / (codeCount + 1);
fontHeight = height - 2;
codeY = height - 4;

}

/**
* @param req
* @param resp
* @throws ServletException
* @throws java.io.IOException
*/
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, java.io.IOException {

BufferedImage buffImg = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
Graphics2D gd = buffImg.createGraphics();

Random random = new Random();

gd.setColor(Color.WHITE);
gd.fillRect(0, 0, width, height);

Font font = new Font("Fixedsys", Font.PLAIN, fontHeight);
gd.setFont(font);

gd.setColor(Color.BLACK);
gd.drawRect(0, 0, width - 1, height - 1);

/* gd.setColor(Color.BLACK);
for (int i = 0; i < 16; i++) {
int x = random.nextInt(width);
int y = random.nextInt(height);
int xl = random.nextInt(120);
int yl = random.nextInt(120);
gd.drawLine(x, y, x + xl, y + yl);
}*/

StringBuffer randomCode = new StringBuffer();
int red = 0, green = 0, blue = 0;

for (int i = 0; i < codeCount; i++) {
String strRand = String.valueOf(codeSequence[random.nextInt(36)]);
red = random.nextInt(255);
green = random.nextInt(255);
blue = random.nextInt(255);

gd.setColor(new Color(red, green, blue));
gd.drawString(strRand, i * xx, codeY);

randomCode.append(strRand);
}
HttpSession session = req.getSession();
session.setAttribute("validateCode", randomCode.toString());

System.out.println(randomCode.toString());

resp.setHeader("Pragma", "no-cache");
resp.setHeader("Cache-Control", "no-cache");
resp.setDateHeader("Expires", 0);

resp.setContentType("image/jpeg");

ServletOutputStream sos = resp.getOutputStream();
ImageIO.write(buffImg, "jpeg", sos);
sos.flush();
sos.close();
return;
}

}

我正在做三大框架的整合 是通过struts访问的servlet

------解决方案--------------------
我的经验就是接受到的参数名称和传入的参数名称不一样
  相关解决方案