为了账号安全,请及时绑定邮箱和手机立即绑定

验证码出不来

package servlet;


import java.awt.Color;

import java.awt.Graphics;

import java.awt.image.BufferedImage;

import java.io.IOException;

import java.io.PrintWriter;

import java.util.Random;


import javax.imageio.ImageIO;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;


public class ImageServlet extends HttpServlet {


/**

* Constructor of the object.

*/

public ImageServlet() {

super();

}


/**

* Destruction of the servlet. <br>

*/

public void destroy() {

super.destroy(); // Just puts "destroy" string in log

// Put your code here

}


/**

* The doGet method of the servlet. <br>

*

* This method is called when a form has its tag value method equals to get.

* @param request the request send by the client to the server

* @param response the response send by the server to the client

* @throws ServletException if an error occurred

* @throws IOException if an error occurred

*/

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

BufferedImage bi = new BufferedImage(68, 22, BufferedImage.TYPE_INT_RGB);

Graphics g = bi.getGraphics();

Color c = new Color(200,150,255);

g.setColor(c);

g.fillRect(0, 0, 68, 22);

char[] ch = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".toCharArray();

Random r = new Random();

        int len = ch.length,index;

        StringBuffer sb = new StringBuffer();

        for(int i=0 ;i<4;i++){

        index = r.nextInt(len);

        g.setColor(new Color(r.nextInt(88),r.nextInt(188),r.nextInt(255)));

        g.drawString(ch[index]+"", (i*15)+3, 18);

        sb.append(ch[index]);

       

        }

        request.getSession().setAttribute("piccode", sb.toString());

        ImageIO.write(bi, "JPG", response.getOutputStream());

}


/**

* The doPost method of the servlet. <br>

*

* This method is called when a form has its tag value method equals to post.

* @param request the request send by the client to the server

* @param response the response send by the server to the client

* @throws ServletException if an error occurred

* @throws IOException if an error occurred

*/

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {


doGet(request,response);

}


/**

* Initialization of the servlet. <br>

*

* @throws ServletException if an error occurs

*/

public void init() throws ServletException {

// Put your code here

}


}


正在回答

3 回答

WEB.XML里面配置路劲不对

0 回复 有任何疑惑可以回复我~

你把你包名试试改一下,或者检查一下有没有那里写错了,我也是直接写的servlet,能出来的

0 回复 有任何疑惑可以回复我~

https://img1.sycdn.imooc.com//5af39f8900019c2110110511.jpghttps://img1.sycdn.imooc.com//5af39f900001049910110511.jpg

我是直接生成servlet的  web.xml系统自动配置

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

验证码出不来

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信