博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
jsp程序中的验证码的成生和处理。
阅读量:6683 次
发布时间:2019-06-25

本文共 3173 字,大约阅读时间需要 10 分钟。

hot3.png

接合网上和书上的实例,这是一个简单的实例。编码用的是UTF-8。各位在处理过程中一定要注意编码问题。

这个是code.jsp的源码:

<%@ page language="java"    import="java.awt.*"    import="java.awt.image.BufferedImage"    import="java.util.*"    import="javax.imageio.ImageIO"    pageEncoding="utf-8" %>    <%     response.setHeader("Cache-Control", "no-cache");     //create image in RAM!    int width=60,height=20;    BufferedImage image=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);    //get graphics    Graphics g=image.getGraphics();    g.setColor(new Color(200,200,200));    g.fillRect(0, 0, width, height);    //create randrom (4 namber)    Random rnd = new Random();    int randNum=rnd.nextInt(8999)+1000;    String randStr=String.valueOf(randNum);    //save in session    session.setAttribute("randStr", randStr);    //display to image    g.setColor(Color.black);    g.setFont(new Font("",Font.PLAIN,20));    g.drawString(randStr, 10, 17);    //100-other    //        //output image to page    ImageIO.write(image, "JPEG", response.getOutputStream());    out.clear();  out=pageContext.popBody();%>

验证码的产生为1000+0-8999的随机值,使用1000+是确保第一位不为0,可以换成其它的汉字编码等其它不变。

它在网页中显示如下。

它在其它网页中调用如下:

 

请输入账号:

请输入密码:

请输入验证码:             //             // 点击更换            

该处理由servlet来处是,也可以由jsp等程序来处理。

下面说一下验证处理过程:
取得提交上来的验证码和session中的写入的验证码进行比较,相同则进行下一步处理,不同则报验证码错误。
下面是servlet处理的源代码:

package servlet;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 javax.servlet.http.HttpSession;public class ValidateServlet extends HttpServlet {		/**	 * The doGet method of the servlet. 
* * 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 { //得到提交的验证码 String code=request.getParameter("code"); //获取session中的验证码 HttpSession session=request.getSession(); String randStr=(String)session.getAttribute("randStr"); response.setCharacterEncoding("utf-8"); PrintWriter out=response.getWriter(); if(!code.equals(randStr)){ out.println("验证码错误"); } else{ out.println("验证码正确!跳转到loginServlet.........."); } } /** * The doPost method of the servlet.
* * * 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 { this.doGet(request, response); }}

转载于:https://my.oschina.net/lenglingx/blog/114043

你可能感兴趣的文章
需求管理工具比较 Doors_Requistie Pro_RDM
查看>>
centos+php+nginx的php.ini无法加载的问题
查看>>
从菜鸟到专家的五步编程语言学习法
查看>>
RequestQueue
查看>>
Android TextView 属性设置
查看>>
html元素分类以及嵌套规则
查看>>
android dpi
查看>>
C语言的预处理、编译、汇编、链接
查看>>
魅族 C++ 微服务框架技术内幕揭秘
查看>>
flask 学习笔记 mvc ,sqlalchemy(insert,update)
查看>>
HTML基础(一)
查看>>
EGOImageView 解析
查看>>
我的友情链接
查看>>
1.解读REST和JAX-RS
查看>>
将RHEL7/centos7系统网卡名称eno16777736改为eth0
查看>>
Nginx域名跳转
查看>>
NTP时间服务器安装
查看>>
Thinkphp3.23 关联模型relation方法不存在解决方法
查看>>
bash脚本编程之一 条件判断及算术运算
查看>>
工作中非常逆天的shell命令
查看>>