jsp没有登陆,怎么才能不让用户进入其它页面

2024-12-29 00:52:35
推荐回答(3个)
回答1:

写一个过滤器---LoginFilter

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import electroniccommerceweb.entity.Member;
import electroniccommerceweb.entity.Cart;

public class LoginFilter extends HttpServlet implements Filter {
private FilterConfig filterConfig;
//Handle the passed-in FilterConfig
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
}

//Process the request/response pair
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain filterChain) {
try {
HttpServletRequest req=(HttpServletRequest)request;
HttpServletResponse res=(HttpServletResponse)response;
HttpSession session=req.getSession();
LoginInfo login=(LoginInfo)session.getAttribute("login");//此session是你在登录是放入的用户实体
if(login==null)
{
res.sendRedirect("login.jsp");
}
else
{
filterChain.doFilter(request, response);
}
} catch (ServletException sx) {
filterConfig.getServletContext().log(sx.getMessage());
} catch (IOException iox) {
filterConfig.getServletContext().log(iox.getMessage());
}
}

//Clean up resources
public void destroy() {
}
}

回答2:

我给你一个思路,login.jsp登陆后再session记录个标记,其他页面首先检查这个标记是否存在,不存在就跳转。

回答3:

他登陆之后把他用户名存入session中,只要判断session为空就说明他没登陆就转到另外页面