前端login.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <span>欢迎登录XXX管理系统</span> <form action='./loginControler' method='post'> 登录名称: <input type='text' name='Name' value='' /><br> 登录密码:<input type='password' name='nPwd' value='' /><br> 验证码:<input type='text' name='loginCode' value='' /> <img src='./captcha' width=80 height=30/> <input type='submit' value='提交' /></form> </body> </html>
后端
LoginController.java
package cn.edu.mju.project2.comtroller; import cn.edu.mju.project2.Util.StrUtil; 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("/loginControler") public class LoginController extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String Name = req.getParameter("Name"); String Pwd = req.getParameter("Pwd"); String loginCode = req.getParameter("loginCode"); String msg = null; String Code = (String) req.getSession().getAttribute("Code"); if(StrUtil.isBank(loginCode)){ msg = "请输入验证码"; }else if (loginCode.equals(Code)){ if("admin".equals(Name) && "admin".equals(Pwd)) { msg = "<font size='20'>hello " + Name + "</font>"; }else{ msg = "<font size='20'>账号或密码错误!</font>"; } }else { msg = "验证码错误 "; } showMsg(resp, msg); } private void showMsg(HttpServletResponse resp, String msg){ resp.setCharacterEncoding("utf-8"); try{ PrintWriter out = resp.getWriter(); out.println("<html>"); out.println("<header>"); out.println("<title>test</title>"); out.println("<meta charset="utf-8">"); out.println("</header"); out.println("<body>"); out.println(msg); out.println("</body>"); out.println("</html>"); out.close(); }catch (Exception ex){ System.out.println(ex.getMessage()); } } }
CaptcheController.java
package cn.edu.mju.project2.comtroller; 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 java.awt.*; import java.awt.image.BufferedImage; import java.io.IOException; import java.util.Random; @WebServlet("/captcha") public class CaptcheController extends HttpServlet { private final int WIDTH = 130; private final int HEIGHT = 50; @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setDateHeader("Expires", 0); resp.setHeader("Cache-Control", "no-cache"); resp.setHeader("Pragma", "no-cache"); BufferedImage img = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB); Graphics g = img.getGraphics(); g.setColor(Color.white); g.fillRect(0,0, WIDTH, HEIGHT); String Code = getRandomString(4); req.getSession().setAttribute("Code",Code); g.setFont(new Font("宋体",Font.BOLD,30)); g.setColor(Color.RED); g.drawString(Code,30,35); g.dispose(); ServletOutputStream out = resp.getOutputStream(); ImageIO.write(img, "PNG", out); try { out.flush(); }catch (Exception ignored){} finally { out.close(); } } //length用户要求产生字符串的长度 public static String getRandomString(int length){ String str="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; Random random=new Random(); StringBuffer sb=new StringBuffer(); for(int i=0;i<length;i++){ int number=random.nextInt(62); sb.append(str.charAt(number)); } return sb.toString(); } }
StrUtil.java
package cn.edu.mju.project2.Util; public class StrUtil { public static boolean isBank(String src){ if(src == null || src.equals("")){ return true; }else { return false; } } }
效果: