实现原理:
1.在网页上访问URL(HTTP:localhost:8080/Login),服务器获取请求头Get,forword转发到(HTTP:localhost:8080/Login.jsp),在浏览器上显示登录窗口。
2.用户在浏览器填写用户信息,点击submit按钮以Post方式请求服务器,服务器通过request获取到请求体的数据,通过代码实现验证用户登录是否正确。
3.如果登录验证成功,则sendRedirect重定向到(HTTP:localhost:8080/Main),服务器获取请求头Get,forword转发到(HTTP:localhost:8080/Main.jsp),在浏览器上显示登录成功。
4.如果登录验证失败,则sendRedirect重定向到(HTTP:localhost:8080/Login),服务器获取请求头Get,forword转发到(HTTP:localhost:8080/Login.jsp),在浏览器上显示登录窗口。
为了让用户不能够直接在URL直接访问到 .JSP文件,我们可以把.JSP文件放在WEB-INF文件底下。
代码实现:
1.每个服务器端对应一个.jsp
*LoginController.java
protected void doGet(HttpServletRequest request, HttpServletResponse resp) throws ServletException, IOException {//把request和response转发到login.jsp文件request.getRequestDispatcher("/WEB-INF/login.jsp").forward(request,resp);}
*MainController.java
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//把request和response转发到main.jsp文件request.getRequestDispatcher("/WEB-INF/main.jsp").forward(request,response);}
2.在login.jsp文件中敲登录界面,通过标签的src属性填写随机验证码CaptcheController.java(/Charles/Captche)路径地址
<form action="/Charles/Login" method="post"><label for='LoginName'>登录名称:</label><input name='LoginName' placeholder='请输入登录名' id='LoginName'><br><label for='LoginPwd'>登录密码:</label><input type='password' name='LoginPwd' placeholder='请输入密码' value='' id='LoginPwd'><br><label for='verification'>验证码:</label><input name='verification' value='' placeholder='请输入验证码' id='verification'><br><img src="/Charles/Captche" ><a >看不清换一张</a><input type='reset' name='reset' value='取消'><input type='submit' name='submit' value='登录'></form>
新写一个Java Class–CaptcheController.java,Servlet路径为(/Charles/Captche),在里面敲验证码的代码
int width = 100;int height = 50;//1.创建一个对象,在内存中存图片(验证码图片对象)BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);//2.美化图片//2.1 填充背景色Graphics graphics = image.getGraphics(); //画笔对象// graphics.fill//填充// graphics.draw//画graphics.setColor(Color.yellow);//画笔的颜色graphics.fillRect(0,0,width,height);//填充 rect 矩形//2.2 画边框graphics.setColor(Color.blue);graphics.drawRect(0,0,width-1,height-1);String str="QWERTYPADFGHLBNM123456789qwertypadfghjkbnm";//生成随机角标Random random = new Random();String randomnumber="";//2.3 写验证码for (int i = 1; i <= 4; i++) {int nextInt = random.nextInt(str.length());//获取字符char charAt = str.charAt(nextInt);//随机字符randomnumber+=charAt;graphics.drawString(charAt+"",width/5*i,25);}System.out.println(randomnumber);//通过ServletContext和程序的容器(服务器)来通信共享数据ServletContext servletContext = this.getServletContext();servletContext.setAttribute("message",randomnumber);//2.4干扰线graphics.setColor(Color.green);//随机生成坐标点for (int i = 0; i < 10; i++) {int nextWidth = random.nextInt(width);int nextHeight = random.nextInt(height);int nextWidth2 = random.nextInt(width);int nextHeight2 = random.nextInt(height);graphics.drawLine(nextWidth,nextHeight,nextWidth2,nextHeight2);}//3.将图片输出到页面展示ImageIO.write(image,"jpg",response.getOutputStream());
实现了随机验证码的生成,当用户点击submit按钮时,*LoginController.java获取请求体里的数据,进行判断验证,
String loginName=request.getParameter("LoginName");//根据LoginName名称获取用户名参数值String loginPwd=request.getParameter("LoginPwd");//根据LoginPwd名称获取登录密码String verification=request.getParameter("verification");//根据verification名称获取验证码值//通过ServletContext获取数据ServletContext servletContext = this.getServletContext();Object validCode = servletContext.getAttribute("message");//进行判断,如果用户输入的用户名为admin,并且密码为1234,并且验证码为随机生成的验证码,就为登录成功,否则为登录失败if(("admin").equals(loginName) && ("1234").equals(loginPwd)&&validCode.equals(verification)) {//如果登录成功,则重定向到/Charles/Mainresponse.sendRedirect("/Charles/Main");}else {//如果登录失败,则重定向到/Charles/Loginresponse.sendRedirect("/Charles/Login");}}
登录效果
1.访问URL(HTTP://localhost:9090/Charles/Login)
2.输入错误的验证码,重定向到本窗体
3.输入正确的验证码,重定向到新的窗体