注册页面 login.html

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  6. <title>Insert title here</title>
  7. <script type="text/javascript">
  8. function changeImage(img) {
  9. img.src = img.src + "?" + new Date().getTime();
  10. }
  11. </script>
  12.  
  13. </head>
  14. <body>
  15. <form action="/WebTest3/Demo9" method="get">
  16. <pre>
  17. User :<input type="text" name="username"/> <br />
  18. Passwd :<input type="password" name="passwd"/> <br />
  19. CheckCode:<input type="text" name="checkcode"/> <br />
  20. <img src="/WebTest3/Demo8" alt="ChangeOne" onclick="changeImage(this)" style="cursor: hand"/> <br />
  21. Submit<input type="submit" name="submit" value="submit"/>
  22. </pre>
  23. </form>
  24. </body>
  25. </html>

随机中文图片生成Servlet

  1. public class Demo8 extends HttpServlet {
  2. private static final long serialVersionUID = 1L;
  3. private static final int width = 120;
  4. private static final int height = 40;
  5.  
  6. /**
  7. * @see HttpServlet#HttpServlet()
  8. */
  9. public Demo8() {
  10. super();
  11. // TODO Auto-generated constructor stub
  12. }
  13.  
  14. /**
  15. * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
  16. */
  17. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  18. // TODO Auto-generated method stub
  19. BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  20. Graphics g = image.getGraphics();
  21.  
  22. setBackGround(g);
  23. setBorder(g);
  24. drawRandomLine(g);
  25. String random = drawRandomNum((Graphics2D)g);
  26. request.getSession().setAttribute("checkcode", random);
  27. response.setContentType("image/jpeg");
  28. response.setDateHeader("expries", -1);
  29. response.setHeader("Cache-Control", "no-cache");
  30. response.setHeader("Pragma", "no-cache");
  31. ImageIO.write(image, "jpg", response.getOutputStream());
  32. }
  33.  
  34. private String drawRandomNum(Graphics2D g) {
  35. // TODO Auto-generated method stub
  36. //[\u4e00 ~ \u9fa5]
  37. g.setColor(Color.BLUE);
  38. g.setFont(new Font(null, Font.BOLD, 20));
  39. String base = "\u7684\u4e00\u662f\u4e86\u6211\u4e0d\u4eba\u5728\u4ed6\u6709\u8fd9\u4e2a\u4e0a\u4eec\u6765";
  40. int x = 5;
  41. StringBuffer sb = new StringBuffer();
  42. for(int i=0; i<4; i++)
  43. {
  44. String ch = base.charAt(new Random().nextInt(base.length())) + "";
  45. //System.out.println(ch);
  46. sb.append(ch);
  47. int degreen = new Random().nextInt()%30;
  48. g.rotate(degreen*Math.PI/180, x, 25);
  49. g.drawString(ch, x, 25);
  50. g.rotate(-degreen*Math.PI/180, x, 25);
  51. x += 30;
  52. }
  53. return sb.toString();
  54. }
  55.  
  56. private void drawRandomLine(Graphics g) {
  57. // TODO Auto-generated method stub
  58. g.setColor(Color.green);
  59. for(int i=0; i<8; i++)
  60. {
  61. int x1 = new Random().nextInt(width);
  62. int y1 = new Random().nextInt(height);
  63. int x2 = new Random().nextInt(width);
  64. int y2 = new Random().nextInt(height);
  65. g.drawLine(x1, y1, x2, y2);
  66. }
  67.  
  68. }
  69.  
  70. private void setBorder(Graphics g) {
  71. // TODO Auto-generated method stub
  72. g.setColor(Color.blue);
  73. g.drawRect(1, 1, width-2, height-2);
  74. }
  75.  
  76. private void setBackGround(Graphics g) {
  77. // TODO Auto-generated method stub
  78. g.setColor(Color.WHITE);
  79. g.fillRect(0, 0, width, height);
  80. }
  81.  
  82. /**
  83. * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
  84. */
  85. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  86. // TODO Auto-generated method stub
  87. }
  88.  
  89. }
  1. 检验验证码正确性Servlet
  1. public class Demo9 extends HttpServlet {
  2. 	private static final long serialVersionUID = 1L;
  3.     /**
  4.      * @see HttpServlet#HttpServlet()
  5.      */
  6.     public Demo9() {
  7.         super();
  8.         // TODO Auto-generated constructor stub
  9.     }
  10. 	/**
  11. 	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
  12. 	 */
  13. 	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  14. 		// TODO Auto-generated method stub
  15. 		request.setCharacterEncoding("UTF-8");
  16. 		response.setCharacterEncoding("UTF-8");
  17. 		response.setContentType("text/html;charset=UTF-8");
  18. 		PrintWriter out = response.getWriter();
  19. 		String c_checkcode_temp = request.getParameter("checkcode");
  20. 		String c_checkcode = new String(c_checkcode_temp.getBytes("iso8859-1"), "UTF-8"); //防乱码
  21. 		String s_checkcode = (String) request.getSession().getAttribute("checkcode");
  22. 		out.println("c_code: " + c_checkcode + "s_code: " + s_checkcode);
  23. 		if(c_checkcode!=null && s_checkcode!=null && c_checkcode.equals(s_checkcode))
  24. 		{
  25. 			out.println("checkcode is right");
  26. 		}
  27. 		else
  28. 		{
  29. 			out.println("checkcode is wrong");
  30. 		}
  31. 	}
  32. 	/**
  33. 	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
  34. 	 */
  35. 	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  36. 		// TODO Auto-generated method stub
  37. 	}
  38. }
  39.  
  40. 												
  41. JavaWeb -- Session应用实例 -- 随机中文验证码 检验的更多相关文章

      1. PHP生成中文验证码并检测对错实例
      1. PHP生成中文验证码并检测对错实例,中文验证码的例子还是比较少的,今天给大家分享一下,支持自定义中文.字体.背景色等 生成验证码,注意font字体路径要对,否则显示图片不存在 session_star ...

      1. C#生成随机中文汉字验证码的基本原理
      1. 前几天去申请免费QQ号码,突然发现申请表单中的验证码内容换成了中文,这叫真叫我大跌眼镜感到好笑,Moper上的猫儿们都大骂腾讯采用中文验证码.^_^  我不得不佩服腾讯为了防止目前网络上横行的QQ号码 ...

      1. Django登录(含随机生成图片验证码)注册实例
      1. 登录,生成随机图片验证码 一.登录 - 随机生成图片验证码 1.随机生成验证码 Python随机生成图片验证码,需要使用PIL模块,安装方式如下: pip3 install pillow 1)创建图片 ...

      1. JavaWeb学习记录(五)——Servlet随机产生验证码
      1. 随机产生验证码的工具类: import java.awt.Color;import java.awt.Graphics;import java.awt.image.BufferedImage;impo ...

      1. Javaweb Session机制(有待补充)
      1. Javaweb Session机制 一.前言 session,中文经常翻译为会话,其本来的含义是指有始有终的一系列动作/消息,比如打电话是从拿起电话拨号到挂断电话这中间的一系列过程可以称之为一个ses ...

      1. javaweb项目中表单生成的验证码以及校验
      1. 首先先来看一下项目的结构吧,有两个servlet,一个是进行验证码的生成以及存储的,一个是进行校验的,还有一个jsp页面是用来实现form表单的书写和展示: 我们只需要看这三个就行了,其他的自动忽略: ...

      1. JavaWeb Session
      1. 1. Session概述 1.1. 什么是Session Session一般译为会话,是解决Http协议的无状态问题的方案,可以将一次会话中的数据存储在服务器端的内存中,保证在下一次的会话中可以使用. ...

      1. springboot搭建项目,实现Java生成随机图片验证码。
      1. 这篇文章主要介绍了如何通过Java如何生成验证码并验证.验证码的作用我想必大家都知道,话不多说开始实施! 首先创建一个springboot项目以下是项目结构,内有utli工具类.存放生成图片验证码方法 ...

      1. Djaingo 随机生成验证码(PIL)
      1. 基础: https://www.cnblogs.com/wupeiqi/articles/5812291.html 实例: https://www.cnblogs.com/6324TV/p/88112 ...

    1.  
    2. 随机推荐

        1. nginxproxy_pass$host的问题
        1. 今天在配置一个location的时候,希望使用一个变量如$host来指示nginx代理: location /test/ { proxy_pass http://$host; } 如你想不到,这个配置 ...

        1. Truck History - poj 1789 (Prim 算法)
        1.   Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 20884   Accepted: 8075 Description Ad ...

        1. PHP 学习内容
        1. 第一阶段: (PHP+MySQL核心编程) 面向对象编程 MySQL数据库, MySQL的优化细节. HTTP协议,http也是我们web开发的基石.对我们了解PHP底层机制有很大帮助,做到知其然,还 ...

        1. [LeetCode] Remove Duplicates from Sorted Array II [27]
        1. 题目 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...

        1. Hive编程指南》问题
        1. 1.Hive不支持记录级别的更新.插入或删除? 2.sort by order by 的区别? https://blog.csdn.net/jthink_/article/details/3890 ...

        1. Android】开发优化之——调优工具:dump hprof file 查看内存情况,找到内存泄露
        1. 虽说知道一般性的开发android应用须要注意的问题,但是也有水平參差不齐的情况.特别是维护代码,假设内存占用大,内存溢出严重,又怎么解决呢?  --  通过DDMSheap抓出来分析 1.打开DD ...

        1. python3的时间日期处理
        1. 1.python3日期和时间 Python 程序能用很多方式处理日期和时间,转换日期格式是一个常见的功能. Python 提供了一个 time calendar 模块可以用于格式化日期和时间. ...

        1. iOS8 with Swift
        1. Ref:iOS8 Day-by-Day Ref:iOS8-day-by-day source Ref:Let's Swift Ref:Swift 代码库 Ref:iOS Apprentice Thir ...

        1. LeetCode:加油站【134】
        1. LeetCode:加油站[134] 题目描述 在一条环路上有 N 个加油站,其中第 i 个加油站有汽油 gas[i] 升. 你有一辆油箱容量无限的的汽车,从第 i 个加油站开往第 i+1 个加油站需要 ...

        1. LeetCode:删除排序链表中的重复元素【83】
        1. LeetCode:删除排序链表中的重复元素[83] 题目描述 给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次. 示例 1: 输入: 1->1->2 输出: 1->2 示 ...