Java web - CAPTCHA
Код:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package action;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;
/**
*
* @author 1
*/
public class Image extends HttpServlet {
public static final String chars = "123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
public static final String[] fonts = {"Arial","Calibri","Bernard MT Condensed","Algerian"};//,"Harlow Solid Italic" - nu ego nah
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
BufferedImage image = new BufferedImage(200, 50, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics2D = image.createGraphics();
int length = (int)(Math.random()*2+4);
int lines = (int)(Math.random()*8+8);
String capstr = "";
// render to graphics 2d -
// graphics2D.drawString(...), graphics2D.drawLine(...), etc.
for(int i = 0; i < length; i++)
{
capstr += chars.charAt((int)(Math.random()*chars.length()));
}//Font.getFont("Arial")
//fonts[(int)(Math.random()*fonts.length)-1]
graphics2D.setColor(Color.white);
graphics2D.fillRect(0, 0, 200, 50);
Font font = new Font(fonts[(int)(Math.random()*(fonts.length-1))], Font.BOLD, 24);
graphics2D.setFont(font);
graphics2D.setColor(Color.black);
graphics2D.drawString(capstr, (int)(Math.random()*80+40), (int)(Math.random()*30+22));
for(int i = 0; i < lines; i++)
{
graphics2D.drawLine((int)(Math.random()*200), (int)(Math.random()*50), (int)(Math.random()*200), (int)(Math.random()*50));
}
graphics2D.dispose();
response.setContentType("image/jpeg");
OutputStream outputStream = response.getOutputStream();
ImageIO.write(image, "jpeg", outputStream);
outputStream.close();
request.getSession().setAttribute("captchaval", capstr);
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP <code>POST</code> method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package action;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;
/**
*
* @author 1
*/
public class Image extends HttpServlet {
public static final String chars = "123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
public static final String[] fonts = {"Arial","Calibri","Bernard MT Condensed","Algerian"};//,"Harlow Solid Italic" - nu ego nah
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
BufferedImage image = new BufferedImage(200, 50, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics2D = image.createGraphics();
int length = (int)(Math.random()*2+4);
int lines = (int)(Math.random()*8+8);
String capstr = "";
// render to graphics 2d -
// graphics2D.drawString(...), graphics2D.drawLine(...), etc.
for(int i = 0; i < length; i++)
{
capstr += chars.charAt((int)(Math.random()*chars.length()));
}//Font.getFont("Arial")
//fonts[(int)(Math.random()*fonts.length)-1]
graphics2D.setColor(Color.white);
graphics2D.fillRect(0, 0, 200, 50);
Font font = new Font(fonts[(int)(Math.random()*(fonts.length-1))], Font.BOLD, 24);
graphics2D.setFont(font);
graphics2D.setColor(Color.black);
graphics2D.drawString(capstr, (int)(Math.random()*80+40), (int)(Math.random()*30+22));
for(int i = 0; i < lines; i++)
{
graphics2D.drawLine((int)(Math.random()*200), (int)(Math.random()*50), (int)(Math.random()*200), (int)(Math.random()*50));
}
graphics2D.dispose();
response.setContentType("image/jpeg");
OutputStream outputStream = response.getOutputStream();
ImageIO.write(image, "jpeg", outputStream);
outputStream.close();
request.getSession().setAttribute("captchaval", capstr);
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP <code>POST</code> method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
вот он - код каптчи!
и можно не паритьcя c jcaptcha!