Home >Java >javaTutorial >JSP implements random verification code

JSP implements random verification code

零到壹度
零到壹度Original
2018-03-24 15:57:071931browse

Verification code technology is the most basic link in protecting website security during the website opening process. It can prevent illegal people from using registration tools or login tools to attack the website (also known as watering), thereby protecting website security. .


Without further ado, let’s go straight to the code:

package com.wgh.random;

public class RanDom {
	private String checknum="";  //生成的验证码
	private int number=0;       //用户输入的位数
	
    public RanDom(){}
	public void setNumber(int number){
		this.number=number;
	}
	public int getNumber(){
		return this.number;
	}
	public void makeChecknum(){
		String sourcenum="0123456789";		//定义获取随机数的源字符串
		String siglenum="";		//保存获取到的单个随机数
		String checknum="";		//获取到的随机数
		int index=0;	//获取随机数的位置
		int i=0;
		while(i<this.number){
			index=((int)(Math.random()*100))%(sourcenum.length()-1);	//随机生成获取随机数的位置
			siglenum=sourcenum.substring(index,index+1);	//获取单个随机数
			checknum+=siglenum;	//连接获取到的随机数
			i++;
		}
		this.checknum=checknum;
	}
	public String getChecknum(){
		return this.checknum;
	}
}

dorandomnum.jsp:

<%@ page contentType="text/html;charset=utf-8"%>
<jsp:useBean id="myrandom" class="com.wgh.random.RanDom"/>
<%
  String strnum=request.getParameter("number");
  if(strnum==null)
	  strnum="0";
  int num=0;
  try{
	  num=Integer.parseInt(strnum);
  }catch(Exception e){num=0;}
  myrandom.setNumber(num);
  myrandom.makeChecknum();
%>
<html>
  <head>
    <title>随机产生指定位数的验证码</title>
    <link rel="stylesheet" type="text/css" href="css/style.css">
  </head>
  <body>
   <center>
       <table style="margin-top:200" width="250" border="1" cellpadding="0" cellspacing="0" bordercolor="black" bordercolorlight="black" bordercolordark="white">
         <tr bgcolor="lightgrey" height="30">
            <td align="center">生成的验证码</td>
         </tr>
         <tr height="50">
            <td align="center">
              验证码的位数:<%=myrandom.getNumber() %>
              <br>
              生成的验证码:<%=myrandom.getChecknum()%>
            </td>
         </tr>
       </table>
       <a href="index.jsp">[返回]</a>
   </center>
  </body>
</html>

index.jsp:

<%@ page contentType="text/html;charset=utf-8"%>
<html>
  <head>
    <title>随机产生指定位数的验证码</title>
    <link rel="stylesheet" type="text/css" href="css/style.css">
  </head>  
  <body>
    <center>
      <form action="dorandomnum.jsp">
        <table style="margin-top:200" width="300" border="1" cellpadding="0" cellspacing="0" bordercolor="black" bordercolorlight="black" bordercolordark="white">
          <tr bgcolor="lightgrey" height="25">
            <td align="center">随机产生指定位数的验证码</td>
          </tr>
          <tr height="50">
            <td align="center">
              输入验证码位数:
              <input type="text" name="number">
              <input type="submit" name="logon" value="生成">
            </td>
          </tr>
        </table>
       </form> 
     </center>
  </body>
</html>

Related recommendations:

JSP implementation Generate random verification code

jsp page dynamically generates verification code

Java Use JSP page to generate random verification code

The above is the detailed content of JSP implements random verification code. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn