*
The moose likes Servlets and the fly likes my captcha font and background are not good Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "my captcha font and background are not good " Watch "my captcha font and background are not good " New topic
Author

my captcha font and background are not good

Ramkumar Subburaj
Ranch Hand

Joined: Sep 07, 2007
Posts: 83
can aybody tell how to change font and background? i got captcha but font and back ground are not good .if any body have source code please give to me.


my source code is

ImageCaptchaServlet.java is

package slj.members.servlet;
import com.octo.captcha.service.CaptchaServiceException;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;


public class ImageCaptchaServlet extends HttpServlet {


public void init(ServletConfig servletConfig) throws ServletException {

super.init(servletConfig);

}
protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {

byte[] captchaChallengeAsJpeg = null;
// the output stream to render the captcha image as jpeg into
ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream();
try {
// get the session id that will identify the generated captcha.
//the same id must be used to validate the response, the session id is a good candidate!
String captchaId = httpServletRequest.getSession().getId();
// call the ImageCaptchaService getChallenge method
BufferedImage challenge =
CaptchaServiceSingleton.getInstance().getImageChallengeForID(captchaId,
httpServletRequest.getLocale());

// a jpeg encoder
JPEGImageEncoder jpegEncoder =
JPEGCodec.createJPEGEncoder(jpegOutputStream);
jpegEncoder.encode(challenge);
} catch (IllegalArgumentException e) {
httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
} catch (CaptchaServiceException e) {
httpServletResponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
return;
}

captchaChallengeAsJpeg = jpegOutputStream.toByteArray();

// flush it in the response
httpServletResponse.setHeader("Cache-Control", "no-store");
httpServletResponse.setHeader("Pragma", "no-cache");
httpServletResponse.setDateHeader("Expires", 0);
httpServletResponse.setContentType("image/jpeg");
ServletOutputStream responseOutputStream =
httpServletResponse.getOutputStream();
responseOutputStream.write(captchaChallengeAsJpeg);
responseOutputStream.flush();
responseOutputStream.close();
}
}


CaptchaServiceSingleton.java is


package slj.members.servlet;
import com.octo.captcha.service.image.ImageCaptchaService;
import com.octo.captcha.service.image.DefaultManageableImageCaptchaService;

public class CaptchaServiceSingleton {

private static ImageCaptchaService instance = new DefaultManageableImageCaptchaService();

public static ImageCaptchaService getInstance(){
return instance;
}
}
<tr>
<td align="left" valign="middle" >Enter these letters:<br/>
<img src="../../jcaptcha" align="middle" alt="Enter the characters appearing in this image" border="1"/>
</td>
<td align="left" valign="middle"><input type="text" name="txtverificationcode" class="textbox2" value="" maxlength="50" ></td>
</tr>


web.xml

<servlet>
<servlet-name>jcaptcha</servlet-name>
<servlet-class>slj.members.servlet.ImageCaptchaServlet</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>


<servlet-mapping>
<servlet-name>jcaptcha</servlet-name>
<url-pattern>/jcaptcha</url-pattern>
</servlet-mapping>


SCJP 1.5, SCWCD 1.4.
Hanging between Web Services and EJB
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

In your singleton (CaptchaServiceSingleton in your case), you define a custom ImageCaptchaEngine of some sort. We extend ListImagecaptchaEngine and provide our own settings. See the API for more options.



Note this isn't the most secure, but is the best our users are able to manage.

Once you have your engine, you plug it into your Engine in a static initialiser:
Ps Se
Greenhorn

Joined: Jan 03, 2011
Posts: 1
public class CaptchaServiceSingleton {
private static ImageCaptchaService instance = new DefaultManageableImageCaptchaService();
public static ImageCaptchaService getInstance(){
instance.setCaptchaEngine(new MyCaptchaEngine());
return instance;
}
}

I tried the above hint. But setCaptchaEngine is not a method of DefaultManageableImageCaptchaService.
Is there any other way to change the font properties etc without using spring config
Can someone help with it.
Appreciate your help!!
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

the API disagrees, what version are you using?
There should be both setCaptchaEngine() and setCaptchaEngineClass()
http://www.docjar.com/docs/api/com/octo/captcha/service/image/DefaultManageableImageCaptchaService.html
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: my captcha font and background are not good
 
Similar Threads
dynamically loop through form parameters in servlet
GET method working but POST method not working
A small web application
my image tag will not call servlet please help
capacha