需求分析
代码实现
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2019/8/20
Time: 14:41
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<style>
div{
color: red;
}
</style>
<head>
<title>sessionLogin</title>
</head>
<body>
<form action="sessionLogin" method="post">
<table>
<tr>
<td>用户名</td>
<td>
<input type="text" name="username">
</td>
</tr>
<tr>
<td>密码</td>
<td>
<input type="password" name="password">
</td>
</tr>
<tr>
<td>验证码</td>
<td>
<input type="text" name="checkcode">
</td>
</tr>
<tr>
<td colspan="2">
<img id="img" src="checkCodeServletSession">
<a id="a" href="javascript:void(0)">看不清换一张</a>
</td>
</tr>
<tr>
<td>
<input type="submit" value="登陆">
</td>
</tr>
</table>
</form>
<script>
window.onload = function () {
var img = document.getElementById("img");
document.getElementById("a").onclick = function () {
var time = new Date().getTime();
img.src = "checkCodeServletSession?" +time;
}
}
</script>
<div><%=request.getAttribute("error_check")==null?"":request.getAttribute("error_check")%></div>
<div><%=request.getAttribute("error_user")==null?"":request.getAttribute("error_user")%></div>
</body>
</html>
package itcast.web.sessionDemoTest;
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 java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
@WebServlet("/checkCodeServletSession")
public class CheckCodeServletSession extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//创建对象 在内存中生成动态图片
int wight = 100;
int height = 50;
BufferedImage bufferedImage = new BufferedImage(wight, height, BufferedImage.TYPE_INT_RGB);
//美化图片
//获取画笔
Graphics graphics = bufferedImage.getGraphics();
graphics.setColor(Color.pink);
graphics.fillRect(0,0,wight,height);
graphics.setColor(Color.blue);
graphics.drawRect(0,0,wight-1,height-1);
String str="ABCDEFGHIGKLMNOPQRSTUVWXYZabcdefghigkhlmopqrstuvwsyz0123456789";
StringBuffer stringBuffer = new StringBuffer();
for (int i = 1; i <= 4; i++) {
int num = new Random().nextInt(str.length());
char chr = str.charAt(num);
stringBuffer.append(chr);
graphics.drawString(chr+"",wight/5*i,25);
}
String string = stringBuffer.toString();
HttpSession session = req.getSession();
session.setAttribute("session_id",string);
for (int i = 1; i <= 10; i++) {
int num1 = new Random().nextInt(wight);
int num2 = new Random().nextInt(wight);
int num3 = new Random().nextInt(height);
int num4 = new Random().nextInt(height);
graphics.setColor(Color.GREEN);
graphics.drawLine(num1,num3,num2,num4);
}
//客户端输出
ImageIO.write(bufferedImage,"jpg",resp.getOutputStream());
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req, resp);
}
}
package itcast.web.sessionDemoTest;
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 java.io.IOException;
@WebServlet("/sessionLogin")
public class SessionLogin extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String username = req.getParameter("username");
String password = req.getParameter("password");
String checkcode = req.getParameter("checkcode");
HttpSession session = req.getSession();
String session_id = (String) session.getAttribute("session_id");
session.removeAttribute("session_id");
if (session_id!=null&&session_id.equalsIgnoreCase(checkcode)){
//验证码验证通过
if ("zhangsan".equals(username)&&"123456".equals(password)){
session.setAttribute("user",username);
resp.sendRedirect(req.getContextPath()+"/sessionSuccess.jsp");
}
else {
req.setAttribute("error_user","用户名或者密码错误,请重新输入");
req.getRequestDispatcher("/sessionLogin.jsp").forward(req,resp);
}
}
else {
req.setAttribute("error_check","验证码错误,请重新输入");
req.getRequestDispatcher("/sessionLogin.jsp").forward(req,resp);
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req, resp);
}
}
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2019/8/20
Time: 15:03
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登陆成功</title>
</head>
<body>
<div>欢迎<%=request.getSession().getAttribute("user")%>登陆</div>
</body>
</html>