authentication filter in web api code example
Example 1: Implement Authentication filter using filter API
import java.io.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ServletFilter extends HttpServlet {
@Override
public void doGet(HttpServletRequest res, HttpServletResponse resp) throws IOException, ServletException {
resp.setContentType("text/html;charset=UTF-8");
PrintWriter out = resp.getWriter();
String str = res.getParameter("name");
out.print("<h1>Hello " + str + "</h1><h2>You have sucessfully logged in....</h2>");
}
}
Example 2: Implement Authentication filter using filter API
<!-- XML File -->
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<servlet>
<servlet-name>ServletFilter</servlet-name>
<servlet-class>ServletFilter</servlet-class>
</servlet>
<welecome-file-list>
<welcome-file>index.html</welcome-file>
</welecome-file-list>
<servlet-mapping>
<servlet-name>ServletFilter</servlet-name>
<url-pattern>/filtering</url-pattern>
</servlet-mapping>
<filter>
<filter-name>f1</filter-name>
<filter-class>MyFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>f1</filter-name>
<url-pattern>/filtering</url-pattern>
</filter-mapping>
</web-app>