当前位置: 首页 > 工具软件 > jCore > 使用案例 >

Jcore

公冶嘉
2023-12-01
$(function(){
	new JCore.CheckCode($('#checkCode'),'${base}/CheckCode.svl');
});

<input name="checkCode" type="text" class="input" id="checkCode" size="15"/>
 
@SuppressWarnings("serial")
public class ImageCaptchaServlet extends HttpServlet {
	public static final String CAPTCHA_IMAGE_FORMAT = "jpeg";
	private ImageCaptchaService imageCaptchaService;
	private String beanName = "imageCaptchaService";

	public void init(ServletConfig servletConfig) throws ServletException {
		super.init(servletConfig);
		WebApplicationContext wac = WebApplicationContextUtils
				.getRequiredWebApplicationContext(servletConfig
						.getServletContext());
		imageCaptchaService = (ImageCaptchaService) wac.getBean(beanName,
				ImageCaptchaService.class);
	}

	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 = imageCaptchaService
					.getImageChallengeForID(captchaId, httpServletRequest
							.getLocale());

			// a jpeg encoder
			ImageIO.write(challenge, CAPTCHA_IMAGE_FORMAT, jpegOutputStream);
		} 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();
	}
}
 
<servlet>
		<servlet-name>jcaptcha</servlet-name>
		<servlet-class>com.mobirit.mtmibp.common.checkcode.ImageCaptchaServlet</servlet-class>
	</servlet>



<servlet-mapping>
		<servlet-name>jcaptcha</servlet-name>
		<url-pattern>/CheckCode.svl</url-pattern>
	</servlet-mapping>
 类似资料:

相关阅读

相关文章

相关问答