You work with elements in web.xml
to attach a filter to a Web resource, and you can attach a filter to such
resources as servlets and JSP pages.
It's easy for a filter to gain access to a Web resource's
request and response objects both before and after the Web resource is
accessed. A filter can display text on the server console with the System.out.println method, as well as
insert text into the response from the filtered Web resource.
You can also chain filters together by attaching multiple
filters to the same Web resource. The filters are called in the order you
specify in web.xml before
calling the Web resource, and then in reverse order after the Web resource
executes.
A filter has access to initialization parameters, just as a
servlet does, and you can use Tomcat log files to log user actions using
filters.
You can use a filter to grant or deny access to a Web resource
by implementing password-protected user authentication using filters, as well
as denying access to a Web resource during certain hours of the day.
Understanding Filters
To use a filter, you have to implement
the javax.servlet.Filter
interface in your filter's code. This interface has three important methods—init,
which is called when the filter is first initialized, destroy, which is called when the filter
is destroyed, and doFilter. The doFilter method is where the action is—you
use this method to pass the response and request objects on to Web resources and
other filters.The init method
is also passed an object of the FilterConfig
class, which gives you access to initialization parameters, Web application
contexts, and more.
PassWord.java
import java.io.*;
import javax.servlet.*;
public class PassWord implements Filter{
private FilterConfig filterConfig = null;
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)
throws IOException, ServletException
{
String password = request.getParameter("password");
if(password.equals("opensesame")) {
chain.doFilter(request, response);
} else {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<HTML>");
out.println("<HEAD>");
out.println("<TITLE>");
out.println("Incorrect Password");
out.println("</TITLE>");
out.println("</HEAD>");
out.println("<BODY>");
out.println("<H1>Incorrect Password</H1>");
out.println("Sorry, that password was incorrect.");
out.println("</BODY>");
out.println("</HTML>");
}
}
@Override
public void destroy() { }
@Override
public void init(FilterConfig filterConfig)
{
this.filterConfig = filterConfig;
}
}
Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<filter>
<filter-name>PassWord</filter-name>
<filter-class>PassWord</filter-class>
</filter>
<filter-mapping>
<filter-name>PassWord</filter-name>
<url-pattern>/Congrats.jsp</url-pattern>
</filter-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>
Congrats.jsp
<%--
Document : Congrats
Created on : Dec 4, 2014, 9:19:42 AM
Author : gyanendra
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<H1>Filters And User Authentication</H1>
Congratulations, you're in!
<BR>
</body>
</html>
index.jsp
<%--
Document : index
Created on : Dec 3, 2014, 11:11:27 AM
Author : gyanendra
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<HTML>
<HEAD>
<TITLE>Using Passwords and Filters</TITLE>
</HEAD>
<BODY>
<H1>Using Passwords and Filters</H1>
<FORM ACTION="Congrats.jsp" METHOD="POST">
Please enter your password:
<INPUT TYPE="PASSWORD" NAME="password">
<BR>
<INPUT TYPE="SUBMIT" VALUE="Submit">
</FORM>
</body>
</html>
****************************************************************************
FilterWebPage.java
import java.io.*;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import javax.servlet.*;
public class FilterWebPage implements Filter{
private FilterConfig filterConfig = null;
@Override
public void init(FilterConfig filterConfig)
{
this.filterConfig = filterConfig;
}
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)
throws IOException, ServletException
{
GregorianCalendar calendar = new GregorianCalendar();
Date date1 = new Date();
calendar.setTime(date1);
int hour = calendar.get(Calendar.HOUR_OF_DAY);
if(hour < 9 || hour > 17) {
chain.doFilter(request, response);
} else {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<HTML>");
out.println("<HEAD>");
out.println("<TITLE>");
out.println("Get Back to Work!");
out.println("</TITLE>");
out.println("</HEAD>");
out.println("<BODY>");
out.println("<H1>Get Back to Work!</H1>");
out.println("Sorry, that resource is not available now.");
out.println("</BODY>");
out.println("</HTML>");
}
}
@Override
public void destroy()
{
}
}
Web.xml
<filter>
<filter-name>FilterWebPage</filter-name>
<filter-class>FilterWebPage</filter-class>
</filter>
<filter-mapping>
<filter-name>FilterWebPage</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

No comments:
Post a Comment