jsp页面无法取出servlet生成的验证码,求解。
用的是patchca来生成验证码,patchca-0.5.0.jar包已经引入。servlet配置如下:
import java.awt.Color;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
//Q277160299
import org.patchca.color.ColorFactory;
import org.patchca.color.SingleColorFactory;
import org.patchca.filter.predefined.CurvesRippleFilterFactory;
import org.patchca.filter.predefined.DiffuseRippleFilterFactory;
import org.patchca.filter.predefined.DoubleRippleFilterFactory;
import org.patchca.filter.predefined.MarbleRippleFilterFactory;
import org.patchca.filter.predefined.WobbleRippleFilterFactory;
import org.patchca.service.ConfigurableCaptchaService;
import org.patchca.utils.encoder.EncoderHelper;
import org.patchca.word.RandomWordFactory;
public class CaptchaServlet extends HttpServlet {
private static final long serialVersionUID = 3796351198097771007L;
private static ConfigurableCaptchaService cs = null;
private static ColorFactory cf = null;
private static RandomWordFactory wf = null;
private static Random r = new Random();
private static CurvesRippleFilterFactory crff = null;
private static MarbleRippleFilterFactory mrff = null;
private static DoubleRippleFilterFactory drff = null;
private static WobbleRippleFilterFactory wrff = null;
private static DiffuseRippleFilterFactory dirff = null;
@Override
public void init() throws ServletException {
super.init();
cs = new ConfigurableCaptchaService();
cf = new SingleColorFactory(new Color(25, 60, 170));
wf = new RandomWordFactory();
crff = new CurvesRippleFilterFactory(cs.getColorFactory());
drff = new DoubleRippleFilterFactory();
wrff = new WobbleRippleFilterFactory();
dirff = new DiffuseRippleFilterFactory();
mrff = new MarbleRippleFilterFactory();
cs.setWordFactory(wf);
cs.setColorFactory(cf);
cs.setWidth(120);
cs.setHeight(50);
}
@Override
public void destroy() {
wf = null;
cf = null;
cs = null;
super.destroy();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("image/png");
response.setHeader("cache", "no-cache");
wf.setMaxLength(5);
wf.setMinLength(3);
HttpSession session = request.getSession(true);
OutputStream os = response.getOutputStream();
switch (r.nextInt(5)) {
case 0:
cs.setFilterFactory(crff);
break;
case 1:
cs.setFilterFactory(mrff);
break;
case 2:
cs.setFilterFactory(drff);
break;
case 3:
cs.setFilterFactory(wrff);
break;
case 4:
cs.setFilterFactory(dirff);
break;
}
String captcha = EncoderHelper.getChallangeAndWriteImage(cs, "png", os);
session.setAttribute("captcha", captcha);
os.flush();
os.close();
}
}
web.xml配置如下:
<servlet-mapping>
<servlet-name>CaptchaServlet</servlet-name>
<url-pattern>/patchca.png</url-pattern>
</servlet-mapping>
验证码取出页面如下:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<base href="<%=basePath%>">
</base></head>
<body>
<table><tr>
<!-- patchca.png和servlet中的<url-pattern>对应 -->
<td><img src="patchca.png" alt="验证码"style="cursor:pointer;vertical-align:text-bottom;" onclick="this.src=this.src+'?'+Math.random();"></td>
<td valign="top">
<form method="post" action="gojsp"><br>
验证码:<input type="text" name="patchcafield"><br />
<input type="submit" name="submit">
</form></td></tr></table>
</body></html>
现在问题是,我在验证码页面,不能取出验证码图片。从浏览器中进入servlet路径,也是没有反应。
而我的验证码的img路径参考了很多帖子,放了servlet的路径,应该没问题啊。
请教。 --------------------编程问答--------------------
<servlet>
<servlet-name>captchaServlet</servlet-name>
<servlet-class>CaptchaServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>captchaServlet</servlet-name>
<url-pattern>/patchca.png</url-pattern>
</servlet-mapping>
其他都没错,至少在XML配置时候少了。 --------------------编程问答-------------------- 其他都没错,只是在XML配置时候少了。 --------------------编程问答-------------------- 这个我配了,是我忘了粘了。是这部分吧。
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>CaptchaServlet</servlet-name>
<servlet-class>com.test.servlet.CaptchaServlet</servlet-class>
</servlet>
这些都有,可问题就是不能解决。
--------------------编程问答-------------------- 没有人指教一下吗。各位大佬。 --------------------编程问答-------------------- <form action="验证码Servlet"> --------------------编程问答-------------------- 在doPost()调用doGet()方法,<form method="post">,省略action属性,试试 --------------------编程问答-------------------- 试过了,不行。
form action是跳转到另一个检验输入结果正确与否的页面go.jsp,好像不是这里的问题。继续坐等指教。
--------------------编程问答-------------------- 我已经试过了,是可以的,还不行的找我 rjzou@qq.com --------------------编程问答-------------------- 奇怪,我的怎么不行。本地环境,mycelipse,ssh框架,验证码jsp页面文件夹目录在根目录下,与WEB-INF同级。
--------------------编程问答-------------------- 还是没有人解答吗。 --------------------编程问答--------------------
<td><img src="/<%=path %>/patchca.png" alt="验证码"style="cursor:pointer;vertical-align:text-bottom;" onclick="this.src='/<%=path %>/patchca.png'+'?'+Math.random();"></td> --------------------编程问答-------------------- http://download.csdn.net/detail/s478853630/4064638
注释齐全,你都看得懂的 --------------------编程问答-------------------- 全部不行,jsp页面仍然刷不出验证码图片,持续读取,没有结果。也不报错。我觉得各位的配置应该都没有问题,但我的机器就是刷不出来,很费解啊这个事,崩溃。
--------------------编程问答-------------------- 按照上面各位所说的,可能是浏览器兼容问题,换换浏览器测试 --------------------编程问答-------------------- ff ie都试过,不行,难道这个问题真要僵死了吗。 --------------------编程问答-------------------- 可能是验证码servlet的代码有问题,我以前也遇到这问题
--------------------编程问答-------------------- 我找了好几份代码试用,都是一样的问题,而楼上的那位仁兄用我贴的这个代码也成功了,我就搞不明白了。恼死我了,难道上天整我来了。
--------------------编程问答-------------------- 我一行一行的找了一下原因,发现servlet中,跳入这一行的时候,就不往下走了,也没有报错!
Graphics graphics=image.createGraphics() --------------------编程问答-------------------- 是否路径的问题,测试下吧 --------------------编程问答-------------------- 好东西,果断下载一个~~~
补充:Java , Web 开发