问答详情
源自:2-2 验证码的代码实现

验证码出不来

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

}


}


提问者:qq_懵懂的岁月_0 2018-05-10 09:25

个回答

  • 忧伤的民工
    2018-06-04 00:19:21

    WEB.XML里面配置路劲不对

  • qq_雨欲语_03900761
    2018-05-13 16:34:30

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

  • qq_懵懂的岁月_0
    2018-05-10 09:25:54

    https://img1.mukewang.com/5af39f8900019c2110110511.jpghttps://img.mukewang.com/5af39f900001049910110511.jpg

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