Solved

servlet filter

Posted on 2014-04-07
5
428 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 19

Assisted Solution

by:Ken Butters
Ken Butters earned 167 total points
ID: 39986565
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
ID: 39988306
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
ID: 40015898
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
ID: 40016512
yes
0
 
LVL 27

Accepted Solution

by:
rrz earned 167 total points
ID: 40016687
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

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…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

752 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