这个页面是我学习前端的时候自己使用JQuery写的轮播图,登录功能的后端判断未实现,现将登陆功能补充完整。
需求:
1,验证码登录#
2,账号密码登录#
3,登录失败在登录部分提示相应的信息#
3,登陆后保存session,实现不关闭浏览器情况下的自动登录#
4,使用验证码防止暴力破解 #
5,点击验证码换验证码 #
6, 点击记住我 session中保存 用户名 。#
7,退出,exit方法(清除浏览器的session记录)#
8,地址栏中直接输入登陆了成功的页面会被拦截#。
注:功能中标有#符号的均为实现了的功能。
截图:
项目结构
![在这里插入图片描述](https://img-blog.csdnimg.cn/20181217105307717.png
这里的代码这里出首页登录的关键代码,具体的css,js代码,和分页的jsp代码请下载此链接:
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!--主页 -->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>主页</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!-- 引入的包css,JQuery -->
<link rel="stylesheet" type="text/css" href="css/index.css">
<script src="./js/jquery-3.3.1.js"></script>
<script src="./js/index.js"></script>
</head>
<body>
<%
//检测错误信息
String err_info = (String) request.getAttribute("err_info");
if (err_info == null) {
err_info = "";
}
%>
<!-- 对登录的session进行判断-->
<%@ include file="/jsp_page/login_succ.jsp"%>
<%
//判断用户名是否为null,如果为null,则将其置为空字符串
if (session_name == null) {
session_name = "";
}
//如果寻找到index==2,说明session中具有用户保存的信息
if (index == 2) {
request.getRequestDispatcher("/succ.jsp").forward(request, response);
}
%>
<!--导入轮播的页面 -->
<%@ include file="/jsp_page/lunbo.jsp"%>
<form action="<%=basePath%>LoginServlet" method="post">
<div id="login">
<img src="./img/tianjiannan.jpg" id="tianjiannan"> 账号: <input type="text" name="username"
value="<%=session_name%>"><br /> 密码: <input type="password" name="password"><br />
验证码 :<input type="text" size="3" name="Verify"><br> <br> <img
src="<%=basePath%>VerifyServlet" id="img_Verify"><br> <br>记住用户名<input
type="checkbox" name="remberme"> <br /> <input type="submit" value="登录" id="but_login">
<a href="#" style="font-size: 10px;">立即注册</a> <br>
<div id="err_info"><%=err_info%></div>
<!-- 导入轮播里的消息模块 -->
<%@include file="/jsp_page/lunbo_info.jsp"%>
</div>
</form>
<div id="div_buttom">123</div>
</body>
</html>
succ.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>登录成功</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<link rel="stylesheet" type="text/css" href="css/index.css">
<script src="./js/jquery-3.3.1.js"></script>
<script src="./js/index.js"></script>
<style type="text/css">
</style>
</head>
<body>
<!-- 登录成功的页面 -->
<%
String username = (String) session.getAttribute("username");
if (username == null) {
response.sendRedirect("index.jsp");
}
%>
<%@ include file="/jsp_page/lunbo.jsp"%>
<div id="login">
<img src="./img/tianjiannan.jpg" id="tianjiannan">
<h1>登录成功</h1>
<h2>欢迎回来</h2><%=username%>
<a href="succ2.jsp">个人中心</a> <a href="exitServlet">退出系统</a>
<%@include file="/jsp_page/lunbo_info.jsp"%>
</div>
<div id="div_buttom">123</div>
</body>
</html>
LoginServlet.java
package com.wlop.wang.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 写死的用户名密码
final String username = "wang";
final String userpassword = "121";
// 判断验证码
String session_verify = (String) request.getSession().getAttribute("verify_val");
String user_input_verify = request.getParameter("Verify");
if (!user_input_verify.equalsIgnoreCase(session_verify)) {
// 发送错误到request域
request.setAttribute("err_info", "验证码错误");
request.getRequestDispatcher("index.jsp").forward(request, response);
return;
}
// 判断账号密码
String userinputname = request.getParameter("username");
String userinputpassword = request.getParameter("password");
String remberme_val = request.getParameter("remberme");
if (userinputname.equals(username) && userinputpassword.equals(userpassword)) {
// 用户的信息存入session(用户名)
request.getSession().setAttribute("username", username);
request.getSession().setAttribute("password", userpassword);
// 存入是否保留用户的信息
request.getSession().setAttribute("remberme", remberme_val);
// 登录成功跳转
request.getRequestDispatcher("/succ.jsp").forward(request, response);
} else {
// 记录错误的err信息到request域
request.setAttribute("err_info", "账号或密码错误!");
// 跳转登录的页面
request.getRequestDispatcher("/index.jsp").forward(request, response);
}
}
}
Verify.java
package com.wlop.wang.servlet;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import utils.VerifyCode;
/*
* 生成验证码,
*/
public class VerifyServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
VerifyCode code = new VerifyCode();// 实例化VerifyCode验证码类
BufferedImage image = code.createImage();// 创建image画布
String verify_val = code.getText();// 获取验证码信息
request.getSession().setAttribute("verify_val", verify_val);// 把验证码的值存入session中用做判断。
VerifyCode.output(image, response.getOutputStream());// 向调用的页面输出image文件流
}
}
VerifyCode.java
package utils;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;
import javax.imageio.ImageIO;
public class VerifyCode {
private int w = 70;
private int h = 35;
private Random r = new Random();
// 定义有那些字体
private String[] fontNames = { "宋体", "华文楷体", "黑体", "微软雅黑", "楷体_GB2312" };
// 定义有那些验证码的随机字符
private String codes = "23456789abcdefghjkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ";
// 生成背景色
private Color bgColor = new Color(250, 250, 250);
// 用于gettext 方法 获得生成的验证码文本
private String text;
// 生成随机颜色
private Color randomColor() {
int red = r.nextInt(150);
int green = r.nextInt(150);
int blue = r.nextInt(150);
return new Color(red, green, blue);
}
// 生成随机字体
private Font randomFont() {
int index = r.nextInt(fontNames.length);
String fontName = fontNames[index];
int style = r.nextInt(4);
int size = r.nextInt(5) + 24;
return new Font(fontName, style, size);
}
// 画干扰线
private void drawLine(BufferedImage image) {
int num = 3;
Graphics2D g2 = (Graphics2D) image.getGraphics();
for (int i = 0; i < num; i++) {
int x1 = r.nextInt(w);
int y1 = r.nextInt(h);
int x2 = r.nextInt(w);
int y2 = r.nextInt(h);
g2.setStroke(new BasicStroke(1.5F));// 不知道
g2.setColor(Color.blue);
g2.drawLine(x1, y1, x2, y2);
}
}
// 得到codes的长度内的随机数 并使用charAt 取得随机数位置上的codes中的字符
private char randomChar() {
int index = r.nextInt(codes.length());
return codes.charAt(index);
}
// 创建一张验证码的图片
public BufferedImage createImage() {
BufferedImage image = new BufferedImage(w, h,
BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = (Graphics2D) image.getGraphics();
StringBuilder sb = new StringBuilder();
// 向图中画四个字符
for (int i = 0; i < 4; i++) {
String s = randomChar() + "";
sb.append(s);
float x = i * 1.0F * w / 4;
g2.setFont(randomFont());
g2.setColor(randomColor());
g2.drawString(s, x, h - 5);
}
this.text = sb.toString();
drawLine(image);
// 返回图片
return image;
}
// 得到验证码的文本 后面是用来和用户输入的验证码 检测用
public String getText() {
return text;
}
// 定义输出的对象和输出的方向
public static void output(BufferedImage bi, OutputStream fos)
throws FileNotFoundException, IOException {
ImageIO.write(bi, "JPEG", fos);
}
}