验证码校验

package cookie;


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.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 CheckcodServlet extends HttpServlet {


/**

*/
private static final long serialVersionUID = 1L;


/**
* Constructor of the object.
*/
public CheckcodServlet() {
super();
}


/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}


/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.

* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {


/**
* 在内存中生成图片,没有设置背景颜色,画填充矩形,并且和纸大小相同,矩形有颜色
* 获取笔的对象,设置颜色
* 先准备好数据,随机生成4个字符,把字符画到纸上
* 画干扰线
* 把内存中的图片输出到客户端上
*/

int width = 120;
int height = 30;

//在内存中生成图片
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

//获取画笔的对象
Graphics2D g = (Graphics2D) image.getGraphics();
//设置颜色
g.setColor(Color.GRAY);
//画填充矩形
g.fillRect(0, 0, width, height);
//设置颜色
g.setColor(Color.BLUE);
//画边框
g.drawRect(0, 0, width-1, height-1);
//准备数据,随机获取4个字符
String words = "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM789561230";

//设置颜色
g.setColor(Color.YELLOW);
//设置字体
g.setFont(new Font("隶书",Font.BOLD,20));

StringBuffer sb = new StringBuffer();
Random random = new Random();


int x = 20;
int y = 20;
for(int i = 0; i < 4; i++){
/**
* Theta弧度
* 弧度 = 角度*Math.PI/180
* 获取正负30之间的角度
*/
int jiaodu = random.nextInt(60)-30;
double hudu = jiaodu*Math.PI/180;
g.rotate(hudu, x, y);
//获取下标
int index = random.nextInt(words.length());

char ch = words.charAt(index);

//把ch装起来,存入到Session中
sb.append(ch);
//写字符串
g.drawString(""+ch, x, y);
//返回到0位置转
g.rotate(-hudu, x, y);
x = x+20;
}

//存入到session中
request.getSession().setAttribute("code", sb.toString());

//设置颜色
g.setColor(Color.GREEN);
int x1, y1, x2, y2;
for(int i = 0; i < 4; i++){
//画干扰线
x1 = random.nextInt(width);
y1 = random.nextInt(height);
x2 = random.nextInt(width);
y2 = random.nextInt(height);
g.drawLine(x1, y1, x2, y2);
}


//把内存中的图片输出到浏览器端
ImageIO.write(image, "jpg", response.getOutputStream());






}


/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.

* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {


doGet(request,response);
}


/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}


}



package 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;


public class LoginServlet extends HttpServlet {


/**

*/
private static final long serialVersionUID = 1L;


/**
* Constructor of the object.
*/
public LoginServlet() {
super();
}


/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}


/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.

* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

request.setCharacterEncoding("UTF-8");
//获取session中验证码
String code1 = (String) request.getSession().getAttribute("code");

//获取表单中的验证码
String code2 = request.getParameter("code");

//是否相同
if(code2 != null && code1.equals(code2)){
response.getWriter().write("success");
}else{
request.setAttribute("msg", "验证错误");
request.getRequestDispatcher("/session/login.jsp").forward(request, response);
}
//如果不同返回信息


}


/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.

* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {






doGet(request,response);
}


/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}


}



<%@ 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 HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'login.jsp' starting page</title>
    
<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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->


  </head>
  
  <body>
  <form action="/day11/login" method="post">
    <table>
    <tr>
    <td>输入姓名:</td>
    <td><input type="text" username="username"/></td>
    </tr>
    <tr>
    <td>输入密码:</td>
    <td><input type="text" password="password"/></td>
    </tr>
    <tr>
    <td>输入验证码:</td>
    <td><input type="text" name="code"/>
    <img  src="/day11/checkcod">
    <a href="#" οnclick="run()">看不清换一张</a>
    </td>
    </tr>
    <tr>
    <td colspan="2"><input type="submit" value="登录"/></td>
    </tr>
    </table>
    </form>
  </body>
  <script type="text/javascript">
  //看不清,换一张
  function run(){
  //获取图片
  var image = document.getElementById("imgId");
  image.src = "/day11/checkcod?"+new Date().getTime();
  }
  </script>
</html>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值