我正在 JSF 应用程序中实现登录过滤器。我有一个名为 config 的私有(private)字段。尽管在 init 和 destroy 方法中引用了该字段,Eclipse 仍显示警告“未使用字段 AuthenticationFilter.config 的值”。我在这里做错了什么,还是 Eclipse 有点困惑?
这是 AuthenticationFilter 类:
package com.mymato.coop;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebFilter("/restricted/*")
public class AuthenticationFilter implements Filter {
private FilterConfig config;
public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain chain) throws IOException, ServletException {
if (((HttpServletRequest) req).getSession().getAttribute(
LoginBean.AUTH_KEY) == null) {
((HttpServletResponse) resp).sendRedirect("../login.xhtml");
} else {
chain.doFilter(req, resp);
}
}
public void init(FilterConfig config) throws ServletException {
this.config = config;
}
public void destroy() {
config = null;
}
}
最佳答案
初始化和销毁config
并不是“使用”它们 - 编译器告诉您,您有一个变量,您在初始化和销毁时浪费了内存和时间,但实际上它不是在任何地方都可以使用。
关于java - Eclipse 中未使用字段警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38540122/