Welcome to Abdul Malik Ikhsan's Blog

Servlet Filter

Posted in Tutorial JSP, Tutorial Servlet by samsonasik on June 7, 2008

Filter adalah sebuah komponen yang dapat menangani request atau response yang harus atau tidak harus dilakukan/diterima oleh client. Secara normal, filter digunakan untuk membungkus dan memanipulasi request, response, atau nilai header sebelum dan setelah eksekusi target.

Now, kita akan mencoba mempraktekkan sebuah filter sederhana 🙂
Pertama, kita siapkan GenericFilter sebagai implementasi dari interface javax.servlet.Filter, mengimplementasikan method doFilter, init,dan destroy.
Method init digunakan untuk menginisialisasi filter, yang kemudian dijalankan oleh doFilter, sedang method destroy digunakan untuk menghancurkan object yang diinstansiasi dari kelas FilterConfig.
berikut coding standard untuk kelas GenericFilter :

Code:
/*
from http://www.orionserver.com/tutorials/filters/1.html
*/
package filters.learn;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.ServletException;
import java.io.IOException;

public class GenericFilter implements Filter{
private FilterConfig filterConfig;
public void doFilter(final ServletRequest req,final ServletResponse resp, FilterChain chain)
throws IOException,ServletException{
chain.doFilter(req,resp);
}
public FilterConfig getFilterConfig(){
return filterConfig;
}
public void init(FilterConfig config){
this.filterConfig = config;
}
public void destroy(){
this.filterConfig = null;
}
}

Now, let’s Writing your first Filter :

Code:
package filters.learn;
import filters.learn.GenericFilter;

import java.io.*;
import javax.servlet.http.*;
import javax.servlet.*;

public class HelloWorldFilter extends GenericFilter {

public void doFilter(final ServletRequest req, final ServletResponse resp, FilterChain chain)
throws IOException, ServletException {
req.setAttribute(“me”,”Ikhsan”);
chain.doFilter(req,resp);
}

}

Kita akan mencoba mengetes filter di atas, kita siapkan file JSP kita :

Code:
hai, ini aku ${me}

Pertanyaannya, bagaimana configurasi filter di atas?? eit …, tunggu dulu, sebelum kita bicara konfigurasi, tentunya kita butuh sebuah servlet lagi yang akan kita memforward, dan memapping address url, berikut codingnya :

Code:
package filters.learn;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class ServletForwarderHello extends HttpServlet{
public void doGet(HttpServletRequest req,HttpServletResponse resp)
throws ServletException, IOException{
req.getRequestDispatcher(“/filts/filter1.jsp”).forward(req,resp);
}
}

Nah, sekarang waktunya konfigurasi web.xml kita :

…..

<servlet>
<servlet-name>servletforwarderhellofilter</servlet-name>
<!–class servlet –>
<servlet-class>filters.learn.ServletForwarderHello</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>servletforwarderhellofilter</servlet-name>
<!–alamat url –>
<url-pattern>/filter</url-pattern>
</servlet-mapping>

<filter>
<filter-name>HelloWorld</filter-name>
<filter-class>filters.learn.HelloWorldFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HelloWorld</filter-name>
<url-pattern>/filter</url-pattern>
</filter-mapping>

…..

Now, let’s try : http://localhost:8080/advance/filter (ceritanya, saya built ke dalam advance.war, kalau ditaruh di bawah direktori ROOT, cukup http://localhost:8080/filter)

Selamat mencoba 🙂
Referensi :
1. http://www.orionserver.com/tutorials/filters/
2. http://www.javapassion.com

Advertisements

4 Responses

Subscribe to comments with RSS.

  1. jolly said, on January 17, 2009 at 3:50 am

    I see that, so whenever url patter form like “/filter” in url-bar, (even just the subset) as writen in tag the servlet filter will be execute, that is the concept rite? so the point of my question is, what can make this concept fail? bcoz i have failure with it. thanks fo helping.

  2. samsonasik said, on January 17, 2009 at 4:38 pm

    have you restart your tomcat ? if you still failed, maybe, there is an error in your configuration, you can read in : http://www.coreservlets.com/Apache-Tomcat-Tutorial
    or, you can download the concept of mvc pattern in :
    http://www.4shared.com/file/46279624/556510d/mvcpattern.html

  3. jolly said, on January 21, 2009 at 9:12 am

    hello again, thank you for share.

  4. sheandayani said, on May 11, 2009 at 1:52 pm

    Permisi… minta ijin unduh data yah… Thankss….


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: