电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術文章
文章詳情頁

jsp實現簡單圖片驗證碼功能

瀏覽:216日期:2022-06-07 17:43:02

本文實例為大家分享了jsp實現簡單圖片驗證碼的具體代碼,供大家參考,具體內容如下

一、實現的功能分析

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

二、代碼實現

(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/>? ? ? ?? ? ? ?? ? ? ? 驗證碼: <input type="text" name="checkCode" height="20px " value=""/>? ? ? <img src="CodeServlet"/><span>${error_code}</span><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><% ?? ? //內置對象? ? 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)實現數據查詢頁面: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>編號</td>? ? ? ? ?<td>名稱</td>? ? ? ? ?<td>價格</td>? ? ? ? ?<td>數量</td>? ? ? ? ?<td>日期</td>? ? ? ? ?<td>風格</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)定義一個Author類,用于接收數據庫中的元素。

package com.entity;//用于獲取數據庫中的元素對象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文件。用于登錄檢驗和驗證碼匹配。

//交互層(客戶端和服務器的交互)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?? ??? ?//內置對象request,response?? ??? ?request.setCharacterEncoding("utf-8");?? ??? ??? ??? ?HttpSession session = request.getSession();?? ??? ??? ??? ?//獲取用戶輸入驗證碼?? ??? ?String checkCode = request.getParameter("checkCode");?? ??? ?//獲取session中的驗證碼,也就是CodeServlet中生成的四個字符?? ??? ?String sessionCode = (String)session.getAttribute("sCode");?? ??? ??? ??? ??? ??? ?//驗證碼正確?? ??? ?if(checkCode.equals(sessionCode)) {?? ??? ??? ?//獲取表單數據?? ??? ??? ?String username = request.getParameter("username");?? ??? ??? ?int password = Integer.valueOf(request.getParameter("password"));?? ??? ??? ??? ??? ??? ?//判斷用戶信息是否正確,查詢數據庫獲取用戶信息?? ??? ??? ? AuthorDao ad = new AuthorDao();?? ??? ? ? ? Author obj = ad.check(username, password);?? ??? ? ? ???? ??? ? ? ? //判斷?? ??? ? ? ? if(obj != null) {?? ??? ? ? ??? ???? ??? ? ? ??? ? //重新放入用戶信息?? ??? ? ? ?//?? ? HttpSession session = request.getSession();?? ??? ? ? ??? ? session.setAttribute("authorInfo", obj);?? ??? ? ? ??? ? //設置session的有效期為10秒?? ??? ? ? ??? ? session.setMaxInactiveInterval(10);?? ??? ? ? ??? ???? ??? ? ? ??? ? //頁面轉發?? ??? ? ? ??? ? response.sendRedirect("user.jsp");?? ??? ? ? ? }?? ??? ? ? ? else {?? ??? ? ? ??? ???? ??? ? ? ??? ? //頁面重定向到登錄頁面?? ??? ? ? ??? ? response.sendRedirect("index.jsp");?? ??? ? ? ? }?? ??? ?}?? ??? ?else {?? ??? ??? ?//驗證碼不正確?? ??? ??? ?request.setAttribute("error_code", "驗證碼不匹配");?? ??? ??? ?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)數據庫查詢的交互層: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?? ??? ?//設置編碼方式?? ??? ? request.setCharacterEncoding("utf-8");?? ??? ???? ??? ? //查詢用戶列表?? ??? ? AuthorDao ad = new AuthorDao();?? ??? ? //將Dao層中的結果放入list中?? ??? ? List<Author> list = ad.queryAuthorList();?? ??? ? request.setAttribute("authorList", list);?? ??? ???? ??? ? //請求轉發的方式將查詢結果放入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)定義一個AuthorDao類實現查詢數據庫和檢驗登錄的用戶名和密碼。

//用于檢驗登錄頁面所填入信息是否正確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();?? ??? ??? ??? ?//獲取數據庫連接?? ??? ??? ??? ?Connection conn = db.getConn();?? ??? ??? ??? ?//設置要執行的數據庫語句?? ??? ??? ??? ?String sql = "select *from furnitures where name = ? and id = ?";?? ??? ??? ??? ??? ??? ??? ??? ?PreparedStatement ps = ?conn.prepareStatement(sql);?? ??? ??? ??? ?//設置用戶名和密碼放入sql語句?? ??? ??? ??? ?ps.setString(1, username);?? ??? ??? ??? ?ps.setInt(2, password);?? ??? ??? ??? ??? ??? ??? ??? ?//執行sql查詢語句 , 并將執行結果放入結果集中?? ??? ??? ? ? ?ResultSet rs = ps.executeQuery();?? ??? ??? ??? ??? ??? ??? ? ? ?//用戶名和密碼都正確?? ??? ??? ? ? ?if(rs.next()) {?? ??? ??? ? ? ??? ??? ??? ??? ? ? ??? ?//新創建一個obj 將查詢結果放入?? ??? ??? ? ? ??? ?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;?? ??? ?//定義一個list集合,用于存放查詢結果?? ??? ?List<Author> list = new ArrayList<Author>() ;?? ??? ?try {?? ??? ??? ??? ??? ??? ??? ??? ??? ?DBConnection db = new DBConnection();?? ??? ??? ?//獲取數據庫連接?? ??? ??? ?Connection conn = db.getConn();?? ??? ??? ?//設置數據庫要查詢的語句?? ??? ??? ?String sql = "select *from furnitures ";?? ??? ??? ??? ??? ??? ?PreparedStatement ps = conn.prepareStatement(sql);?? ??? ??? ??? ??? ??? ?//執行數據庫查詢語句,并將查詢結果放入結果集?? ??? ??? ?ResultSet rs = ps.executeQuery();?? ??? ??? ??? ??? ??? ?//利用循環將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)定義一個驗證碼生成CodeServlet類,用于生成驗證碼。

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{?? ??? ?//定義驗證碼的源碼?? ?private static final String str ="abcdefghijklmnopqrstuvwxyaABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";?? ??? ?//定義隨機數?? ?private Random random = new Random();?? ??? ?//隨機生成四個字符?? ?public String getStr()?? ?{?? ??? ?String s = "";?? ??? ?int len = str.length();?? ??? ?for(int i=0;i<4;i++) {?? ??? ??? ?s+=str.charAt(random.nextInt(len));?? ??? ?}?? ??? ?return s;?? ?}?? ??? ?//隨機顏色?? ?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?? ??? ??? ??? ??? ??? ?//生成驗證碼圖片?? ??? ?//畫板?? ??? ?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個字符?? ??? ?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對象繪制圖片到頁面,Servle輸出流進行圖片的輸出?? ??? ?ServletOutputStream sos = resp.getOutputStream();?? ??? ?ImageIO.write(image, "png", sos);?? ??? ??? ??? ?sos.flush();?? ??? ?sos.close();?? ??? ??? ??? ?//驗證碼放入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)創建DBConnectoin.java類用戶獲取數據庫連接。(我用的是mysql)

//獲取數據庫連接package com.dao;import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;public class DBConnection {?? ?private static String username="填入自己的數據庫名";?? ?private static String password="填入自己的數據庫密碼";?? ?private static String driver = "com.mysql.jdbc.Driver";?? ?private static String url="jdbc:mysql://localhost:3306/已經創建數據庫名";?? ??? ?private Connection conn;?? ??? ?static {?? ??? ?try {?? ??? ??? ?//加載驅動,捕獲異常?? ??? ??? ?Class.forName(driver);?? ??? ?} catch (ClassNotFoundException e) {?? ??? ??? ?// TODO Auto-generated catch block?? ??? ??? ?e.printStackTrace();?? ??? ?}?? ?}?? ??? ?public DBConnection () throws SQLException {?? ??? ?//連接數據庫?? ??? ?conn = DriverManager.getConnection(url,username,password);?? ?}?? ??? ?//用于獲取conn?? ?public Connection getConn() {?? ??? ?return conn;?? ?}?? ?public void setConn(Connection conn) {?? ??? ?this.conn = conn;?? ?}??? ?}

三、頁面

(1)登錄頁面

(2)數據查詢頁面

(3)查詢結果顯示頁面

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持。

標簽: JSP
主站蜘蛛池模板: 苏商学院官网 - 江苏地区唯一一家企业家自办的前瞻型、实操型商学院 | 森旺-A级防火板_石英纤维板_不燃抗菌板装饰板_医疗板 | 实木家具_实木家具定制_全屋定制_美式家具_圣蒂斯堡官网 | 定制奶茶纸杯_定制豆浆杯_广东纸杯厂_[绿保佳]一家专业生产纸杯碗的厂家 | 色谱柱-淋洗液罐-巴罗克试剂槽-巴氏吸管-5ml样品瓶-SBS液氮冻存管-上海希言科学仪器有限公司 | 紫外可见光分光度计-紫外分光度计-分光光度仪-屹谱仪器制造(上海)有限公司 | 切铝机-数控切割机-型材切割机-铝型材切割机-【昆山邓氏精密机械有限公司】 | 特种阀门-调节阀门-高温熔盐阀-镍合金截止阀-钛阀门-高温阀门-高性能蝶阀-蒙乃尔合金阀门-福建捷斯特阀门制造有限公司 | 天津拓展_天津团建_天津趣味运动会_天津活动策划公司-天津华天拓展培训中心 | 浇钢砖,流钢砖_厂家价低-淄博恒森耐火材料有限公司 | 南京交通事故律师-专打交通事故的南京律师 | 高尔夫球杆_高尔夫果岭_高尔夫用品-深圳市新高品体育用品有限公司 | 昆山PCB加工_SMT贴片_PCB抄板_线路板焊接加工-昆山腾宸电子科技有限公司 | 【铜排折弯机,钢丝折弯成型机,汽车发泡钢丝折弯机,线材折弯机厂家,线材成型机,铁线折弯机】贝朗折弯机厂家_东莞市贝朗自动化设备有限公司 | 安徽千住锡膏_安徽阿尔法锡膏锡条_安徽唯特偶锡膏_卡夫特胶水-芜湖荣亮电子科技有限公司 | 2025福建平潭岛旅游攻略|蓝眼泪,景点,住宿攻略-趣平潭网 | 校车_校车价格_19座幼儿园校车_幼儿园校车_大鼻子校车 | 深圳标识制作公司-标识标牌厂家-深圳广告标识制作-玟璟广告-深圳市玟璟广告有限公司 | 河南生物显微镜,全自动冰冻切片机-河南荣程联合科技有限公司 | 学校用栓剂模,玻璃瓶轧盖钳,小型安瓿熔封机,实验室安瓿熔封机-长沙中亚制药设备有限公司 | 真空泵维修保养,普发,阿尔卡特,荏原,卡西亚玛,莱宝,爱德华干式螺杆真空泵维修-东莞比其尔真空机电设备有限公司 | 便携式表面粗糙度仪-彩屏硬度计-分体式粗糙度仪-北京凯达科仪科技有限公司 | 电子万能试验机_液压拉力试验机_冲击疲劳试验机_材料试验机厂家-济南众标仪器设备有限公司 | 钢制暖气片散热器_天津钢制暖气片_卡麦罗散热器厂家 | 微信聊天记录恢复_手机短信删除怎么恢复_通讯录恢复软件下载-快易数据恢复 | ET3000双钳形接地电阻测试仪_ZSR10A直流_SXJS-IV智能_SX-9000全自动油介质损耗测试仪-上海康登 | 河南橡胶接头厂家,河南波纹补偿器厂家,河南可曲挠橡胶软连接,河南套筒补偿器厂家-河南正大阀门 | 中视电广_短视频拍摄_短视频推广_短视频代运营_宣传片拍摄_影视广告制作_中视电广 | 污水/卧式/潜水/钻井/矿用/大型/小型/泥浆泵,价格,参数,型号,厂家 - 安平县鼎千泵业制造厂 | 冻干机(冷冻干燥机)_小型|实验型|食品真空冷冻干燥机-松源 | 北京易通慧公司从事北京网站优化,北京网络推广、网站建设一站式服务商-北京网站优化公司 | 定制奶茶纸杯_定制豆浆杯_广东纸杯厂_[绿保佳]一家专业生产纸杯碗的厂家 | 无轨电动平车_轨道平车_蓄电池电动平车★尽在新乡百特智能转运设备有限公司 | TTCMS自助建站_网站建设_自助建站_免费网站_免费建站_天天向上旗下品牌 | 铝合金线槽_铝型材加工_空调挡水板厂家-江阴炜福金属制品有限公司 | 奇酷教育-Python培训|UI培训|WEB大前端培训|Unity3D培训|HTML5培训|人工智能培训|JAVA开发的教育品牌 | 离子色谱自动进样器-青岛艾力析实验科技有限公司 | 517瓜水果特产网|一个专注特产好物的网站 | 浙江清风侠环保设备有限公司| 聚天冬氨酸,亚氨基二琥珀酸四钠,PASP,IDS - 远联化工 | 智成电子深圳tdk一级代理-提供TDK电容电感贴片蜂鸣器磁芯lambda电源代理经销,TDK代理商有哪些TDK一级代理商排名查询。-深圳tdk一级代理 |