在登錄界面中使用圖片驗(yàn)證碼, 對(duì)于現(xiàn)在的web應(yīng)用到處可見(jiàn).
話不多說(shuō), 開(kāi)始寫(xiě)代碼了!
首先, 新建一個(gè)JSP, 表示登錄界面:
login3.jsp
文件:
<%--
User: menglanyingfei
Date: 2018/1/12
Time: 16:16
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>登錄界面</title>
</head>
<body>
<%
// 獲取瀏覽器發(fā)送過(guò)來(lái)的cookie, 獲取用戶(hù)信息
Cookie[] cookies = request.getCookies();
String username = "";
if (cookies != null) {
for (Cookie cookie : cookies) {
if ("username".equals(cookie.getName())) {
username = cookie.getValue();
}
}
}
%>
<font color="red">${requestScope.message}</font>
<form action="/day_1_12/LoginServlet3" method="post">
用戶(hù)名:<input type="text" name="username" value="<%= username%>"><font color="red">${requestScope.error}</font>
<br>
密碼:<input type="password" name="password">
<br>
驗(yàn)證碼:<input type="text" name="image">
<img src="/day_1_12/VerifyCodeServlet">
<input type="button" value="看不清? 換一張." id="btn"><font color="red">${requestScope.imageMess}</font>
<br>
<input type="submit" value="登錄">
</form>
<script type="text/javascript">
document.getElementById("btn").onclick = function () {
// 獲取img元素
// 為了讓瀏覽器發(fā)送請(qǐng)求到servlet, 所以一定要改變src
document.getElementsByTagName("img")[0].src =
"/day_1_12/VerifyCodeServlet?time=" + new Date().getTime();
};
</script>
</body>
</html>
界面就是這樣, 當(dāng)然, 你肯定現(xiàn)在出來(lái)不了這個(gè)效果! 沒(méi)事, 接著往下看吧!
登錄界面.png
然后, 新建一個(gè)Servlet:
VerifyCodeServlet.java
文件, 因?yàn)檫@里配置了@WebServlet注解, 所以無(wú)需在web.xml中配置!
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 java.awt.image.BufferedImage;
import java.io.IOException;
/**
* 用來(lái)生成圖片驗(yàn)證碼
* Created by menglanyingfei on 2018/1/12.
*/
@WebServlet(name = "VerifyCodeServlet", value = "/VerifyCodeServlet")
public class VerifyCodeServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//創(chuàng)建對(duì)象
VerifyCode vc = new VerifyCode();
//獲取圖片對(duì)象
BufferedImage bi = vc.getImage();
//獲得圖片的文本內(nèi)容
String text = vc.getText();
// 將系統(tǒng)生成的文本內(nèi)容保存到session中
request.getSession().setAttribute("text", text);
//向?yàn)g覽器輸出圖片
vc.output(bi, response.getOutputStream());
}
}
在同一包下, 新建一個(gè)Java文件VerifyCode.java(具體的代碼意思, 可以看注釋, 已經(jīng)寫(xiě)得非常清楚了!):
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;
import javax.imageio.ImageIO;
public class VerifyCode {
private int w = 70;
private int h = 35;
private Random r = new Random();
// {"宋體", "華文楷體", "黑體", "華文新魏", "華文隸書(shū)", "微軟雅黑", "楷體_GB2312"}
private String[] fontNames = {"宋體", "華文楷體", "黑體", "微軟雅黑", "楷體_GB2312"};
// 可選字符
private String codes = "0123456789abcdefghjkmnopqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ";
// 背景色
private Color bgColor = new Color(255, 255, 255);
// 驗(yàn)證碼上的文本
private String text ;
// 生成隨機(jī)的顏色
private Color randomColor () {
int red = r.nextInt(150);
int green = r.nextInt(150);
int blue = r.nextInt(150);
return new Color(red, green, blue);
}
// 生成隨機(jī)的字體
private Font randomFont () {
int index = r.nextInt(fontNames.length);
String fontName = fontNames[index];//生成隨機(jī)的字體名稱(chēng)
int style = r.nextInt(4);//生成隨機(jī)的樣式, 0(無(wú)樣式), 1(粗體), 2(斜體), 3(粗體+斜體)
int size = r.nextInt(5) + 24; //生成隨機(jī)字號(hào), 24 ~ 28
return new Font(fontName, style, size);
}
// 畫(huà)干擾線
private void drawLine (BufferedImage image) {
int num = 3;//一共畫(huà)3條
Graphics2D g2 = (Graphics2D)image.getGraphics();
for(int i = 0; i < num; i++) {//生成兩個(gè)點(diǎn)的坐標(biāo)赘那,即4個(gè)值
int x1 = r.nextInt(w);
int y1 = r.nextInt(h);
int x2 = r.nextInt(w);
int y2 = r.nextInt(h);
g2.setStroke(new BasicStroke(1.5F));
g2.setColor(Color.BLUE); //干擾線是藍(lán)色
g2.drawLine(x1, y1, x2, y2);//畫(huà)線
}
}
// 隨機(jī)生成一個(gè)字符
private char randomChar () {
int index = r.nextInt(codes.length());
return codes.charAt(index);
}
// 創(chuàng)建BufferedImage
private BufferedImage createImage () {
BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = (Graphics2D)image.getGraphics();
g2.setColor(this.bgColor);
g2.fillRect(0, 0, w, h);
return image;
}
// 調(diào)用這個(gè)方法得到驗(yàn)證碼
public BufferedImage getImage () {
BufferedImage image = createImage();//創(chuàng)建圖片緩沖區(qū)
Graphics2D g2 = (Graphics2D)image.getGraphics();//得到繪制環(huán)境
StringBuilder sb = new StringBuilder();//用來(lái)裝載生成的驗(yàn)證碼文本
// 向圖片中畫(huà)4個(gè)字符
for(int i = 0; i < 4; i++) {//循環(huán)四次,每次生成一個(gè)字符
String s = randomChar() + "";//隨機(jī)生成一個(gè)字母
sb.append(s); //把字母添加到sb中
float x = i * 1.0F * w / 4; //設(shè)置當(dāng)前字符的x軸坐標(biāo)
g2.setFont(randomFont()); //設(shè)置隨機(jī)字體
g2.setColor(randomColor()); //設(shè)置隨機(jī)顏色
g2.drawString(s, x, h-5); //畫(huà)圖
}
this.text = sb.toString(); //把生成的字符串賦給了this.text
drawLine(image); //添加干擾線
return image;
}
// 返回驗(yàn)證碼圖片上的文本
public String getText () {
return text;
}
// 保存圖片到指定的輸出流
public static void output (BufferedImage image, OutputStream out)
throws IOException {
ImageIO.write(image, "JPEG", out);
}
}
處理登錄邏輯的Servlet(LoginServlet3.java):
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* Created by menglanyingfei on 2018/1/12.
*/
@WebServlet(name = "LoginServlet3", value = "/LoginServlet3")
public class LoginServlet3 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 編碼
request.setCharacterEncoding("utf-8");
// 獲取請(qǐng)求參數(shù)
/*
拿到頁(yè)面?zhèn)鬟^(guò)來(lái)的手動(dòng)輸入的驗(yàn)證碼, 該驗(yàn)證碼要和生成圖片上的
文本驗(yàn)證碼比較, 如果相同, 驗(yàn)證碼輸入成功!
*/
String imageText = request.getParameter("image");
// 圖片的驗(yàn)證碼
String text = (String) request.getSession().getAttribute("text");
if (!text.equalsIgnoreCase(imageText)) {
request.setAttribute("imageMess", "驗(yàn)證碼輸入錯(cuò)誤!");
request.getRequestDispatcher("/jsp/login3.jsp").forward(request, response);
}
// 獲取用戶(hù)名和密碼
String username = request.getParameter("username");
String password = request.getParameter("password");
if ("haha".equals(username) && "123".equals(password)) {
// 將用戶(hù)信息保存到session中
request.getSession().setAttribute("username", username);
// 使用cookie實(shí)現(xiàn)回寫(xiě)用戶(hù)名
Cookie cookie = new Cookie("username", username);
cookie.setMaxAge(60 * 60);
// 通過(guò)響應(yīng)頭發(fā)送cookie
response.addCookie(cookie);
// 重定向登錄成功界面
response.sendRedirect(request.getContextPath() + "/jsp/success2.jsp");
} else {
request.setAttribute("error", "用戶(hù)名或密碼錯(cuò)誤!");
request.getRequestDispatcher("/jsp/login3.jsp").forward(request, response);
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
登錄成功后的界面:(success2.jsp)
<%--
User: menglanyingfei
Date: 2018/1/12
Time: 16:44
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>成功登錄</title>
</head>
<body>
<%
// 獲取用戶(hù)信息
String username = (String) session.getAttribute("username");
if (username == null) {
// 保存錯(cuò)誤信息到request中, 然后轉(zhuǎn)發(fā)到login3.jsp中, 提醒登錄
request.setAttribute("message", "請(qǐng)登錄!");
// 轉(zhuǎn)發(fā)到登錄頁(yè)面
request.getRequestDispatcher("/jsp/login3.jsp").forward(request, response);
}
%>
<h2>歡迎登錄:${sessionScope.username}!!!</h2>
</body>
</html>
最后, 演示登錄:
因?yàn)? 用戶(hù)名haha和密碼123已經(jīng)寫(xiě)死在代碼里了!
登錄.png
成功.png
完整代碼地址
https://github.com/menglanyingfei/Java/tree/master/JavaWebTrain/day_1_12