jsp通过<img>标签的src属性来调用servlet类生成验证码遇到的问题

在配置了注解或者xml都正确的情况下

包如下:



xml 配置如下:


jsp页面(action="" method="post")<img src>:


都配置好了,servlet生成代码也没问题,可是不显示验证码。

我做了如下修改:

修改方法一:把servlet中doPost方法改为doGet方法就出现验证码的图片了(jsp里提交的方式还是post方式)。

修改方法二:同时写doPost和doGet方法,在doGet方法里调用doPost方法





Servlet代码:

public class ValidateCodeServlet extends HttpServlet {


@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(req, resp);
}


@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
// System.out.println("V");
// 禁止页面缓存
resp.setHeader("Pragma", "No-cache");
resp.setHeader("Cacahe-Control", "No-Cache");
resp.setDateHeader("Expires", 0);
resp.setContentType("image/jpeg");
int width = 60, height = 20;
/* 创建一个位于缓存的图像,宽度为60,高度为20 */
BufferedImage image = new BufferedImage(height, width, BufferedImage.TYPE_INT_BGR);
Graphics g = image.getGraphics();
Random rand = 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(this.getRandColor(160, 200));// 按Table键自动跳到下一个


for (int i = 0; i < 130; i++) {// 随机130条随机线
int x = rand.nextInt(width);
int y = rand.nextInt(height);
int x1 = rand.nextInt(12);
int y1 = rand.nextInt(12);
g.drawLine(x, y, x + x1, y + y1); // 在图像坐标(x,y)和坐标(x+x1,y+y1)之间话干扰线
}


String strCode = "";
for (int i = 0; i < 4; i++) {
String strNum = String.valueOf(rand.nextInt(10));
strCode = strCode + strNum;
// 设置字体的颜色
g.setColor(new Color(15 + rand.nextInt(120), 15 + rand.nextInt(120), 15 + rand.nextInt(120)));
g.drawString(strNum, 13 * i + 6, 16); // 将验证码依次画在图像上,坐标为(x=13*i+6,y=16)
System.out.println(strNum);
}


req.getSession().setAttribute("Code", strCode); // 把验证码保存到session中
g.dispose(); // 释放此图像的上下文以及它使用的所有系统资源
ImageIO.write(image, "JPEG", resp.getOutputStream()); // 输出JPEG格式的图像
resp.getOutputStream().flush(); // 刷新输出流
resp.getOutputStream().close(); // 关闭输出流
}


public Color getRandColor(int fc, int bc) {
Random rand = new Random();
Color c = null;
if (fc > 255)
fc = 255;
if (bc > 255)
bc = 255;
// 置0-255之间的随机颜色
int r = fc + rand.nextInt(bc - fc);
int g = fc + rand.nextInt(bc - fc);
int b = fc + rand.nextInt(bc - fc);
c = new Color(r, g, b);
return c;
}
}


jsp代码:

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
<%-- 
<%
String basepath = request.getScheme() + "://" + request.getServerName() 
                  + ":" + request.getServerPort() + request.getContextPath() + "/";
%>
<base href="<%=basepath%>">
 --%>
<!-- 
<script type="text/javascript">
    //刷新验证码
    function changeImg(){
        document.getElementById("validateCodeImg").src="helpDrawValidateCode?t=" + Math.random();
    }
</script> 
-->
</head>
<body>
<form action="message.jsp" method="post">
    <table align="center">
        <tr>
            <td>
                用户名:
            </td>
            <td>
               <input type="text" name="username">
            </td>
        </tr>
        <tr>
            <td>
                密  码:
            </td>
            <td>
               <input type="password" name="password">
            </td>
        </tr>
        <tr>
            <td>
                确认密码:
            </td>
            <td>
               <input type="password" name="repassword">
            </td>
        </tr>
        <tr>
            <td>
                性别:
            </td>
            <td>
            <input type="radio" name="sex" value="男" checked="checked">男
            <input type="radio" name="sex" value="女" >女
            </td>
        </tr>
        <tr>
            <td>
                验证码:
            </td>
            <td>
            <img alt="" src="ValidateCodeServlet">
            </td>
        </tr>
        <tr>
            <td>
                输入验证码:
            </td>
            <td>
            <input type="text" name="code">
            </td>
        </tr>
        <tr>
            <td>
                邮箱:
            </td>
            <td>
            <input type="text" name="email">
            </td>
        </tr>
        <tr>
            <td colspan="2" align="center">
            <input type="submit" value="提交">
            <input type="reset" value="重置">
            </td>
        </tr>
    </table> 
</form>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值