系统登录界面的验证码
一、java后臺(tái)生成隨機(jī)驗(yàn)證碼
package com.code;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@SuppressWarnings("serial")
public class Check extends HttpServlet {
public String suijima(){
? char [] str="ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".toCharArray();
? Random rd=new Random();
? String suiji="";
? int temp=0;
? for(int i=0;i<4;i++){
? temp=rd.nextInt(36);
? suiji+=str[temp];
? }
? return suiji;?
}
public void service(HttpServletRequest request, HttpServletResponse response)
? ?throws ServletException, IOException {
? String sjm=suijima();
? HttpSession session=request.getSession();
? session.setAttribute("check",sjm); //在登錄驗(yàn)證的時(shí)候會(huì)首先檢查驗(yàn)證碼是否輸入正確
? ??
? BufferedImage buffimg=new BufferedImage(60,20,BufferedImage.TYPE_INT_RGB);
? Graphics g=buffimg.createGraphics();
? Random rd=new Random();
? int cr,cg,cb;
? cr=rd.nextInt(255);
? cg=rd.nextInt(255);
? cb=rd.nextInt(255);
? Color mycolor=new Color(cr,cg,cb);
? //干擾線
? g.setColor(mycolor);
? for (int i = 0; i < 10; i++){
? ? ? ? ? ? int x1 = rd.nextInt(60);
? ? ? ? ? ? int x2 = rd.nextInt(60);
? ? ? ? ? ? int y1 = rd.nextInt(20);
? ? ? ? ? ? int y2 = rd.nextInt(20);
? ? ? ? ? ? g.drawLine(x1, y1, x2, y2);
? ? ? ? }?
? ?//顯示隨機(jī)碼
? ? ? ? Font myfont=new Font("times new roman",Font.PLAIN,19);
? ? ? ? g.setFont(myfont); ??
? ? ? ? g.setColor(Color.WHITE);
? ? ? ? g.drawString(sjm,5,15);
? ?//將圖像輸出到servlet輸出流中。
? ? ? ? ServletOutputStream sos=response.getOutputStream();
? ? ? ? ImageIO.write(buffimg, "jpeg",sos);
? ? ? ? sos.close();
? ? ? ? g.dispose();
}
}
二、jsp頁面,用jquery驗(yàn)證
<body>
? ?<form method="post">
? ? 用戶名:<input type="text" name="username" id="username"/> <br>?
密 ?碼: <input type="password" name="pwd" id="pwd"/><br>
驗(yàn)證碼:<input type="text" name="yzm" id="yzm"/>
<img id="code" src="http://localhost:8080/javacode/check.do" οnclick="javascript:change()" title="點(diǎn)擊刷新驗(yàn)證碼"><br>
<input type="button" οnclick="check()" value="登錄"/>
<input type="reset" name="reset" value="重置" />
<div id="result"></div>
</form>
? </body>
? <script language="javascript">
function check()
{
var jqueryobj = $("#username"); ? ?
? ?var username = jqueryobj.val();
var jqueryobj1 = $("#pwd"); ? ?
? ?var pwd = jqueryobj1.val();
? ?var jqueryobj2 = $("#yzm"); ? ?
? ?var yzm = jqueryobj2.val();
? ?var object =username+","+pwd+","+yzm;
??
? ?$.get("checkcode.do?object="+object,null,callback); ??
}
function callback(data){
? if(data==1){
? data = "驗(yàn)證碼出錯(cuò),請(qǐng)重新輸入!";
? }else if(data==2){
? data = "驗(yàn)證碼正確!";
? }
? var resultObj = $("#result"); ??
? resultObj.html("<font color=red>"+data+"</font>"); ??
}?
function change(){
? ?var dt = new Date();
? var img=document.getElementById("code");
? img.src="http://localhost:8080/javacode/check.do?dt"+dt; ?//意思是讓圖片重新加載一次 效果類似與直接刷新yanzhengma.jsp有了這個(gè),就能讓圖片重新加載了
? }
</script>
</html>
三、后臺(tái)驗(yàn)證
package com.code;
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;
@SuppressWarnings("serial")
public class Checkcode extends HttpServlet{
public void doPost(HttpServletRequest request,HttpServletResponse ?response) throws IOException, ServletException{
String strYzm=request.getParameter("object"); //用戶輸入的驗(yàn)證碼
String [] str = strYzm.split(",");
? ?String stryz=(String)request.getSession().getAttribute("check"); //servlet中生成的驗(yàn)證碼
? ?int error;
? ?response.setContentType("text/html;charset=GBK");
? ?str[2]=str[2].toUpperCase();
? ? ? ? PrintWriter out=response.getWriter();?
? ? ? ? if(!str[2].equals(stryz)){
? ? ? ? error=1;
? ? ? ? }else{
? ? ? ? error=2;
? ? ? ? }
? ? ? ? out.println(error);
? ?
}
public void doGet(HttpServletRequest request,HttpServletResponse ?response) throws IOException, ServletException{
doPost(request,response);
}
}
這樣就可以完成登錄頁面的驗(yàn)證!
轉(zhuǎn)載于:https://www.cnblogs.com/javaTest/archive/2012/05/10/2589101.html
總結(jié)
以上是生活随笔為你收集整理的系统登录界面的验证码的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 一步一步asp.net_三层构架的学习
- 下一篇: Oracle9在Windows7下的安装