导包:
login.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登录</title>
</head>
<body>
<form action="/LoginServlet" method="post">
用户名:
<input type="text" name="username"><br>
密 码:
<input type="password" name="password"><br>
验证码:
<input type="text" name="code"><img src="/ValidateCodeServlet"><br>
<button type="submit">登录</button>
</form>
</body>
</html>
welcome.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>首页</title>
</head>
<body>
welcome!
<br>
<a href="/login.jsp">退出</a>
</body>
</html>
ValidateCodeServlet.java
package com.qfedu.servlet.demo;
import cn.dsna.util.images.ValidateCode;
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;
/**
* @author xue_yun_xiang
* @create 2021-04-13-19:42
*/
@WebServlet("/ValidateCodeServlet")
public class ValidateCodeServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//验证码工具类
ValidateCode validateCode = new ValidateCode(200, 30, 4, 30);
//获取生成的验证码
String code = validateCode.getCode();
System.out.println(code);
//将验证码放入会话中
HttpSession session = req.getSession();
session.setAttribute("code",code);
//将验证码图片放到前端并将图片写入响应输出流
validateCode.write(resp.getOutputStream());
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
LoginServlet.java
package com.qfedu.servlet.demo;
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;
/**
* @author xue_yun_xiang
* @create 2021-04-13-19:32
*/
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String username = req.getParameter("username");
String password = req.getParameter("password");
String code = req.getParameter("code");
HttpSession session = req.getSession();
//获取生成的验证码
String sessionCode = (String) session.getAttribute("code");
//验证码忽略大小写
if ("root".equals(username) && "123456".equals(password) && code.equalsIgnoreCase(sessionCode)) {
//设置session的成功标记
session.setAttribute("success",true);
//重定向
resp.sendRedirect("/WelcomeServlet");
//禁用cookie后的解决办法:重写URL
//重定向:302
// String url = resp.encodeURL("/WelcomeServlet");
//resp.sendRedirect(url);
} else {
req.getRequestDispatcher("/login.jsp").forward(req, resp);
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
WelcomeServlet.java
package com.qfedu.servlet.demo;
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;
/**
* @author xue_yun_xiang
* @create 2021-04-13-19:40
*/
@WebServlet("/WelcomeServlet")
public class WelcomeServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
HttpSession session = req.getSession();
Object result = session.getAttribute("success");
if (result != null) {
boolean success = (boolean)result;
if (success) {
//重定向
resp.sendRedirect("/welcome.jsp");
} else {
req.getRequestDispatcher("/login.jsp").forward(req, resp);
}
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
LoginOutServlet.java
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @author xue_yun_xiang
* @create 2021-04-13-19:39
*/
@WebServlet("/LoginOutServlet")
public class LoginOutServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doGet(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
}