Servlet – 身份验证过滤器
Servlet 是在支持Java的 Web 服务器或应用程序服务器上运行的 Java 程序。它们用于处理从网络服务器获得的请求,处理请求,产生响应,然后将响应发送回网络服务器。
Servlet 中的身份验证过滤器
认证可以在过滤器中完成。在这里,我们将在 ServletFilter 类中验证用户的密码;如果密码是“geeksforgeeks” ,请求将被发送到 Gfg servlet;否则,将显示错误消息。
实现:让我们看一个利用过滤器对用户进行身份验证的简单示例。我们在这里生成了四个文件:
- 索引.html
- 女朋友。Java
- 小服务过滤器。Java
- web.xml
文件: index.html
HTML
Java
// Java Program to Illustrate ServletFilter Class
// Importing required classes
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.*;
// Class
// Implementing Filter class
public class ServletFilter implements Filter {
public void init(FilterConfig arg0)
throws ServletException
{
}
public void doFilter(ServletRequest req,
ServletResponse resp,
FilterChain chain)
throws IOException, ServletException
{
PrintWriter out = resp.getWriter();
String password = req.getParameter("password");
if (password.equals("geeksforgeeks")) {
// Sending request to next
chain.doFilter(req, resp);
}
// Password incorrect
else {
out.print("username or password is wrong");
RequestDispatcher rd
= req.getRequestDispatcher("index.html");
rd.include(req, resp);
}
}
public void destroy() {}
}
Java
// Java Program to Illustrate GFG Class
// Importing required classes
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;
// Class
// Derived from HttpServlet class
public class GFG extends HttpServlet {
// Getting request response
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.print("welcome to GEEKSFORGEEKS");
// Closing connections to
// avoid any memory leakage
out.close();
}
}
XML
Gfg
Gfg
Gfg
/servlet1
f1
ServletFilter
f1
/servlet1
示例 1-A:
Java
// Java Program to Illustrate ServletFilter Class
// Importing required classes
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.*;
// Class
// Implementing Filter class
public class ServletFilter implements Filter {
public void init(FilterConfig arg0)
throws ServletException
{
}
public void doFilter(ServletRequest req,
ServletResponse resp,
FilterChain chain)
throws IOException, ServletException
{
PrintWriter out = resp.getWriter();
String password = req.getParameter("password");
if (password.equals("geeksforgeeks")) {
// Sending request to next
chain.doFilter(req, resp);
}
// Password incorrect
else {
out.print("username or password is wrong");
RequestDispatcher rd
= req.getRequestDispatcher("index.html");
rd.include(req, resp);
}
}
public void destroy() {}
}
示例 1-B:
Java
// Java Program to Illustrate GFG Class
// Importing required classes
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;
// Class
// Derived from HttpServlet class
public class GFG extends HttpServlet {
// Getting request response
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.print("welcome to GEEKSFORGEEKS");
// Closing connections to
// avoid any memory leakage
out.close();
}
}
文件: web.xml
XML
Gfg
Gfg
Gfg
/servlet1
f1
ServletFilter
f1
/servlet1
输出:
Click on the login button, if the password is correct then the above message will be shown to the user.