为期2周多的实训终于结束,收获满满,在这里,我将与大家分享使用ssm框架如何实现验证码功能!
1首先.肯定是要搭建好ssm框架,前面文章已经写过如何搭建,这里不再啰嗦,
2.新建一个validatecode.jsp文件,里面内容如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.util.Random"%>
<%@ page import="java.io.OutputStream"%>
<%@ page import="java.awt.*"%>
<%@ page import="java.awt.image.BufferedImage"%>
<%@ page import="javax.imageio.ImageIO"%>
<%
int width = 80;
int height = 32;
//create the image
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
// set the background color
g.setColor(new Color(0xDCDCDC));
g.fillRect(0, 0, width, height);
// draw the border
g.setColor(Color.black);
g.drawRect(0, 0, width - 1, height - 1);
// create a random instance to generate the codes
Random rdm = new Random();
String hash1 = Integer.toHexString(rdm.nextInt());
// make some confusion
for (int i = 0; i < 50; i++) {
int x = rdm.nextInt(width);
int y = rdm.nextInt(height);
g.drawOval(x, y, 0, 0);
}
// generate a random code
String capstr = hash1.substring(0, 4);
session.setAttribute("key", capstr);
g.setColor(new Color(0, 100, 0));
g.setFont(new Font("Candara", Font.BOLD, 24));
g.drawString(capstr, 8, 24);
g.dispose();
response.setContentType("image/jpeg");
out.clear();
out = pageContext.pushBody();
OutputStream strm = response.getOutputStream();
ImageIO.write(image, "jpeg", strm);
strm.close();
%>
当然,这些东西也可以写在一个类里面,主要使用java中的画笔画出验证码,不需要加入任何组价包和配置文件即可使用.
此处将验证码封装成了一个键值对形式,如下:
3.然后新建一个index.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
</head>
<body>
<form action="check_Yz.do" method="post">
<input type="text" id="num" name="num"><img
id="loginform:vCode"
src="${pageContext.request.contextPath }/validatecode.jsp"
onclick="javascript:document.getElementById('loginform:vCode').src='${pageContext.request.contextPath }/validatecode.jsp?'+Math.random();" />
<span class="color_1" id="errorMsg"></span>
<input type="submit" value="提交">
</form>
</body>
</html>
如果上一步你的文件命名与我不同,请在此处修改路径即可!
4.后台判断其是否相等:
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class CheckAction implements IAction{
@RequestMapping(value="check_Yz.do")
public String check(HttpServletRequest request, HttpServletResponse response,String num){
HttpSession session = request.getSession();
//String num = request.getParameter("num");
String key = (String) session.getAttribute("key");
System.out.println(num);
System.out.println(key);
if (key.equals(num)) {
System.out.println("输入正确");
} else {
System.out.println("验证码错误");
}
return "index.jsp";
}
}
运行效果如下:
如果输错,后台会打印,验证码错误!
如果输入正确:
后台打印输入正确!
over!
笔者刚开始运营公众号啦,感兴趣的可关注我的公众号,欢迎交流!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/96881.html