1.导入jar包
2. 生成验证码的Servlet
package com.whoami.servletProject;
import cn.dsna.util.images.ValidateCode;
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;
@WebServlet(name = "CreateCodeServlet",value = "/createCode")
public class CreateCodeServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ValidateCode code = new ValidateCode(200,30,4,20);
code.write(response.getOutputStream());
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
}
code.getCode();
3.验证效果
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>登录页面</title>
</head>
<body>
<form action="/login" method="post">
username:<input type="text" name="username"/><br/>
password:<input type="password" name="password"/><br/>
code:<input type="text" name="inputVcode"/><img src="/createCode"><br>
<input type="submit" value="login"/>
</form>
</body>
</html>