在JSP頁面中動態(tài)生成圖片驗證碼的方法實例
更新時間:2019年03月30日 09:13:49 作者:HolyCode_
今天小編就為大家分享一篇關(guān)于在JSP頁面中動態(tài)生成圖片驗證碼的方法實例,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
在JSP頁面中動態(tài)生成圖片驗證碼
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ page
import="java.awt.*,java.awt.image.*,com.sun.image.codec.jpeg.*,java.util.*" %>
<%@ taglib http://struts.apache.org/tags-bean">http://struts.apache.org/tags-bean"
prefix="bean" %>
<%@ taglib http://struts.apache.org/tags-html">http://struts.apache.org/tags-html"
prefix="html" %>
<%@ taglib http://struts.apache.org/tags-logic">http://struts.apache.org/tags-logic"
prefix="logic" %>
<%@ taglib http://struts.apache.org/tags-tiles">http://struts.apache.org/tags-tiles"
prefix="tiles" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html lang="true">
<head>
<html:base />
<title>MyJsp.jsp</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css" rel="external nofollow" >
-->
</head>
<body>
<h3>在jsp頁面生成驗證碼</h3>
<hr/>
<%
//out.clear();
//response.setContentType("image/jpeg");//設(shè)置響應(yīng)類型
//response.addHeader("pragma","NO-cache");
//response.addHeader("Cache-Control","no-cache");
//response.addDateHeader("Expries",0);
int width=400, height=30;//圖片的大?。▽捄透撸?
//構(gòu)架畫布,第一個參數(shù)表示畫布的寬,第二個參數(shù)表示畫布的高,第三個參數(shù)的含義有待確定
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();//實例化畫圖對象
//以下設(shè)置背景色
g.setColor(Color.yellow);
Font DeFont=new Font("宋體", Font.ITALIC, 20);
g.setFont(DeFont);
//將已經(jīng)設(shè)置好的背景顏色填充到指定的畫布區(qū)域
g.fillRect(0,0, width, height);
//置字體色
g.setColor(Color.blue);
int x=10,y=10,xl=550,yl=15;
g.drawLine(x,y,x+xl,y+yl);
//在畫布中畫橢圓形,參數(shù)為橢圓的坐標,用于確定橢圓的大小
g.drawOval(0,10,200,10);
//在畫布上輸出文字信息,第一個參數(shù)表示要顯示的文字,第二和第三個參數(shù)表示起始點的X、Y坐標
g.drawString("想要輸出的文字-我是陳杉",70,20);
g.dispose();
ServletOutputStream outStream = response.getOutputStream();
JPEGImageEncoder encoder =JPEGCodec.createJPEGEncoder(outStream);
encoder.encode(image);
outStream.close();
%>
</body>
</html:html>
--將該文件保存為pic.jsp,該文件負責生成圖片!如果要在其他的頁面顯示該圖片只需要寫上
<img src="pic.jsp"/>
僅此一句就ok了,適用于生成各種驗證碼!
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接
相關(guān)文章
JSP由淺入深(5)—— Scriptlets和HTML的混合
JSP由淺入深(5)—— Scriptlets和HTML的混合...2006-10-10
jsp中使用frameset框架 邊框固定不讓更改邊框的大小
有時候可能要對自己布局好的頁面不讓用戶更改邊框的大小,這樣我們可以在frame里面添加noresize="noresize"屬性就可以實現(xiàn)其中的功能2014-07-07

