简体   繁体   English

Java:使用过滤器(已登录用户/未登录用户)

[英]Java: Using filter (user logged/user not logged)

I need to write a code, that will be redirecting to different *.jsp sites depending on whether user is logged on or not logged on. 我需要编写一个代码,该代码将根据用户是否登录而重定向到不同的* .jsp站点。 I found a hint, that I can use filter to do it and I need to use doFilter or/along with init methods. 我发现了一个提示,我可以使用过滤器来执行此操作,并且需要在init方法中使用doFilter或/以及其他方法。 Any ideas? 有任何想法吗?

public void doFilter(ServletRequest req, ServletResponse res,
            FilterChain chain) throws IOException, ServletException {
    }
public void init(FilterConfig config) throws ServletException {

    }

This is a very basic sample...but let's suppose that the login proces set in session an attribute called "user" in the doFilter method you can do something like this 这是一个非常基本的示例...但是让我们假设登录过程在会话中设置了doFilter方法中名为“ user”的属性,您可以执行以下操作

if( request.getSession().getAttribute("user") == null )
{
//User not logged...redirect
}
else
{
//Normal filter execution
}

init() method will be called on Filter's initialization and doFilter() will be called when a request is made and Filter is mapped to filter those request init()方法将在Filter的初始化时调用,并且doFilter()将在发出请求时被调用,并且Filter被映射为过滤这些请求


Related: 有关:

For an example, see Filters Tutorial , particularly the section titled Authentication with Filters. 有关示例,请参阅“ 过滤器教程” ,特别是标题为“使用过滤器进行身份验证”的部分。 (There's a typo that actually makes this say "Authentication with Filers" but that is the section I am referring to...obviously it is supposed to say filters :) (实际上有一个错字,就是说“通过文件管理器验证”,但这是我要指的部分...显然应该说过滤器:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM