Magazine
 
Tips & Tricks
 

responses to transform or use the request and response information. Filters are like preprocessors of the request before sending the request to servlet and postprocessor of the response before sending the response back to the client. Filters allow to take control logic out of servlets and putting this logic in more reusable pieces of code. Request filters can be used to audit or log requests, reformat request headers or perform security checks etc.
Response filters can be used to create a different response, compress the response stream, append or alter the response stream etc.

The example given below demonstrates how to use filter to check the password input by the user before passing the request to the requested servlet. If the user passes incorrect password then it displays the page informing
the incorrect password. If it is correct, then it passes the control to the desired servlet and does its job, then it returns back to the filter and log some information.

The login.html displays the page to the user to fill the user name and password.

login.html

<!DOCTYPE HTML PUBLIC “-//W3C//DTD
HTML 4.0 Transitional//EN”>
<HTML>
<BODY>
<form action=”/javajazzup/WelcomeServlet”>
<table>
<tr>
<td>User Name</td>
<td>
<input type=”text” name=”username”/>
</td>
</tr>
<tr>
<td>Password</td>
<td><input type=”password”
name=”password”/></td>
</tr>
<tr>
<td></td>
<td><input type=”submit”
value=”Submit”/></td>
</tr>
</table>
</form>
</BODY>
</HTML>

 

 

Create the filter “MyFilter” to be processed before processing the servlet “WelcomeServlet”. Like servlets, Filters have their own API. The filter class implements Filter interface as servlet implements Servlet interface. Filters can get access to the servlet context and can be linked to other filters. Like servlets, filters also have life cycle and has init() and destroy() methods and like servlet’s doGet() or doPost() methods filters also has doFilter() method. Like servlet, Filters are declared in deployment descriptor (web.xml file). A web application can have lots of filters and a given request can cause more than one filter to execute.

MyFilter.java

import java.util.*;
import java.io.*;
import javax.servlet.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class MyFilter implements Filter {
private FilterConfig filterConfig = null;
public void init(FilterConfig filterConfig) {
this.filterConfig = filterConfig;
}
public void doFilter(ServletRequest req,
ServletResponse res, FilterChain chain)
throws IOException, ServletException {
// Get the password entered by the user.

Dec 2007 | Java Jazz Up | 76
previous
index
next
 
View All Topics
All Pages of this Issue
Pages: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,

30
, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53 , 54, 55, 56, 57,

58
, 59, 60, 61, 62, 63 , 64, 65 , 66 , 67 , 68 , 69 , 70 , 71 , 72 , 73 , 74 , 75 , 76 , 77 , 78 , 79 , 80 , 81 , 82 ,

Download PDF