Login.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/Login" method="get">
<input type="text" name="username">
<input type="submit">
</form>
</body>
</html>
success.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%--<%
Object user_session = request.getSession().getAttribute("USER_SESSION");
if(user_session==null)
response.sendRedirect("Login.jsp");//这种拦截不应该由jsp来做,应该使用Filter
%>--%>
<h1>Login Success!</h1><br>
<a href="${pageContext.request.contextPath}/LogoutServlet">注销</a>
</body>
</html>
fail.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>Login Fail!</h1>
<a href="../Login.jsp">返回登录页面</a>
</body>
</html>
LoginServlet
package Servlet;
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 java.io.IOException;
@WebServlet("/Login")
public class LoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username=request.getParameter("username");
String url=request.getContextPath();
if(username.equals("admin"))
{
request.getSession().setAttribute("USER_SESSION", request.getSession().getId());
response.sendRedirect(url+"/sys/success.jsp");
}
else
{
response.sendRedirect(url+"/sys/fail.jsp");
}
}
}
LogoutServlet
package Servlet;
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 java.io.IOException;
@WebServlet( "/LogoutServlet")
public class LogoutServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String url=request.getContextPath();
Object user_session = request.getSession().getAttribute("USER_SESSION");
if(user_session!=null)
{
request.getSession().removeAttribute("USER_SESSION");
}
response.sendRedirect(url+"/Login.jsp");
}
}
sysFilter
package Filter;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebFilter(filterName = "SysFilter")
public class SysFilter implements Filter {
public void destroy() {
}
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
HttpServletResponse response=(HttpServletResponse)resp;
HttpServletRequest request=(HttpServletRequest)req;
Object user_session = request.getSession().getAttribute("USER_SESSION");
if(user_session==null)
{
String url=request.getContextPath();
response.sendRedirect(url+"/Login.jsp");
}
chain.doFilter(req, resp);
}
public void init(FilterConfig config) throws ServletException {
}
}
配置xml
<filter>
<filter-name>sysFilter</filter-name>
<filter-class>Filter.SysFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>sysFilter</filter-name>
<url-pattern>/sys/*</url-pattern>
</filter-mapping>