Solved

servlet filter

Posted on 2014-04-07
5
395 Views
Last Modified: 2014-04-29
Hi,

I am going thorugh below example

http://www.javatpoint.com/servlet-filter


When i click on Index.html link i was lead to below url
http://localhost:8080/Filter/servlet1
with


message diplayed as below on the page(or browser? what is difference)

filter is invoked before
welcome to servlet
filter is invoked after


I wonder what below line do

chain.doFilter(req, resp);

please advise
Any links resources ideas highly appreciated. Thanks in advance
0
Comment
Question by:gudii9
5 Comments
 
LVL 19

Assisted Solution

by:Ken Butters
Ken Butters earned 167 total points
Comment Utility
message displayed as below on the page(or browser? what is difference)

filter is invoked before
welcome to servlet
filter is invoked after

The purpose of these lines... is to demonstrate that with a filter you can take actions or do something both before and after a servlet is executed.... in their example they have this code:

public void doFilter(ServletRequest req, ServletResponse resp,  
    FilterChain chain) throws IOException, ServletException {  
          
    PrintWriter out=resp.getWriter();  
    out.print("filter is invoked before");  
          
    chain.doFilter(req, resp);//sends request to next resource  
          
    out.print("filter is invoked after");  
    }  
    public void destroy() {}  
}  

Open in new window


out.print("filter is invoked before");   shows the timing of execution... that the println is executed Prior to the servlet.


out.print("filter is invoked after");     shows the timing of execution... that the println is executed Prior after execution of the servlet.

I wonder what below line do

chain.doFilter(req, resp);

From the website you referenced:

The object of FilterChain is responsible to invoke the next filter or resource in the chain.This object is passed in the doFilter method of Filter interface.The FilterChain interface contains only one method:

public void doFilter(HttpServletRequest request, HttpServletResponse response): it passes the control to the next filter or resource.
0
 
LVL 20

Assisted Solution

by:Sathish David Kumar N
Sathish David  Kumar N earned 166 total points
Comment Utility
In Web.xml

your metion that Filter and filter mappling details .

So that willl to ur configuration java class which web.xml page
0
 
LVL 7

Author Comment

by:gudii9
Comment Utility
I see
<filter>  
<filter-name>f1</filter-name>  
<filter-class>MyFilter</filter-class>  
</filter>  
   
<filter-mapping>  
<filter-name>f1</filter-name>  
<url-pattern>/servlet1</url-pattern>  
</filter-mapping>

Filter is configured for servlet1 so

chain.doFilter(req, resp);

leads to servlet1 right. please advise
0
 
LVL 20

Expert Comment

by:chaitu chaitu
Comment Utility
yes
0
 
LVL 27

Accepted Solution

by:
rrz earned 167 total points
Comment Utility
Look at illustrations for an explanation.
Look at  figure 4-1  at
http://sqltech.cl/doc/oas10gR31/web.1013/b28959/filters.htm
Look at figure 3 and 4  at
http://www.onjava.com/pub/a/onjava/2001/05/10/servlet_filters.html?page=2  
You can see the links of the chain(FilterChain). Each link decides whether to pass to next link or send the response to the client.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now