...::: Recent Updates :::...

Wednesday, June 20, 2012

Filter

Download Source Code
<!-- Create a servlet filter that logs all access to and from servlets in an application and
prints the following to System.out: or in your terminal or konsole
a. the time
d. the URL of the resource requested
e. the IP address of the visitor-->
<!-- display_Filter.java Filter Class-->
package FilterDisplay;
import java.io.*;
import javax.servlet.*;
import java.util.*;
import javax.servlet.http.*;
public class display_Filter implements Filter
{
    public void doFilter(ServletRequest request,ServletResponse response,FilterChain fc)throws ServletException,IOException
    {
        HttpServletRequest req=(HttpServletRequest)request;
        System.out.println("Requesting URL:-"+req.getRequestURL());
        System.out.println("Host or Ip address:-"+req.getRemoteHost());
        System.out.println("Date Of Accessing Request And Response Time:- "+new Date());
        fc.doFilter(request,response);
    }
    public void init(FilterConfig config){}
    public void destroy(){}
}
<!-- filter is implemented in this Servlet  Simple.java-->
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class Simple extends HttpServlet
{
    public void doGet(HttpServletRequest requeset,HttpServletResponse response)throws ServletException,IOException
    {
        response.setContentType("text/html");       
        PrintWriter out=response.getWriter();
        out.println("<html><head><title>fileter Example</title></head><body>");
        out.println("Hello See Your Terminal Please :) ");
        out.println("</body></html>");
        out.close();
    }
}
<!--web.xml file -->
<web-app>
    <servlet>
        <servlet-name>Display</servlet-name>
        <servlet-class>Simple</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Display</servlet-name>
        <url-pattern>/Maulin_Filter</url-pattern>
    </servlet-mapping>
    <filter>
        <filter-name>Display_Filter</filter-name>
        <filter-class>FilterDisplay.display_Filter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>Display_Filter</filter-name>
        <servlet-name>Display</servlet-name>
    </filter-mapping>
</web-app>

No comments:

Post a Comment