一,前端展示html编写:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form name="reg" action="Login" method="post">
用户名:<input name="uname" type="text" > <br>
密码:<input name="pass" type="password"> <br>
验证码:<input type="text" name="check_code">
<img src="Checkservlet">
<input type="submit" value="提交" id="bt">
</form>
</body>
</html>
二,后端对应的Checkservlet
package com.shiyaner;
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;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
@WebServlet("/Checkservlet")
public class CheckServlet extends HttpServlet {
private static int width = 60;
private static int height = 20;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
HttpSession session = req.getSession();
resp.setContentType("image/jpeg");
ServletOutputStream sos = resp.getOutputStream();
resp.setHeader("Pragma","No-cache");
resp.setHeader("Cache-Control", "no-cache");
resp.setDateHeader("Expires", 0);
BufferedImage image =
new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
char[] rands = generateCheckCode();
drawBackground(g);
drawRands(g,rands);
g.dispose();;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ImageIO.write(image,"JPEG",bos);
byte[] buf = bos.toByteArray();
resp.setContentLength(buf.length);
sos.write(buf);
bos.close();
sos.close();
session.setAttribute("check_code",new String(rands));
}
private char[] generateCheckCode(){
String chars = "0123456789abcdefghijklmnopqrstuvwxyz";
char[] rands = new char[4];
for(int i = 0; i < 4; ++i){
int rand = (int)(Math.random() * 36);
rands[i] = chars.charAt(rand);
}
return rands;
}
private void drawRands(Graphics g,char[] rands){
g.setColor(Color.BLACK);
g.setFont(new Font(null,Font.ITALIC | Font.BOLD, 18));
g.drawString("" + rands[0],1,17);
g.drawString("" + rands[1],16,15);
g.drawString("" + rands[2],31,18);
g.drawString("" + rands[3],46,16);
System.out.println(rands);
}
private void drawBackground(Graphics g){
g.setColor(new Color(0xDCDCDC));
g.fillRect(0,0,width,height);
for(int i = 0; i < 120; i++){
int x = (int) (Math.random() * width);
int y = (int) (Math.random() * height);
int red = (int) (Math.random() * 255);
int green = (int) (Math.random() * 255);
int blue = (int) (Math.random() * 255);
g.setColor(new Color(red,green,blue));
g.drawOval(x,y,1,0);
}
}
}
三,表单提交给Login处理,对应的Servlet类
package com.shiyaner;
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;
import java.io.PrintWriter;
@WebServlet("/Login")
public class Login extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset=utf-8");
String username = req.getParameter("uname");
String password = req.getParameter("pass");
String checkCode = req.getParameter("check_code");
String saveCode = (String)req.getSession().getAttribute("check_code");
PrintWriter pw = resp.getWriter();
if("itcast".equals(username) && ("123".equals(password))
&& checkCode.equals(saveCode)){
User user = new User();
user.setUname(username);
user.setPass(password);
req.getSession().setAttribute("user", user);
resp.sendRedirect("IndexServlet");
}else if(checkCode.equals(saveCode)){
pw.write("用户名或密码错误,登录失败");
}else {
pw.write("验证码错误");
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req,resp);
}
}
最终效果如下: