1.编写html注册界面(这里没有编写codeError.jsp)
- 重点是验证码添加一个onclick事件,点击更改验证码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script>
function change(obj) {
// 传递一个时间戳 更新servlet访问的地址
obj.src="codeServlet?"+new Date().getTime();
}
</script>
</head>
<body>
<h2>注册页面</h2>
<form action="registerServlet" method="post">
账号<input type="text" name="username"><br>
密码<input type="password" name="password"><br>
昵称<input type="text" name="nickname"><br>
验证码<input type="text" name="code">
<img src="codeServlet" alt="验证码" style="cursor: pointer" onclick="change(this);"><br>
<input type="submit" value="注册">
</form>
</body>
</html>
2.编写验证码CodeSerlet
- 重点是将生成的验证码保存在session中,用于注册界面中与用户输入的校验比对
package com.itheima.code;
import javax.imageio.ImageIO;
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.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
/**
* Created by tps on 2018/7/7.
*/
@WebServlet("/codeServlet")
public class CodeServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// gui 生成图片
// 1 高和宽
int height = 30;
int width = 60;
String data = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
Random random = new Random();
// 2 创建一个图片
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
// 3 获得画板
Graphics g = image.getGraphics();
// 4 填充一个矩形
// * 设置颜色
g.setColor(Color.BLACK);
g.fillRect(0, 0, width, height);
g.setColor(Color.WHITE);
g.fillRect(1, 1, width - 2, height - 2);
// * 设置字体
g.setFont(new Font("宋体", Font.BOLD | Font.ITALIC, 25));
StringBuffer sb = new StringBuffer();
// 5 写随机字
for (int i = 0; i < 4; i++) {
// 设置颜色--随机数
g.setColor(new Color(random.nextInt(255), random.nextInt(255), random.nextInt(255)));
// 获得随机字
int index = random.nextInt(data.length());
String str = data.substring(index, index + 1);
// 写入
g.drawString(str, width / 6 * (i + 1), 20);
sb.append(str);// 获取验证码数据
}
// 验证码保存到session中
request.getSession().setAttribute("code",sb.toString());
// 6 干扰线
for (int i = 0; i < 3; i++) {
// 设置颜色--随机数
g.setColor(new Color(random.nextInt(255), random.nextInt(255), random.nextInt(255)));
// 随机绘制先
g.drawLine(random.nextInt(width), random.nextInt(height), random.nextInt(width), random.nextInt(height));
// 随机点
g.drawOval(random.nextInt(width), random.nextInt(height), 2, 2);
}
// end 将图片响应给浏览器
ImageIO.write(image, "jpg", response.getOutputStream());
}
}
3.编写RegisterServlet注册服务
- 重点是根据属性名称获取储存在session中的验证码用于比对
package com.itheima.code;
import com.itheima.Utils.DruidUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
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.io.IOException;
/**
* description:<校验用户是否可以注册>
* create by Richard on 2018-07-08
*/
@WebServlet(name = "registerServlet", urlPatterns = "/registerServlet")
public class RegisterServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
String username = request.getParameter("username");
String password = request.getParameter("password");
String nickname = request.getParameter("nickname");
//获取用户输入(表单提交)的验证码
String input_code = request.getParameter("code");
//根据属性名称获取储存在session中的验证码
String session_code = (String) request.getSession().getAttribute("code");
//验证码判空
if(input_code!=null){
// 验证码是否一致 如果一致 才进行数据库注册插入操作
if(input_code.equalsIgnoreCase(session_code)){
// 验证码正确 进行数据库注册插入操作。。。
String sql = "insert into users values(null,?,?,?)";
JdbcTemplate jdbcTemplate = new JdbcTemplate(DruidUtils.getDataSource());
try {
jdbcTemplate.update(sql, username, password, nickname);
} catch (DataAccessException e) {
e.printStackTrace();
}
System.out.println(username+" 注册成功 ! ");
}else{
// 验证码错误 携带错误 跳回注册页面
request.setAttribute("code_error","验证码错误");
request.getRequestDispatcher("/codeError.jsp").forward(request,response);
}
}else {
//获取不到用户输入的验证码的话直接报错(偷懒了)
throw new RuntimeException("验证码必须输入");
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
doPost(request, response);
}
}