download。jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>download</title>
</head>
<body>
<a href="">ziyau1</a>
<a href="">ziya2</a>
<a href="">ziyau3</a>
</body>
</html>
login.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>login</title>
</head>
<body>
<form action="/login"method="post">
<input type="text"name="name"><br>
<input type="password"name="password"><br>
<input type="submit"value="提交">
</form>
</body>
</html>
loginservlet
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String name=req.getParameter("name");
String password=req.getParameter("password");
if(name.equals("admin")&&password.equals("123")){
HttpSession session=req.getSession();
session.setAttribute("name",name);
resp.sendRedirect("/download.jsp");
}
System.out.println(name);
}
}
downloadservlet
@WebFilter("/download.jsp")标签:name,filter,session,jsp,简单,Override,权限,public,String From: https://www.cnblogs.com/uninan/p/17383233.html
public class DOwnloadFIlter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request=(HttpServletRequest)servletRequest;
HttpServletResponse response=(HttpServletResponse)servletResponse;
HttpSession session=request.getSession();
String name = (String) session.getAttribute("name");
if(name==null){
//不是登录状态
response.sendRedirect("/login.jsp");
}else {
filterChain.doFilter(servletRequest,servletResponse);
}
}
@Override
public void destroy() {
}
}