JSP实现登录功能之添加验证码jsp登陆验证,⽹页登陆验证带验证码校验,登录功能之添加验证码
part_1:专门⽤于⽣成⼀个验证码图⽚的类:VerificationCode.Java
package cn.st;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;
import javax.imageio.ImageIO;
import org.junit.Test;
/**
* @author : Administrator
* @function : 这是⽤来测试随机⽣成验证码图⽚的类;
*/
public class VerificationCode {
/**
* 单元测试,试⼀下能不能⾃动⽣成验证码图⽚
*/
// 这个函数是单元测试时使⽤的,这⾥private⼀下外⾯就调⽤不到了;
/* @Test */
/* public */private void test_fun() {
VerificationCode vc = new VerificationCode();
BufferedImage image = vc.getImage();
try {
// ⽣成验证码图⽚,并保存到指定的路径
VerificationCode.output(image, new FileOutputStream(new File(
".\\image\\vcode_2.jpg")));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
// 将随机⽣成的⽂本内容输出到控制台,⽤于校验
System.out.Text());
}
private int w = 70;// 宽
private int h = 35;// ⾼
private String text;// ⽂本内容(验证码字符串)
private Random r = new Random();
private String[] fontNames = { "宋体", "华⽂楷体", "⿊体", "微软雅⿊", "楷体_GB2312" };
// 随机字符集合中不包括0和o,O,1和l,因为这些不易区分
private String codes = "23456789abcdefghijkmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYXZ";
// 验证码图⽚的背景⾊:⽩⾊
private Color bgColor = new Color(255, 255, 255);
/**
* 返回⼀个验证码图⽚buffer对象:BufferedImage
*/
public BufferedImage getImage() {
BufferedImage image = createImage();
// 获取绘图环境(画笔⼯具)
Graphics2D g2 = (Graphics2D) Graphics();
// sb :⽤来保存验证码字符串⽂本内容
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 4; ++i) {// 随机⽣成4个字符
String s = randomChar() + "";
sb.append(s);
float x = i * 1.0F * w / 4;
g2.setFont(randomFont());
g2.setColor(randomColor());
g2.drawString(s, x, h - 5);
}
< = sb.toString();// 记录验证码⽂本内容
drawLine(image);// 画⼲扰线
return image;
}
/**
* @return 获取验证码⽂本内容
*/
public String getText() {
return text;
}
/**
* @param image
* @param out
* 将⽂本写到指定的输出流。⽐如本测试中FileOutputStream指定的保存路径
*/
public static void output(BufferedImage image, OutputStream out) {
try {
ImageIO.write(image, "jpeg", out);
} catch (IOException e) {
e.printStackTrace();
}
}
private void drawLine(BufferedImage image) {
Graphics2D g2 = (Graphics2D) Graphics();
for (int i = 0; i < 3; ++i) {// 画3条⼲扰线
int x1 = r.nextInt(w);
int y1 = r.nextInt(h);
int x2 = r.nextInt(w);
int y2 = r.nextInt(h);
g2.drawLine(x1, y1, x2, y2);
}
}
private Color randomColor() {
int red = r.nextInt(150);
int green = r.nextInt(150);
int blue = r.nextInt(150);
return new Color(red, green, blue);
}
private Font randomFont() {
int index = r.nextInt(fontNames.length);
String fontName = fontNames[index];
int style = r.nextInt(4);
int size = r.nextInt(5) + 24;
return new Font(fontName, style, size);
}
private char randomChar() {
int index = r.nextInt(codes.length());
return codes.charAt(index);
}
private BufferedImage createImage() {
BufferedImage image = new BufferedImage(w, h,
BufferedImage.TYPE_INT_RGB);
javascript登录注册界面Graphics2D g2 = (Graphics2D) Graphics();
g2.setColor(this.bgColor);
g2.fillRect(0, 0, w, h);
return image;
}
}
part_2:登录界⾯:Login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = ContextPath();
String basePath = Scheme() + "://"
+ ServerName() + ":" + ServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'Login.jsp' starting page</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">
-->
<script type="text/javascript">
function _change_verity_code() {
var imgElem = ElementById("img_src");
//添加⼀个请求参数a是因为,通常浏览器都有缓存,点击换⼀张的时候没反应,所以加⼀个请求参数,获取当前请求时间,可以精确到毫秒,所以每次请求的参数都不同,所以浏览器有缓存也不妨碍; imgElem.src = "/ServletDemoProject/servlet/GetVerificationCodeServlet?a="
+ new Date().getTime();
}
</script>
</head>
<%
String fdbkMsg = (String) Attribute("fdbkMsg");
if (null == fdbkMsg) {
fdbkMsg = "";
}
%>
<%
Boolean logedIn = (Boolean) Attribute("logedIn");
if (null == logedIn) {
logedIn = false;
} else if (logedIn) {
//如果在本次会话已经登陆,直接重定向到success-page-1
response
.
sendRedirect("/ServletDemoProject/LOGIN-DEMO/success-page-1.jsp");
}
%>
<%
String username = "";
Cookie[] cookies = Cookies();
if ((null != cookies) && (cookies.length > 0)) {
for (Cookie c : cookies) {
if ("admin".Value())) {
username = "admin";
break;
}
}
}//end if-condition
%>
<body>
<br>
<div align="center">
请登录:
<br>
<form action="/ServletDemoProject/servlet/LoginVerificationServlet"
method="post">
<div>
⽤户名:
<input type="text" name="username" value="<%=username%>" />
</div>
<div>
密码:
<input type="password" name="password" />
<br>
</div>
<div>
验证码:
<input type="text" name="code_text" size="3" />
<img src="/ServletDemoProject/servlet/GetVerificationCodeServlet"
id="img_src" />
<a href="javascript:_change_verity_code()">换⼀张</a>
<br>
</div>
<div>
<font color='red'><%=fdbkMsg%></font>
<br>
</div>
<div>
<input type="submit" value="提交" />
<br>
</div>
</form>
</div>
</body>
</html>
part_3:处理登录校验的servlet :LoginVerificationServlet.java
package cn.st_1212;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.st.VerificationCode;
public class GetVerificationCodeServlet extends HttpServlet {
private static final long serialVersionUID = -3520994675366100452L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 1.新建⼀个VerificationCode类;
VerificationCode vc = new VerificationCode();
/
/ 2.从VerificationCode类中获取BufferedImage对象;
BufferedImage bufImage = vc.getImage();
// 3.同时获取验证码中的⽂本内容,并放到session域中,⽤于校验;
String code_text = vc.getText();
// 4.将⽣成的图⽚输出到客户端浏览器
VerificationCode.output(bufImage, OutputStream());
}// end method-doGet
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// do same as GET-method :
doGet(request, response);
}// end method-doPost
}
part_4:成功登陆后的提⽰界⾯1:success-page-1.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = ContextPath();
String basePath = Scheme() + "://"
+ ServerName() + ":" + ServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'success-page-1.jsp' starting page</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">
-->
</head>
<%
String username = (String) Attribute("username");
if (null == username) {
//如果username为空值,说明不是通过正常渠道来的,转发到Login页⾯;
request.setAttribute("fdbkMsg", "别想⾛后门进来,赶紧登录!");
request, response);
}
%>
<body>
<br>
<%=username%>已经成功登陆。
<br>
<font>您可以选择浏览:</font>
<br>
<a href="/ServletDemoProject/LOGIN-DEMO/success-page-2.jsp">点这⼉有精彩.</a> <br>
<a href="/ServletDemoProject/LOGIN-DEMO/success-page-2.jsp">点这⼉更精彩.</a>
<a href="/ServletDemoProject/LOGIN-DEMO/success-page-2.jsp">你敢点这⼉吗.</a>
<br />
</body>
</html>
part_5:成功登陆后的提⽰界⾯1:success-page-2.jsp
<%@ page language="java" import="java.util.Date" pageEncoding="UTF-8"%>
<%@ page language="java" import="SimpleDateFormat"%>
<%
String path = ContextPath();
String basePath = Scheme() + "://"
+ ServerName() + ":" + ServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'success-page-2.jsp' starting page</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">
-->
</head>
<%
String username = (String) Attribute("username");
if (null == username) {
request.setAttribute("fdbkMsg", "呵呵嗒,这⾥是你来的地⽅吗?快登陆!");
//转发到登录界⾯:
request, response);
}
SimpleDateFormat sDateFormat = new SimpleDateFormat("a");
Date today = new Date();
String am_pm_str = sDateFormat.format(today);
String am_pm_str_in_chinese = "";
if ("am".equalsIgnoreCase(am_pm_str)) {
am_pm_str_in_chinese = "上午";
} else
am_pm_str_in_chinese = "下午";
// set null;
sDateFormat = null;
today = null;
am_pm_str = null;
%>
<body>
<br />
<font><b><%=username%><%=am_pm_str_in_chinese%>好,能来到页⾯2真不简单.</b>
</font>
</body>
</html>
以上所述是⼩编给⼤家介绍的JSP实现登录功能之添加验证码,希望对⼤家有所帮助,如果⼤家有任何疑问请给我留⾔,⼩编会及时回复⼤家的。在此也⾮常感谢⼤家对⽹站的⽀持!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论