验证码出错
//我参考的这个程序,验证码图片运行时出不来,点击“看不清”刷新的是用户的姓名和密码,并没有刷新验证码,并且姓名和密码为什么会自动填写,各位大神能给一些帮助吗?谢谢!<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
<script type="text/javascript">
function toreal() {
document.location.href = "index.jsp";
}
</script>
<body>
<table>
<form action="" method="post">
<b>请输入姓名: <input type="text" name="userName"></b><br> <b>请输入密码:<input
type="password" name="userPassWord"></b><br> <b>验证码: <input
type="text" name="code"> <img alt="验证码"
src="/code/user_code/Code"> <a href="javascript:toreal()">看不清</a></b><br>
<input type="submit" value="登录">
</form>
</table>
</body>
</html>
public class Code extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 通知浏览器不要缓存
response.setHeader("Expires", "-1");
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Pragma", "-1");
int width = 25, height = 120;
// 得到一个内存图像BufferedImage
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
// 得到一个画笔
Graphics g = image.getGraphics();
// 画边框drawRect绘制指定矩形的边框。
g.drawLine(0, 0, width, height);
// 填充颜色
g.setColor(Color.blue);
g.fillRect(1, 1, width - 2, height - 2);
// 画干扰线
g.setColor(Color.BLACK);
Random rnd = new Random();
for (int i = 100; i >= 0; i--) {
g.drawLine(rnd.nextInt(width), rnd.nextInt(height),
rnd.nextInt(width), rnd.nextInt(height));
}
// 生成随机数字
g.setColor(Color.green);
g.setFont(new Font("微软雅黑", Font.BOLD | Font.ITALIC, 20));// BOLD加粗,ITALIC斜体
int d = 15;
for (int j = 0; j < 4; j++) {
g.drawString(rnd.nextInt(10) + "", d, 20);
d += 20;
}
// 输出到web页面
ImageIO.write(image, "jpg", response.getOutputStream());
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}