Java Filter Problem Solution

           A filter is an object that is used to perform filtering tasks such as conversion, log maintain, compression, encryption and decryption, input validation etc. I'm not going to discuss about Java filter tutorial or javax filter tutorial here, there are many sites and books provide good tutorial for how to use java servlet filter in application.
           One common problem to exclude css, javaScript or any image and icon content with Java Filter in Java Web Application. Its a particular issue when during authentication via filter in java web application. Here I explain how to out from problem with java filter in java web application.

For example if we want to exclude css which in use with before authentication or home page accessible without even being logged in, we can add below code to resolve the issue.
 if(uri.endsWith("css")){  
       this.context.log("Unauthorized access request");  
       res.sendRedirect("Home.html");  
     }else{  
       // pass the request along the filter chain  
       chain.doFilter(request, response);  
     }  
Do same as with javaScript or jQuery file
 if(uri.endsWith("js")){  
       this.context.log("Unauthorized access request");  
       res.sendRedirect("Home.html");  
     }else{  
       // pass the request along the filter chain  
       chain.doFilter(request, response);  
     }  
 we can also use regular expressions or regex
 if(uri.matches(".*(css|js|jpg|jpeg|png)")){  
       this.context.log("Unauthorized access request");  
       res.sendRedirect("Home.html");  
     }else{  
       // pass the request along the filter chain  
       chain.doFilter(request, response);  
     }  

So this is the easy way to make pages or contents becomes accessible without even being logged in with Java Filter in Java Web application
If anyone have any query or problem then free to ask via comment.
Read More...