Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Use Of Filter in Web App

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Angry Use Of Filter in Web App

    HI,
    Can anyone give me the idea for why we use Filter in web applicaiton. eg in servlet.

    Thanks
    Noorul.


  2. #2
    Junior Member
    Join Date
    Jan 2013
    Posts
    20
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Use Of Filter in Web App

    Hi ,
    Filters are acts as a front end design pattern. It mean any request go through to filter. These filters are used for authentication purpose, to store loggin informations, data compression and image conversion etc. For example given filter check given request get or post.


    public class AutFilter implements Filter {

    public void init(FilterConfig config) {
    System.out.println("init method called");
    }


    public void doFilter(ServletRequest request,ServletResponse respons,FilterChain chain) throws IOException {
    HttpServletRequest hrequest=(HttpServletRequest)request;

    PrintWriter out=request.getWriter();

    if(hrequest.getMethod().equalsIgnoreCase("post")) {
    chain.doFilter(request,response);
    }
    else {
    out.write("get method is not allowed");
    }
    }


    public void destroy() {
    System.out.println("destroy method is called");
    }

    }

    in web.xml:

    <web-app>
    <filter>
    <filter-name>AutFilter</filtr-name>
    <filter-class>AutFilter</filter-class>
    </filter>
    <filter-mapping>
    <filter-name>AutFilter</filtr-name>
    <url-pattern>/*</url-patterm>
    </filter-mapping>

    </web-app>

    one.html:
    -----------
    <head> <body> <form action="/webproject/two.html" method="post"> <inpu type="submit" vaue="submit: /></form></body></html>

    two.html:
    -----------
    it is two.html.

    For filters no need to used <load-on-startup> tag.Defaultly the server create the object for filters at deployment time and it call the init(FilterConfig) method. Every filter conatin the url-pattern /* , hence whenever send the request to server (/webproject/one.html) , the server call the doFilter(-,-,-) of the AutFilter. This method check given request is get or post. If it is post request the doFilter(request,response) method send request to two.html .If it is get request just it display the "get method is not allowed".

  3. #3

    Default Re: Use Of Filter in Web App

    Filters are powerful features provided by servlet API to implement cross-cutting features such as logging, auditing, keeping track of user activities, zipping a large file before sending it to a user, or creating a different response altogether.

Similar Threads

  1. Java web app, Saving to desktop?
    By Java Programmer in forum What's Wrong With My Code?
    Replies: 14
    Last Post: January 21st, 2012, 04:41 PM
  2. will pay 20$ for a web app
    By imohi999 in forum Paid Java Projects
    Replies: 3
    Last Post: December 7th, 2011, 11:28 AM
  3. Web app image processing from stream to UIComponent
    By throwcode in forum File I/O & Other I/O Streams
    Replies: 8
    Last Post: August 19th, 2011, 10:58 AM
  4. Please help!!! Adding java app to a web application
    By snapper in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 19th, 2011, 10:26 AM
  5. Creating a java program to "Beat" Snake web app?
    By AkOndray in forum AWT / Java Swing
    Replies: 2
    Last Post: December 5th, 2010, 12:36 AM