cn.apiclub.captcha.servlet.SimpleCaptchaServlet Maven / Gradle / Ivy
The newest version!
package cn.apiclub.captcha.servlet;
import static cn.apiclub.captcha.Captcha.NAME;
import java.awt.Color;
import java.awt.Font;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.apiclub.captcha.Captcha;
import cn.apiclub.captcha.backgrounds.GradiatedBackgroundProducer;
import cn.apiclub.captcha.text.renderer.ColoredEdgesWordRenderer;
/**
* Generates, displays, and stores in session a 200x50 CAPTCHA image with sheared
* black text, a solid dark grey background, and a slightly curved line over the
* text.
*
* @author James Childers
*/
public class SimpleCaptchaServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private static int _width = 200;
private static int _height = 50;
private static final List COLORS = new ArrayList(2);
private static final List FONTS = new ArrayList(3);
static {
COLORS.add(Color.BLACK);
COLORS.add(Color.BLUE);
FONTS.add(new Font("Geneva", Font.ITALIC, 48));
FONTS.add(new Font("Courier", Font.BOLD, 48));
FONTS.add(new Font("Arial", Font.BOLD, 48));
}
@Override
public void init(ServletConfig config) throws ServletException {
super.init(config);
if (getInitParameter("captcha-height") != null) {
_height = Integer.valueOf(getInitParameter("captcha-height"));
}
if (getInitParameter("captcha-width") != null) {
_width = Integer.valueOf(getInitParameter("captcha-width"));
}
}
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
ColoredEdgesWordRenderer wordRenderer = new ColoredEdgesWordRenderer(COLORS, FONTS);
Captcha captcha = new Captcha.Builder(_width, _height).addText(wordRenderer)
.gimp()
.addNoise()
.addBackground(new GradiatedBackgroundProducer())
.build();
CaptchaServletUtil.writeImage(resp, captcha.getImage());
req.getSession().setAttribute(NAME, captcha);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy