【知了堂学习笔记】java web 简单的登录
最近皮皮潇在学习java web,刚接触了简单的东西,所以今天给大家带来一个简单的登录实现。
页面:
页面代码:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> <script type="text/javascript"> function authcode(obj) { obj.src = "lala.do?"+Math.random(); } </script> </head> <body> <form action="index.do" method="post"> <p>请输入账号</p> <input name="logname" class="text"> <p>请输入密码</p> <input name="logpass" class="text"> <p>请输入验证码</p> <img src="lala.do" onclick="authcode(this)"> <br> <input name="usercode" class="text"> <div> <input type="submit" value="登录" > </div> </form> </body> </html>
servlet代码:
package pipixia; import java.awt.image.BufferedImage; import java.io.IOException; 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 javax.servlet.http.HttpSession; import pipixia.AuthCode; @WebServlet("/index.do") public class UserServlet extends HttpServlet { String authcode = AuthCode.getAuthCode();//生成随机数 private static final long serialVersionUID = 1L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { BufferedImage img = AuthCode.getAuthImg(authcode);//随机数放在图片上,生成验证码 ImageIO.write(img, "JPEG", resp.getOutputStream()); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String lognname = request.getParameter("logname"); String logpass = request.getParameter("logpass"); String usercode = request.getParameter("usercode"); HttpSession session = request.getSession(); if ("pipixiao".equals(lognname) && "123456".equals(logpass)) {//判断账号,密码是否正确 if (authcode.equalsIgnoreCase(usercode)) {//判断验证码是否正确 response.sendRedirect("Test1.jsp"); } else { System.out.println("请输入正确的验证码"); response.sendRedirect("Prac.jsp"); } } else { response.sendRedirect("Prac.jsp"); } } }
在页面上可以清除的看到我还设定了验证码,但是我的验证码是经过别人的指导完成的。
当验证码输错或不输时会提醒输入正确的验证码
主要的制作原理:产生随机数,放入一个五个单位的存储空间,形成验证码,再制作一张图片,将验证码放入图片,就形成了上图所示的验证码。
package pipixia; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.image.BufferedImage; import java.util.Random; import javax.servlet.annotation.WebServlet; public class AuthCode { private final static int lenght = 5; //验证码长度 public static final int width = 15; //单个验证码宽度 public static final int height = 30; //单个验证码高度 public static final int gap = 4; //单个验证码之间间隔 public static final int Img_width = lenght * (width + gap); public static final int Img_height = height; //产生验证码 public static String getAuthCode(){ String authcode=""; for(int i=0;i<lenght;i++){ authcode+=(new Random()).nextInt(10); } return authcode; } //制作图片 public static BufferedImage getAuthImg(String authCode){ BufferedImage img = new BufferedImage(Img_width,Img_height, BufferedImage.TYPE_INT_BGR); Graphics g = img.getGraphics(); g.setColor(Color.YELLOW); g.fillRect(0, 0, Img_width, Img_height); g.setColor(Color.BLACK); g.setFont(new Font("宋体", Font.PLAIN,Img_height )); char c; for(int i=0;i<authCode.toCharArray().length;i++){ c=authCode.charAt(i); g.drawString(c + "", i * (width + gap) + gap / 2, Img_height); } Random random = new Random(); for (int i = 0; i < 10; i++) { int x = random.nextInt(Img_width); int y = random.nextInt(Img_height); int x2 = random.nextInt(Img_width); int y2 = random.nextInt(Img_height); g.drawLine(x, y, x + x2, y + y2); } return img; } }
由于做的是登陆过程,所以还得判断账号密码的正确与否,当然为了提高安全性,还加入了过滤起Filter来起到保护安全的作用
Filter代码:
package pipixia; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.annotation.WebFilter; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; @WebFilter("/Test1.jsp") public class ASFilter implements Filter { @Override public void destroy() { } @Override public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) arg0; HttpServletResponse response = (HttpServletResponse) arg1; HttpSession session = request.getSession(); String logname = (String) session.getAttribute("logname");
if (!"".equals(logname) && logname != null) {
arg2.doFilter(request, response);
} else {
System.out.println("已成功拦截");
response.sendRedirect("Prac.jsp");
}
}
@Override
public void init(FilterConfig arg0) throws ServletException {
}
}
Filter能够阻止直接输入必须登录后才能看到的页面的地址被不登陆的人看见,所以必须要拦截。用了Filter后直接输入必须登录后才能看见的页面的地址,会被拦截,加上一句输出语句控制台就会出现下面的效果,并且会返回登录页面。
www.zhiliaotang.com 请关注知了堂,这里有太多好学的程序员,加入我们,一起努力。