JSP產生隨機證碼,並且局部重新整理驗證碼

來源:互聯網
上載者:User

1.前台jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>

    <base href="<%=basePath%>">

    <title>管理員登陸</title>

<script type="text/javascript" language="javascript">

  function change(){

var o = document.getElementById("random");

var timenow = new Date().getTime();

o.src = "<%=path%>/server/ImageValidate?d=" + timenow;

  }

</script>

  </head>

  <body>

        <img width="80px"  height="30px" id="random" src="<%=path %>/server/ImageValidate"/>

        <input type="button" value="換一張圖片" id="label" onclick="change()"></input>

  </body>

</html>

 

2.後台生存隨機碼的servlet

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.servlet.ServletException;

import javax.servlet.ServletOutputStream;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

 

import com.sun.image.codec.jpeg.JPEGCodec;

import com.sun.image.codec.jpeg.JPEGImageEncoder;

 

@SuppressWarnings("serial")

public class ImageValidate extends HttpServlet {

public ImageValidate() {

super();

}

public void destroy() {

super.destroy(); // Just puts "destroy" string in log

}

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

request.setCharacterEncoding("GBK");

response.setCharacterEncoding("GBK");

response.setContentType("image/jpeg");

        response.addHeader("pragma","NO-cache");

        response.addHeader("Cache-Control","no-cache");

        response.addDateHeader("Expries",0);

        int width=80, height=30;

response.setContentType("text/html; charset=GBK");

String chose="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

char temp;//儲存隨機產生的chose元素

Random rand=new Random();

       BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

       Graphics g = image.getGraphics();

       //以下填充背景顏色

       g.setColor(Color.YELLOW);

       g.fillRect(0, 0, width, height);

      //畫邊框

       g.setColor(Color.BLACK);

       Font font;

       g.drawRect(0,0,width-1,height-1);

       StringBuffer sRand = new StringBuffer();

       for(int i=0;i<4;i++){

temp=chose.charAt(rand.nextInt(chose.length()));

sRand.append(temp);

font=new Font("Arial",Font.PLAIN,15 + rand.nextInt(10));//字型

       g.setFont(font);

g.setColor(new Color(rand.nextInt(255),0,rand.nextInt(255)));//驗證碼的顏色

g.drawString(String.valueOf(temp),15*i+5,20+rand.nextInt(10));//位置

}

request.getSession().setAttribute("randomNumber",sRand.toString());//儲存產生的驗證碼值

      //設定字型顏色

       g.dispose();

       ServletOutputStream outStream = response.getOutputStream();

       JPEGImageEncoder encoder =JPEGCodec.createJPEGEncoder(outStream);

       encoder.encode(image);

       outStream.close();

}

public void init() throws ServletException {

}

}

3.xml設定檔<servlet>    <servlet-name>ImageValidate</servlet-name>    <servlet-class>user.ImageValidate</servlet-class>  </servlet>  <servlet-mapping>    <servlet-name>ImageValidate</servlet-name>    <url-pattern>/server/ImageValidate</url-pattern>  </servlet-mapping>
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.