aspose file tools*
The moose likes Other Open Source Projects and the fly likes my image tag will not call servlet  please help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Products » Other Open Source Projects
Bookmark "my image tag will not call servlet  please help" Watch "my image tag will not call servlet  please help" New topic
Author

my image tag will not call servlet please help

Ramkumar Subburaj
Ranch Hand

Joined: Sep 07, 2007
Posts: 83
I placed jcaptcha-all.jar and commons jar i am not getting captcha image
please help what is problem?

registation.jsp

<tr>
<td valign="middle">Enter these letters:<br/>
<img src="/jcaptcha" align="middle" alt="Enter the characters appearing in this image" border="1"/></td>
<td><input type="text" name="inCaptchaChars"/>
</td>

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>

ImageCaptchaServlet.java

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 {
System.out.println("hai");
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
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;
}
}


SCJP 1.5, SCWCD 1.4.
Hanging between Web Services and EJB
Chaminda Amarasinghe
Ranch Hand

Joined: May 17, 2006
Posts: 402
<img src="/jcaptcha" align="middle" alt="Enter the characters appearing in this image" border="1"/></td>


Is your context in root? or src should be like src="/mycontext/jcaptcha"

Check that
udyan keshari
Greenhorn

Joined: Apr 08, 2012
Posts: 1

Chaminda Amarasinghe wrote:
<img src="/jcaptcha" align="middle" alt="Enter the characters appearing in this image" border="1"/></td>


Is your context in root? or src should be like src="/mycontext/jcaptcha"

Check that


Thanks A lot .... it resolved my problem .......


Udyan
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: my image tag will not call servlet please help