Uploaded by javaprogrammingcbts 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
-
4 likes, 0 dislikes
8:06
ServletwithFilter.flvby siva2cu662 views
47:00
Servlet Basicsby ignousocis70,245 views
5:49
Servlets Video Tutorialby sharman210153,417 views
8:36
Using the HttpSession Object: Servlet and JSP Tutorials J2EEby javaprogrammingcbts45,939 views
3:12
Rational Application Developer for WebSphere Software V8by IBMRedbooks2,093 views
46:10
Session Tracking in Servlet Programmingby ignousocis10,257 views
9:56
Web applications with Java - Tutorial 02 - Servlet + Basic HTML Formby camigonz45,627 views
5:26
SCJP note Part Iby zhouchenjuan11,732 views
7:04
PortletModes: doEdit doView doHelp doPrint doConfigure: 168by tutorials168portlets2,204 views
7:20
Creating StateFUL Session Beans (SFSB) with IRAD / WebSphereby javaprogrammingcbts5,138 views
3:48
Deploying Servlet on Tomcatby lifemichael10,940 views
9:14
Servlet Listener.flvby siva2cu510 views
9:56
HTTP client-server application - Part 1.mp4by 000haitham0004,972 views
11:20
Servlet tutorial with Access databaseby layanchathuranga718 views
5:40
Part2: How to forward to a JSP from a Portlet doView Methodby tutorials168portlets8,493 views
8:34
Creating a Simple J2EE Servlet Using IRAD for WebSphere 7.0by javaprogrammingcbts8,892 views
3:11
Portlet Application Export w Rational Application Dev IRADby tutorials168portlets1,339 views
5:15
Creating a Simple J2EE Servlet Using IRAD for WebSphereby javaprogrammingcbts4,155 views
1:54
Build HelloWorld Servlet in Eclipse(JEE version)by iridiumcao18,186 views
7:21
Part I: Portlet Edit Mode and the NumberGuesserPortletby tutorials168portlets5,772 views
- Loading more suggestions...
Thank you very helpful
engHossamGhareb 4 months ago
Heyyyyy this is cool. I dont hear any machine typing sound anymore! Coooolllllll at last I could make my speaker louder =D
MaloEdu 1 year ago
can you please keep your mouth a bit away from mic. its very irritating
tkmazzz 2 years ago