現象說明:能刷新出字符,但是常會拋出異常,說是STRING定義有異常,對照老師所講的,沒有錯。有時候在第一次刷新時出現。有時連續要刷新幾次才會出現
異常現象:
016-8-4 15:16:06 org.apache.catalina.core.StandardWrapperValve invoke
嚴重: Servlet.service() for servlet ValidataServlet threw exception2016-8-4 15:16:08 org.apache.catalina.core.StandardWrapperValve invoke
源代碼:
//response.setContentType("text/html");
//PrintWriter out = response.getWriter();
//創建圖像
int width=100;
int height=30;
BufferedImage image=new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
//創建圖層
Graphics g=image.getGraphics();
//確立畫筆顏色
g.setColor(Color.BLACK);
//創建矩形
g.fillRect(0, 0, width, height);
//在大矩形中畫小矩形
g.setColor(Color.WHITE);
g.fillRect(1, 1, width-2, height-2);
//填充字符
String data="abcdefghijklmnuvwxyzABCDEFGHIJKLMNUVWXYZ1234567890";
//隨機獲取4個字符做為驗證碼
Random random=new Random();
for (int i = 0; i < 4; i++) {
//從DATA的62個字符中隨機取的4個字符
int index=random.nextInt(62);
g.setColor(Color.RED);
g.setFont(new Font("宋體", Font.BOLD, 22));
String str=data.substring(index, index+1);
g.drawString(str, 25*i, 26);
}
//服務器響應頁面的請求
response.setContentType("image/jpeg");
//輸出圖像到瀏覽器
ImageIO.write(image, "jpg", response.getOutputStream());
現象說明:能刷新出字符,但是常會拋出異常
發表回復: | ||||||||||||||||
|