首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 服务器 > 应用服务器 >

帮忙看看什么有关问题?

2013-09-28 
帮忙看看什么问题??package cn.com.checkCodeimport java.awt.Colorimport java.awt.Fontimport java.a

帮忙看看什么问题??
package cn.com.checkCode;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ImageServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public static final int WIDTH=120;
public static final int HEIGHT=25;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
BufferedImage image=new BufferedImage(WIDTH,HEIGHT,BufferedImage.TYPE_INT_RGB);
Graphics g=image.getGraphics();
//设置背景色
setBackGround(g);

//设置边框
setBorder(g);

//画干扰线
drawRandomLine(g);

//写随机数
String num=drawRandomNum((Graphics2D)g);
System.out.println(num);
request.getSession().setAttribute("imageCheckCode", num);


response.setContentType("image/jpeg");
response.setDateHeader("expries", -1);
response.setHeader("Cache-Control", "no-control");
response.setHeader("prama", "no-control");
ImageIO.write(image, "jpg", response.getOutputStream());
}

private String drawRandomNum(Graphics2D g) {
g.setColor(Color.PINK);
g.setFont(new Font("宋体",Font.BOLD,20));
int x=10;
StringBuffer sb=new StringBuffer();
for(int i=0;i<4;i++)
{
int degree=new Random().nextInt()%30;
int num=new Random().nextInt(9);
String str=num+"";
sb.append(str);
g.rotate(degree*Math.PI/180, x, 20);
g.drawString(str, x, 20);
g.rotate(-degree*Math.PI/180, x, 20);
x+=30;
}
return sb.toString();
}

private void drawRandomLine(Graphics g) {
g.setColor(Color.blue);
for(int i=0;i<5;i++)
{
int x1=new Random().nextInt(WIDTH-4)+2;
int y1=new Random().nextInt(HEIGHT-4)+2;

int x2=new Random().nextInt(WIDTH-2);
int y2=new Random().nextInt(HEIGHT-2);

g.drawLine(x1, y1, x2, y2);

}

}

private void setBorder(Graphics g) {
g.setColor(Color.GREEN);
g.drawRect(1, 1, WIDTH-2, HEIGHT-2);
}

private void setBackGround(Graphics g) {
g.setColor(Color.gray);
g.fillRect(0, 0, WIDTH, HEIGHT);
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}

}
<!DOCTYPE html>
<html>
  <head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>MyHtml.html</title>
    <script type="text/javascript">
    function changeImag(image)
    {
    image.src=image.src+"?"+new Date().getTime();
    
    }
    </script>
  </head>
  
  <body>
  <form action="/Session/RegisterServlet"  >
   用户名:<input type="text" name="userName"/>
   <br/>
   密码:<input type="password" name="password"/>
   <br/>
   人证码:<input type="text" name="numRandom">
   <img src="/Session/ImageServlet" onclick="changeImag(this)" style="cursor:hand">
   <br/>
   <input type="submit" value="提交"/>
   <input type="reset" value="重置"/>
   </form>
  </body>
</html>

package cn.com.checkCode;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class RegisterServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
       
   
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
request.setCharacterEncoding("UTF-8");
PrintWriter out=response.getWriter();


//处理请求之前校验验证码是否正确
String random=request.getParameter("numRandom");
String s_check=(String)request.getAttribute("imageCheckCode");
System.out.println(s_check);
System.out.println(random);
if(s_check!=null&&random!=null&& random.equals(s_check))
{
out.print("注册成功!");
System.out.println("注册成功");
}
else
{
out.print("注册失败!");
System.out.println("注册失败!");
}

}


protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}

}
可是那个图片上的绘制的随机数传不过去,最下面的那个Servlet里面输出的s_check的值为null

你早说不就好了

设置 request.getSession().setAttribute("imageCheckCode", num);
获取 request.getAttribute("imageCheckCode");

你看2个一致吗

热点排行