博客
关于我
强烈建议你试试无所不能的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

你可能感兴趣的文章
Binary Tree Level Order Traversal [LEETCODE]
查看>>
Install/uninstall .deb files
查看>>
如何让自己的代码更加安全?
查看>>
EBS_DBA_问题:关于不小心drop了表,进行恢复
查看>>
MongoDB数据库
查看>>
HDU-1875-畅通工程再续(最小生成树)
查看>>
POJ-1182-食物链(并查集种类)
查看>>
POJ1288 Sly Number(高斯消元 dfs枚举)
查看>>
Toping Kagglers:Bestfitting,目前世界排名第一
查看>>
Missing key(s) in state_dict: Unexpected key(s) in state_dict
查看>>
sqlconnection,sqlcommand,sqldataadapter,sqldatareader,dataset都是做什么用的?
查看>>
mysql字符集说明
查看>>
linux 动态库 静态库 函数覆盖
查看>>
nginx代理天地图做缓存解决跨域问题
查看>>
SQL MAP 注入测试
查看>>
Android开机自启动程序
查看>>
php弱类型
查看>>
vim
查看>>
【转载】MapReduce编程 Intellij Idea配置MapReduce编程环境
查看>>
安装配置管理 之 Fedora 6.0 蓝牙bluebooth传送文件的问题解决方法
查看>>