用servlet生成图片及action 同步的问题
现在用servlet生成了验证图片,页面中用的是 <img>标签,但是我在往action设置值的时候,出现了问题,导向这个页面要用一个 action,然后页面显示图片的时候还要用到一个action,因为不同步,所以在用session,获得生成图片的内容的时候,是上次的那个数值,而不是现在生成的数值。不知道大家明白我的意思没有?这种问题相信大家也遇到过类似的问题吧。
哎,现在我只敢往初级讨论这边发东西了,怕被扣分。
希望大家帮忙解决下。g.drawString("" + rands[1],16,15);
g.drawString("" + rands[2],31,18);
g.drawString("" + rands[3],46,16);
System.out.println(rands);
}
private void drawBackground(Graphics g)
{
//画背景
g.setColor(new Color(0xDCDCDC));
g.fillRect(0, 0, WIDTH, HEIGHT);
//随机产生120个干扰点
for(int i=0; i<120; i++)
{
int x = (int)(Math.random() * WIDTH);
int y = (int)(Math.random() * HEIGHT);
int red = (int)(Math.random() * 255);
int green = (int)(Math.random() * 255);
int blue = (int)(Math.random() * 255);
g.setColor(new Color(red,green,blue));
g.drawOval(x,y,1,0);
}
}
}</pre><pre name='code' class='java'>?<pre name='code' class='java'>import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class LogonFormServlet extends HttpServlet
{
public void service(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
{
response.setContentType("text/html;charset=GB2312");
PrintWriter out = response.getWriter();
HttpSession session = request.getSession(false);
if(session == null)
{
out.println("验证码处理问题!");
return;
}
String savedCode = (String)session.getAttribute("check_code");
if(savedCode == null)
{
out.println("验证码处理问题!");
return;
}
String checkCode = request.getParameter("check_code");
if(!savedCode.equals(checkCode))
{
/*验证码未通过,不从Session中清除原来的验证码,
以便用户可以后退回登录页面继续使用原来的验证码进行登录*/
out.println("验证码无效!");
return;
}
/*验证码检查通过后,从Session中清除原来的验证码,
以防用户后退回登录页面继续使用原来的验证码进行登录*/
session.removeAttribute("check_code");
out.println("验证码通过,服务器正在校验用户名和密码!");
}
}
</pre>?</pre><pre name='code' class='java'><servlet>
<servlet-name>CheckCodeServlet</servlet-name>
<servlet-class>CheckCodeServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>LogonFormServlet</servlet-name>
<servlet-class>LogonFormServlet</servlet-class>
</servlet>
……
<servlet-mapping>
<servlet-name>CheckCodeServlet</servlet-name>
<url-pattern>/servlet/CheckCodeServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>LogonFormServlet</servlet-name>
<url-pattern>/servlet/LogonFormServlet</url-pattern>
</servlet-mapping>
</pre>?
createImageAction
在生成图片之后可以将number model存入session
MM函数通过XMLHttpRequest请求存在session中的number model, 然后通过javascript验证
这个思路利用了AJAXpublic class VerifyCode { public static String sRandCode = ""; public static BufferedImage image; /** 验证码和图片分成2个方法来操作:*/ // 创建图片验证码 public static void createVerifyCode(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException { //....} //输出图片验证码 public static void outPutImage(HttpServletRequest request, HttpServletResponse response) throws Exception{ImageIO.write(image, "JPEG", response.getOutputStream()); }}
Action: (VerifyCode 图片验证码的操作类)
public static String verifyCode="";public class RegisterAction extends ActionSupport implementsModelDriven<Member> { public String execute() throws Exception { //.... } public String input() throws Exception{VerifyCode.createVerifyCode(ServletActionContext.getRequest(),ServletActionContext.getResponse());verifyCode = VerifyCode.sRandCode; } public String createVerifyCode() throws Exception, IOException {VerifyCode.outPutImage(ServletActionContext.getRequest(), ServletActionContext.getResponse());return null; }