Creating Servlet Filters with Rational Application Developer

Loading...

Sign in or sign up now!
Alert icon
Upgrade to the latest Flash Player for improved playback performance. Upgrade now or more info.
5,774
Loading...
Alert icon
Sign in or sign up now!
Alert icon

Uploaded by on Aug 4, 2008

You can find the original, high resolution video tutorial on creating Java ServletFilters right here:

http://jpa.thebookonhibernate.com/j2ee/index.jsp?link=08servletfilters

Creating a Custom Servlet Filter

This free, multimedia tutorial shows you how to use IBM's Rational Application Developer (IRAD) 6.0 to create a Servlet Filter. The web.xml file is then edited to apply the filter to the CountrySnooperServlet, to ensure that only true, patriotic Americans actually get to see the output of the CountrySnooperServlet. We also add in a little TimeStamping or Time Logging functionality, but that's neither here nor there.

If you found something helpful here, please do your part and help support the site. Link to us, buy some books, support our sponsors, tell your developer friends about us, and remember: Happy Java!

package com.examscam.web.filter;

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

public class AmericanFilter implements Filter {

public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}


public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
String language = request.getLocale().getDisplayLanguage(); if (language!="Spanish") { response.getWriter().print("Only people speaking American can view this Servlet.");
}
else{ chain.doFilter(request, response);
}
System.out.println("Time after the servlet is run: " + System.currentTimeMillis());
}


public void destroy() {
// TODO Auto-generated method stub
}

}




Google Enter your search terms Submit search form
Download the Completed Solution: 08ServletFilters.ear.

Scroll to the bottom of the page for pertinent code snippets.
Creating a Custom Servlet Filter

This free, multimedia tutorial shows you how to use IBM's Rational Application Developer (IRAD) 6.0 to create a Servlet Filter. The web.xml file is then edited to apply the filter to the CountrySnooperServlet, to ensure that only true, patriotic Americans actually get to see the output of the CountrySnooperServlet. We also add in a little TimeStamping or Time Logging functionality, but that's neither here nor there.

If you found something helpful here, please do your part and help support the site. Link to us, buy some books, support our sponsors, tell your developer friends about us, and remember: Happy Java!


package com.examscam.web.filter;

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

public class AmericanFilter implements Filter {

public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}


public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
String language = request.getLocale().getDisplayLanguage(); if (language!="Spanish") { response.getWriter().print("Only people speaking American can view this Servlet.");
}
else{ chain.doFilter(request, response);
}
System.out.println("Time after the servlet is run: " + System.currentTimeMillis());
}


public void destroy() {
// TODO Auto-generated method stub
}

}

package com.examscam.servlet;

import java.io.IOException;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CountrySnooper extends HttpServlet implements Servlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String country = request.getLocale().getCountry();
if (country.equals("CA")){
response.getWriter().print("Hey, lets go for a Tim Hortons coffee!");
}
else {
response.getWriter().print("We love people who speak " + request.getLocale().getDisplayLanguage());
}
}
}

You can download the Java source code here:
http://jpa.thebookonhibernate.com/j2ee/vangogh/08servletfilter.ear

Please check out my SCJA Java Certification Mock Exam website:
http://www.scja.com/associate/

And for Hibernate and JPA tutorials, check out:

http://www.thebookonhibernate.com


IRAD SCWCD

  • likes, 0 dislikes

Link to this comment:

Share to:
see all

All Comments (4)

Sign In or Sign Up now to post a comment!
  • Thank you very helpful

  • Heyyyyy this is cool. I dont hear any machine typing sound anymore! Coooolllllll at last I could make my speaker louder =D

  • can you please keep your mouth a bit away from mic.  its very irritating

Loading...

Alert icon
0 / 00Unsaved Playlist Return to active list
    1. Your queue is empty. Add videos to your queue using this button:
      or sign in to load a different list.
    Loading...Loading...Saving...
    • Clear all videos from this list
    • Learn more