Source Code : Block Filter
Java Is Open Source Programming Language You Can Download From Java and Java Libraries From http://www.oracle.com.
Click Here to download
We provide this code related to title for you to solve your developing problem easily. Libraries which is import in this program you can download from http://www.oracle.com.
Click Here or search from google with Libraries Name you get jar file related it
Block Filter
import java.io.IOException;
import java.io.PrintWriter;
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.http.HttpServletRequest;
public class BlockFilter implements Filter {
private FilterConfig config;
/** Creates new BlockFilter */
public BlockFilter() {
}
public void init(FilterConfig filterConfig) throws ServletException {
this.config = filterConfig;
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = null;
boolean authenticated = false;
PrintWriter out = null;
if (request instanceof HttpServletRequest) {
req = (HttpServletRequest) request;
String user = req.getParameter("user");
authenticated = authenticateUser(user);
}
if (authenticated) {
chain.doFilter(request, response);
} else {
response.setContentType("text/html");
out = response.getWriter();
out
.println("<html><head><title>Authentication Response</title></head><body>");
out.println("<h2>Sorry your authentication attempt failed</h2>");
out.println("</body></html>");
out.close();
}
}// doFilter
public void destroy() {
/*
* called before the Filter instance is removed from service by the web
* container
*/
}
private boolean authenticateUser(String userName) {
//authenticate the user using JNDI and a database, for instance
return false;
}
}
Thank with us