Servlet – FilterConfig
Web 容器为每个过滤器创建一个 FilterConfig 对象。它可用于读取过滤器初始化参数。放置在 web.xml 文件中。如果配置信息是从 web.xml 文件中修改的,我们不需要更改过滤器。因此,如果不时修改任何特定内容,则更容易管理 Web 应用程序。
FilterConfig 接口的方法
FilterConfig 接口中有四个方法。
1. String getFilterName():返回部署描述符中定义的此过滤器的过滤器名称。
Syntax: public String getFilterName()
2. String getInitParameter(String name):返回一个包含命名初始化参数值的String,如果参数不存在则返回null。
Syntax: public String getInitParameter(String name)
3. Enumeration getInitParameterNames():如果过滤器没有初始化参数,则将过滤器初始化参数的名称作为字符串对象的枚举或空枚举返回。
Syntax: public Enumeration getInitParameterNames()
4. ServletContext getServletContext():返回对调用者正在执行的ServletContext的引用
Syntax: public ServletContext getServletContext()
例子
索引.html
HTML
TODO supply a title
FilterConfig Demo
Java
import static java.lang.System.out;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class abc extends HttpServlet {
protected void
processRequest(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html;charset=UTF-8");
PrintWriter p = response.getWriter();
p.println(
"Welcome User
");
}
//
@Override
protected void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
processRequest(request, response);
}
@Override
public String getServletInfo()
{
return "Short description";
}
}
Java
package demo;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class filter_config_demo implements Filter {
FilterConfig fc;
@Override
public void init(FilterConfig filterConfig)
throws ServletException
{
this.fc = filterConfig;
}
@Override
public void doFilter(ServletRequest request,
ServletResponse response,
FilterChain chain)
throws IOException, ServletException
{
String un, up;
un = request.getParameter("un");
up = request.getParameter("up");
PrintWriter p = response.getWriter();
System.out.println("My filter Name is "
+ fc.getFilterName());
if (un.equals(fc.getInitParameter("username"))
&& up.equals(
fc.getInitParameter("userpassword")))
{
chain.doFilter(request, response);
}
else
{
RequestDispatcher rd
= request.getRequestDispatcher(
"/index.html");
rd.include(request, response);
p.println("incorrect user name and password");
}
}
@Override
public void destroy()
{
this.fc = null;
}
}
XML
filter1
demo.filter_config_demo
username
sumit
userpassword
sumit@123
filter1
abc
abc
abc
abc
/abc
30
美国广播公司。Java
Java
import static java.lang.System.out;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class abc extends HttpServlet {
protected void
processRequest(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html;charset=UTF-8");
PrintWriter p = response.getWriter();
p.println(
"Welcome User
");
}
//
@Override
protected void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
processRequest(request, response);
}
@Override
public String getServletInfo()
{
return "Short description";
}
}
过滤器配置演示。Java
Java
package demo;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class filter_config_demo implements Filter {
FilterConfig fc;
@Override
public void init(FilterConfig filterConfig)
throws ServletException
{
this.fc = filterConfig;
}
@Override
public void doFilter(ServletRequest request,
ServletResponse response,
FilterChain chain)
throws IOException, ServletException
{
String un, up;
un = request.getParameter("un");
up = request.getParameter("up");
PrintWriter p = response.getWriter();
System.out.println("My filter Name is "
+ fc.getFilterName());
if (un.equals(fc.getInitParameter("username"))
&& up.equals(
fc.getInitParameter("userpassword")))
{
chain.doFilter(request, response);
}
else
{
RequestDispatcher rd
= request.getRequestDispatcher(
"/index.html");
rd.include(request, response);
p.println("incorrect user name and password");
}
}
@Override
public void destroy()
{
this.fc = null;
}
}
web.xml
XML
filter1
demo.filter_config_demo
username
sumit
userpassword
sumit@123
filter1
abc
abc
abc
abc
/abc
30
输出
首先,我们输入了错误的用户名和密码:
- 用户 ID - 管理员
- 密码 - 12345
现在我们输入正确的用户名和密码:
- 用户 ID – sumit
- 密码 – sumit@123