【web案例】赵雅智_校验验证码

  • RandomImage.java
    1. 保存src
    2. 获取到src,把src写进去,把数字追加到字符缓冲区,把字符缓冲区内容返回给调用者,在方法前加返回值
    3. 此方法有String返回值
    4. 返回值
    5. 调用setRandouNum()方法时,获取验证码内容
    6. 获取session验证码是什么放到session里
  • LoginServlet.java
    1. 获取填写的验证码
    2. 与session里的值比较
    3. 判断验证码是否一致
    4. 设置字符集
  • login,html
    1. 验证码看不清点击图片刷新



主页面:login.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>login.html</title>
	
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">

  </head>
  
  <body>
    <form action="/bookCookie/LoginServlet" method="post">
    用户名:<input type="text" name="username"><br/><br/>
  密码:<input type="password" name="password"><br/><br/>
  验证码<input type="text" name="checkcode"><br>
  <img src="./RandomImage"><br/><br/>
  <input type="submit" value="登录">
    </form>
  </body>
</html>


新建servlet

RandomImage.java

package com.hbsi.session;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.PrintWriter;
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 RandomImage extends HttpServlet {


	public static final int WIDTH = 130;
	public static final int HEIGHT = 30;
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
		Graphics g = image.getGraphics();
		setBackGrount(g);
		setBorder(g);
		setRandomLine(g);
//		5.调用setRandouNum()方法时,获取验证码内容
		String checkcode = setRandomNum(g);
//		6.获取session验证码是什么放到session里
		request.getSession().setAttribute("imgcode", checkcode);

		response.setContentType("image/jpeg");

		response.setHeader("Pragma", "no-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setIntHeader("Expires",-1);
		
		ImageIO.write(image,"JPEG",response.getOutputStream());
	}

	//产生验证码
//	3.此方法有String返回值
	private String setRandomNum(Graphics g) {
		// TODO Auto-generated method stub
		//g.setColor(Color.BLACK);
		g.setFont(new Font("宋体",Font.BOLD,20));
		Random random = new Random();
		
//		1.保存src
		StringBuffer sb = new StringBuffer();
		int x=10;
		for(int i=0;i<4;i++){
			//产生随机数
			String str = String.valueOf(random.nextInt(10));
			
//			2.获取到src,把src写进去,把数字追加到字符缓冲区,把字符缓冲区内容返回给调用者,在方法前加返回值
			sb.append(str);
			g.setColor(new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255)));
			
			int degree = random.nextInt()%30;
			((Graphics2D)g).rotate(degree*Math.PI/180, x, 20);			
			g.drawString(str, x, 20);
			((Graphics2D)g).rotate(-degree*Math.PI/180, x, 20);
			x +=30;
		}
//		4.返回值
		return sb.toString();
	}

	private void setRandomLine(Graphics g) {
		// TODO Auto-generated method stub
		g.setColor(Color.blue);
		Random random = new Random();
		for(int i=0;i<5;i++){
			int x1 = random.nextInt(WIDTH);
			int y1 = random.nextInt(HEIGHT);
			
			int x2 = random.nextInt(WIDTH);
			int y2 = random.nextInt(HEIGHT);
			
			g.drawLine(x1, y1, x2, y2);
		}
	}

	private void setBorder(Graphics g) {
		// TODO Auto-generated method stub
		g.setColor(Color.BLUE);
		g.drawRect(1, 1, WIDTH-2, HEIGHT-2);
	}

	private void setBackGrount(Graphics g) {
		// TODO Auto-generated method stub
		g.setColor(Color.PINK);
		g.fillRect(0, 0, WIDTH, HEIGHT);
		
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

	}

}

新建LoginServlet

package com.hbsi.session;

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

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

import com.hbsi.dao.UserDao;
import com.hbsi.dao.UserDaoImpl;
import com.hbsi.domian.User;

public class LoginServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
//        10.设置字符集
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		
		String username = request.getParameter("username");
		String password = request.getParameter("password");
//		7.获取填写的验证码
		String checkcode = request.getParameter("checkcode");
//		8.与session里的值比较
		String imgcode = (String) request.getSession().getAttribute("imgcode");
			
//		9.判断验证码是否一致
		if(checkcode!=null && imgcode!=null && checkcode.equals(imgcode)){
			//处理登陆请求
			UserDao ud = new UserDaoImpl();
			User user = ud.find(username, password);
				
			if(user!=null){
				request.getSession().setAttribute("user",user);
				response.sendRedirect("/bookCookie/IndexServlet");
			}
			out.print("用户名密码错");
		}else{
			out.print("验证码错误!!!");
		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request, response);
	}

}

验证码看不清点击图片刷新

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>login.html</title>
	
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">

    <script type="text/javascript">
       function change(img){
          img.src = img.src +"?"+ new Date().getTime();
        }
    </script>
    <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->

  </head>
  
  <body>
    <form action="/bookCookie/LoginServlet" method="post">
    用户名:<input type="text" name="username"><br/><br/>
  密码:<input type="password" name="password"><br/><br/>
  验证码<input type="text" name="checkcode"><br>
  <img src="./RandomImage" οnclick="change(this)"><br/><br/>
  <input type="submit" value="登录">
    </form>
  </body>
</html>



点击粉色图片后:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值