jsp實(shí)現(xiàn)簡(jiǎn)單圖片驗(yàn)證碼功能

本文實(shí)例為大家分享了jsp實(shí)現(xiàn)簡(jiǎn)單圖片驗(yàn)證碼的具體代碼,供大家參考,具體內(nèi)容如下

一、實(shí)現(xiàn)的功能分析

(1)在登陸頁面加驗(yàn)證碼的功能,起到一定的安全性。在輸入正確的驗(yàn)證碼,用戶名和密碼的情況下,才可以實(shí)現(xiàn)登錄。
(2)實(shí)現(xiàn)查詢數(shù)據(jù)庫的功能。在登陸后的頁面中,顯示用戶名和密碼,并且設(shè)置有一個(gè)超鏈接,實(shí)現(xiàn)查詢數(shù)據(jù)庫的功能。
(3)代碼核心是:隨機(jī)生成驗(yàn)證碼,并且顯示在頁面上。同時(shí)要和輸入框中的輸入驗(yàn)證碼進(jìn)行校驗(yàn)。
(4)主頁面使用img標(biāo)簽的src屬性引入驗(yàn)證頁面的jsp文件。
(5)驗(yàn)證碼的實(shí)現(xiàn)頁面使用bufferedimage類的方法產(chǎn)生圖片。
(6)使用graphics類的各種方法實(shí)現(xiàn)驗(yàn)證碼的構(gòu)成。

二、代碼實(shí)現(xiàn)

(1)登錄頁面:index.jsp文件。

<%@ page language="java" contenttype="text/html; charset=utf-8"
? ? pageencoding="utf-8"%>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>登錄頁面</title>
</head>
<body>
<form action="loginservlet" method="post">
? ? ? ?用戶名:<input name="username" type="text" value=""/><br/><br/>
? ? ? ?密碼:<input name="password" type="password" value=""/><br/><br/>
? ? ? ?
? ? ? ?
? ? ? ? 驗(yàn)證碼: <input type="text" name="checkcode" height="20px " value=""/>
? ? ? <img src="codeservlet"/>${error_code}<br/>
? ? ? ?<input type="submit" value="提交">

</form>
</body>
</html>

(2)登錄后的頁面:user.jsp文件。

<%@ page language="java" contenttype="text/html; charset=utf-8"
? ? pageencoding="utf-8"%>
<%@ ?page import = "com.entity.author"%>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>顯示登錄用戶的用戶名和密碼頁面</title>
</head>
<body>
<% ?
? ? //內(nèi)置對(duì)象
? ? request.setcharacterencoding("utf-8");
? ? //獲取交互層放入session中的obj
? ? author obj = (author)session.getattribute("authorinfo");
? ??
? ? if(obj != null){
? ? ?? ?out.print("<p>用戶名:"+obj.getname()+"</p>");
? ? ?? ?out.print("<p>密碼:"+obj.getid()+"</p>");
? ? }
? ? else{
? ? ?? ?response.sendredirect("index.jsp");
? ? }
%>
<br/>
<a href="authorservlet">用戶信息查詢 </a>
</body>
</html>

(3)實(shí)現(xiàn)數(shù)據(jù)查詢頁面:ueslist.jsp文件。

<%@ page language="java" contenttype="text/html; charset=utf-8"
? ? pageencoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>查詢信息顯示頁面</title>
</head>
<body>
<table border="1">
? <tr>
? ? ? ? ?<td>編號(hào)</td>
? ? ? ? ?<td>名稱</td>
? ? ? ? ?<td>價(jià)格</td>
? ? ? ? ?<td>數(shù)量</td>
? ? ? ? ?<td>日期</td>
? ? ? ? ?<td>風(fēng)格</td>
? </tr>
??
? ?<c:foreach items="${authorlist}" var="author">
? <tr>
? ? <td>${author.id}</td>
? ? <td>${author.name }</td>
? ? <td>${author.price }</td>
? ? <td>${author.num }</td>
? ? <td>${author.dates}</td>
? ? <td>${author.style}</td>
? </tr>
? </c:foreach>
</table>
</body>
</html>

(4)定義一個(gè)author類,用于接收數(shù)據(jù)庫中的元素。

package com.entity;
//用于獲取數(shù)據(jù)庫中的元素對(duì)象
public class author {
?? ?private int id;
?? ?private string name;
?? ?private int price ;
?? ?private int num;
?? ?private string dates;
?? ?private string style;
?? ?public int getid() {
?? ??? ?return id;
?? ?}
?? ?public void setid(int id) {
?? ??? ?this.id = id;
?? ?}
?? ?public string getname() {
?? ??? ?return name;
?? ?}
?? ?public void setname(string name) {
?? ??? ?this.name = name;
?? ?}
?? ?public int getprice() {
?? ??? ?return price;
?? ?}
?? ?public void setprice(int price) {
?? ??? ?this.price = price;
?? ?}
?? ?public int getnum() {
?? ??? ?return num;
?? ?}
?? ?public void setnum(int num) {
?? ??? ?this.num = num;
?? ?}
?? ?public string getdates() {
?? ??? ?return dates;
?? ?}
?? ?public void setdates(string dates) {
?? ??? ?this.dates = dates;
?? ?}
?? ?public string getstyle() {
?? ??? ?return style;
?? ?}
?? ?public void setstyle(string style) {
?? ??? ?this.style = style;
?? ?}

}

(5)登錄頁面的交互層:loginservlet.java文件。用于登錄檢驗(yàn)和驗(yàn)證碼匹配。

//交互層(客戶端和服務(wù)器的交互)
package com.servlet;

import java.io.ioexception;
import java.sql.sqlexception;

import javax.servlet.servletexception;
import javax.servlet.annotation.webservlet;
import javax.servlet.http.httpservlet;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
import javax.servlet.http.httpsession;

import com.dao.authordao;
import com.entity.author;

/**
?* servlet implementation class loginservlet
?*/
@webservlet("/loginservlet")
public class loginservlet extends httpservlet {
?? ?private static final long serialversionuid = 1l;
? ? ? ?
? ? /**
? ? ?* @see httpservlet#httpservlet()
? ? ?*/
? ? public loginservlet() {
? ? ? ? super();
? ? ? ? // todo auto-generated constructor stub
? ? }

?? ?/**
?? ? * @see httpservlet#doget(httpservletrequest request, httpservletresponse response)
?? ? */
?? ?protected void doget(httpservletrequest request, httpservletresponse response) throws ?servletexception, ioexception {
?? ??? ?// todo auto-generated method stub
?? ??? ?//內(nèi)置對(duì)象request,response
?? ??? ?request.setcharacterencoding("utf-8");
?? ??? ?
?? ??? ?httpsession session = request.getsession();
?? ??? ?
?? ??? ?//獲取用戶輸入驗(yàn)證碼
?? ??? ?string checkcode = request.getparameter("checkcode");
?? ??? ?//獲取session中的驗(yàn)證碼,也就是codeservlet中生成的四個(gè)字符
?? ??? ?string sessioncode = (string)session.getattribute("scode");
?? ??? ?
?? ??? ?
?? ??? ?//驗(yàn)證碼正確
?? ??? ?if(checkcode.equals(sessioncode)) {
?? ??? ??? ?//獲取表單數(shù)據(jù)
?? ??? ??? ?string username = request.getparameter("username");
?? ??? ??? ?int password = integer.valueof(request.getparameter("password"));
?? ??? ??? ?
?? ??? ??? ?//判斷用戶信息是否正確,查詢數(shù)據(jù)庫獲取用戶信息
?? ??? ??? ? authordao ad = new authordao();
?? ??? ? ? ? author obj = ad.check(username, password);
?? ??? ? ? ??
?? ??? ? ? ? //判斷
?? ??? ? ? ? if(obj != null) {
?? ??? ? ? ??? ??
?? ??? ? ? ??? ? //重新放入用戶信息
?? ??? ? ? ?//?? ? httpsession session = request.getsession();
?? ??? ? ? ??? ? session.setattribute("authorinfo", obj);
?? ??? ? ? ??? ? //設(shè)置session的有效期為10秒
?? ??? ? ? ??? ? session.setmaxinactiveinterval(10);
?? ??? ? ? ??? ??
?? ??? ? ? ??? ? //頁面轉(zhuǎn)發(fā)
?? ??? ? ? ??? ? response.sendredirect("user.jsp");
?? ??? ? ? ? }
?? ??? ? ? ? else {
?? ??? ? ? ??? ??
?? ??? ? ? ??? ? //頁面重定向到登錄頁面
?? ??? ? ? ??? ? response.sendredirect("index.jsp");
?? ??? ? ? ? }
?? ??? ?}
?? ??? ?else {
?? ??? ??? ?//驗(yàn)證碼不正確
?? ??? ??? ?request.setattribute("error_code", "驗(yàn)證碼不匹配");
?? ??? ??? ?request.getrequestdispatcher("index.jsp").forward(request, response);
?? ??? ?}
?? ?}?? ?
?? ?/**
?? ? * @see httpservlet#dopost(httpservletrequest request, httpservletresponse response)
?? ? */
?? ?protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {
?? ??? ?// todo auto-generated method stub
?? ??? ?doget(request, response);
?? ?}

}

(6)數(shù)據(jù)庫查詢的交互層:authorservlet.java文件。

package com.servlet;

import java.io.ioexception;
import java.util.list;

import javax.servlet.servletexception;
import javax.servlet.annotation.webservlet;
import javax.servlet.http.httpservlet;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;

import com.dao.authordao;
import com.entity.author;

/**
?* servlet implementation class authorservlet
?*/
@webservlet("/authorservlet")
public class authorservlet extends httpservlet {
?? ?private static final long serialversionuid = 1l;
? ? ? ?
? ? /**
? ? ?* @see httpservlet#httpservlet()
? ? ?*/
? ? public authorservlet() {
? ? ? ? super();
? ? ? ? // todo auto-generated constructor stub
? ? }

?? ?/**
?? ? * @see httpservlet#doget(httpservletrequest request, httpservletresponse response)
?? ? */
?? ?protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {
?? ??? ?// todo auto-generated method stub
?? ??? ?//設(shè)置編碼方式
?? ??? ? request.setcharacterencoding("utf-8");
?? ??? ??
?? ??? ? //查詢用戶列表
?? ??? ? authordao ad = new authordao();
?? ??? ? //將dao層中的結(jié)果放入list中
?? ??? ? list<author> list = ad.queryauthorlist();
?? ??? ? request.setattribute("authorlist", list);
?? ??? ??
?? ??? ? //請(qǐng)求轉(zhuǎn)發(fā)的方式將查詢結(jié)果放入request中,再將超鏈接直接訪問authorservlet就將信息顯示出來了。
?? ??? ? request.getrequestdispatcher("uselist.jsp").forward(request, response);
?? ??? ??
?? ??? ??
?? ?}

?? ?/**
?? ? * @see httpservlet#dopost(httpservletrequest request, httpservletresponse response)
?? ? */
?? ?protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {
?? ??? ?// todo auto-generated method stub
?? ??? ?doget(request, response);
?? ?}

}

(7)定義一個(gè)authordao類實(shí)現(xiàn)查詢數(shù)據(jù)庫和檢驗(yàn)登錄的用戶名和密碼。

//用于檢驗(yàn)登錄頁面所填入信息是否正確
package com.dao;

import java.sql.connection;
import java.sql.preparedstatement;
import java.sql.resultset;
import java.sql.sqlexception;
import java.util.arraylist;
import java.util.list;

import com.entity.author;

public class authordao {
?? ?
?? ?public author check(string username ,int password)
?? ?{
?? ??? ?author obj = null ;
?? ??? ?try {
?? ??? ??? ??? ?dbconnection db = new dbconnection();
?? ??? ??? ??? ?//獲取數(shù)據(jù)庫連接
?? ??? ??? ??? ?connection conn = db.getconn();
?? ??? ??? ??? ?//設(shè)置要執(zhí)行的數(shù)據(jù)庫語句
?? ??? ??? ??? ?string sql = "select *from furnitures where name = ? and id = ?";
?? ??? ??? ??? ?
?? ??? ??? ??? ?preparedstatement ps = ?conn.preparestatement(sql);
?? ??? ??? ??? ?//設(shè)置用戶名和密碼放入sql語句
?? ??? ??? ??? ?ps.setstring(1, username);
?? ??? ??? ??? ?ps.setint(2, password);
?? ??? ??? ??? ?
?? ??? ??? ??? ?//執(zhí)行sql查詢語句 , 并將執(zhí)行結(jié)果放入結(jié)果集中
?? ??? ??? ? ? ?resultset rs = ps.executequery();
?? ??? ??? ??? ?
?? ??? ??? ? ? ?//用戶名和密碼都正確
?? ??? ??? ? ? ?if(rs.next()) {
?? ??? ??? ? ? ??? ?
?? ??? ??? ? ? ??? ?//新創(chuàng)建一個(gè)obj 將查詢結(jié)果放入
?? ??? ??? ? ? ??? ?obj = new author();
?? ??? ??? ? ? ??? ?obj.setid(rs.getint(1));
?? ??? ??? ? ? ??? ?obj.setname(rs.getstring(2));
?? ??? ??? ? ? ??? ?obj.setprice(rs.getint(3));
?? ??? ??? ? ? ??? ?obj.setnum(rs.getint(4));
?? ??? ??? ? ? ??? ?obj.setdates(rs.getstring(5));
?? ??? ??? ? ? ??? ?obj.setstyle(rs.getstring(6));
?? ??? ??? ? ? ?}
?? ? ??
?? ??? ?} catch (sqlexception e) {
?? ??? ??? ?// todo auto-generated catch block
?? ??? ??? ?e.printstacktrace();
?? ??? ?}
?? ??? ?
?? ??? ?return obj;
? }
?? ?
?? ?public list<author> queryauthorlist(){
?? ??? ?
?? ??? ?
?? ??? ?author obj = null;
?? ??? ?//定義一個(gè)list集合,用于存放查詢結(jié)果
?? ??? ?list<author> list = new arraylist<author>() ;
?? ??? ?try {
?? ??? ??? ?
?? ??? ??? ?
?? ??? ??? ?dbconnection db = new dbconnection();
?? ??? ??? ?//獲取數(shù)據(jù)庫連接
?? ??? ??? ?connection conn = db.getconn();
?? ??? ??? ?//設(shè)置數(shù)據(jù)庫要查詢的語句
?? ??? ??? ?string sql = "select *from furnitures ";
?? ??? ??? ?
?? ??? ??? ?preparedstatement ps = conn.preparestatement(sql);
?? ??? ??? ?
?? ??? ??? ?//執(zhí)行數(shù)據(jù)庫查詢語句,并將查詢結(jié)果放入結(jié)果集
?? ??? ??? ?resultset rs = ps.executequery();
?? ??? ??? ?
?? ??? ??? ?//利用循環(huán)將obj放入list集合中
?? ??? ??? ?while(rs.next()) {
?? ??? ??? ??? ?obj = new author();
?? ??? ??? ??? ?
?? ??? ??? ??? ?obj.setid(rs.getint(1));
?? ??? ??? ??? ?obj.setname(rs.getnstring(2));
?? ??? ??? ??? ?obj.setprice(rs.getint(3));
?? ??? ??? ??? ?obj.setnum(rs.getint(4));
?? ??? ??? ??? ?obj.setdates(rs.getstring(5));
?? ??? ??? ??? ?obj.setstyle(rs.getstring(6));
?? ??? ??? ??? ?
?? ??? ??? ??? ?//將obj加入到list
?? ??? ??? ??? ?
?? ??? ??? ??? ?list.add(obj);
?? ??? ??? ??? ?
?? ??? ??? ?}
?? ??? ??? ?
?? ??? ??? ?
?? ??? ?} catch (sqlexception e) {
?? ??? ??? ?// todo auto-generated catch block
?? ??? ??? ?e.printstacktrace();
?? ??? ?}
?? ??? ?
?? ??? ?
?? ??? ?return list;
?? ?}
?? ?
}

(8)定義一個(gè)驗(yàn)證碼生成codeservlet類,用于生成驗(yàn)證碼。

package com.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.imageio.imageio;
import javax.servlet.servletexception;
import javax.servlet.servletoutputstream;
import javax.servlet.annotation.webservlet;
import javax.servlet.http.httpservlet;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
import javax.servlet.http.httpsession;

@webservlet("/codeservlet")
public class codeservlet extends httpservlet{
?? ?
?? ?//定義驗(yàn)證碼的源碼
?? ?private static final string str ="abcdefghijklmnopqrstuvwxyaabcdefghijklmnopqrstuvwxyz1234567890";
?? ?
?? ?//定義隨機(jī)數(shù)
?? ?private random random = new random();
?? ?
?? ?//隨機(jī)生成四個(gè)字符
?? ?public string getstr()
?? ?{
?? ??? ?string s = "";
?? ??? ?int len = str.length();
?? ??? ?for(int i=0;i<4;i++) {
?? ??? ??? ?s+=str.charat(random.nextint(len));
?? ??? ?}
?? ??? ?return s;
?? ?}
?? ?
?? ?//隨機(jī)顏色
?? ?public color getcolor() {
?? ??? ?
?? ??? ?int red = random.nextint(256);
?? ??? ?int green = random.nextint(256);
?? ??? ?int blue = random.nextint(256);
?? ??? ?color color = new color(red,green,blue);
?? ??? ?
?? ??? ?return color;
?? ?}
?? ?@override
?? ?protected void doget(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception {
?? ??? ?// todo auto-generated method stub
?? ??? ?
?? ??? ?
?? ??? ?//生成驗(yàn)證碼圖片
?? ??? ?//畫板
?? ??? ?bufferedimage image = new bufferedimage(70,20,bufferedimage.type_int_rgb );
?? ??? ?//畫筆
?? ??? ?graphics pen = ?image.getgraphics();
?? ??? ?//矩形
?? ??? ?pen.fillrect(0, 0, 70, 20);
?? ??? ?//字體
?? ??? ?pen.setfont(new font("微軟雅黑",font.bold,20));
?? ??? ?
?? ??? ?//獲取4個(gè)字符
?? ??? ?string code = getstr();
?? ??? ?
?? ??? ?//繪制圖片
?? ??? ?for(int i=0;i<code.length();i++) {
?? ??? ??? ?pen.setcolor(getcolor());
?? ??? ??? ?pen.drawstring(string.valueof(code.charat(i)), i*15+5, 20);;
?? ??? ?}
?? ??? ?
?? ??? ?//response對(duì)象繪制圖片到頁面,servle輸出流進(jìn)行圖片的輸出
?? ??? ?servletoutputstream sos = resp.getoutputstream();
?? ??? ?imageio.write(image, "png", sos);
?? ??? ?
?? ??? ?sos.flush();
?? ??? ?sos.close();
?? ??? ?
?? ??? ?//驗(yàn)證碼放入session
?? ??? ?httpsession session = req.getsession();
?? ??? ?session.setattribute("scode", code);
?? ??? ?
?? ?}

?? ?@override
?? ?protected void dopost(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception {
?? ??? ?// todo auto-generated method stub
?? ??? ?dopost(req, resp);
?? ?}
}

(9)創(chuàng)建dbconnectoin.java類用戶獲取數(shù)據(jù)庫連接。(我用的是mysql)

//獲取數(shù)據(jù)庫連接
package com.dao;

import java.sql.connection;
import java.sql.drivermanager;
import java.sql.sqlexception;

public class dbconnection {
?? ?private static string username="填入自己的數(shù)據(jù)庫名";
?? ?private static string password="填入自己的數(shù)據(jù)庫密碼";
?? ?private static string driver = "com.mysql.jdbc.driver";
?? ?private static string url="jdbc:mysql://localhost:3306/已經(jīng)創(chuàng)建數(shù)據(jù)庫名";
?? ?
?? ?private connection conn;
?? ?
?? ?static {
?? ??? ?try {
?? ??? ??? ?//加載驅(qū)動(dòng),捕獲異常
?? ??? ??? ?class.forname(driver);
?? ??? ?} catch (classnotfoundexception e) {
?? ??? ??? ?// todo auto-generated catch block
?? ??? ??? ?e.printstacktrace();
?? ??? ?}
?? ?}
?? ?
?? ?public dbconnection () throws sqlexception {
?? ??? ?//連接數(shù)據(jù)庫
?? ??? ?conn = drivermanager.getconnection(url,username,password);
?? ?}
?? ?
?? ?//用于獲取conn
?? ?public connection getconn() {
?? ??? ?return conn;
?? ?}

?? ?public void setconn(connection conn) {
?? ??? ?this.conn = conn;
?? ?}?
?? ?

}

三、頁面

(1)登錄頁面

(2)數(shù)據(jù)查詢頁面

(3)查詢結(jié)果顯示頁面

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持碩編程。

相關(guān)文章
亚洲国产精品第一区二区,久久免费视频77,99V久久综合狠狠综合久久,国产免费久久九九免费视频