Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

servlet filter

Posted on 2014-04-07
5
Medium Priority
?
450 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 668 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 664 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 28

Accepted Solution

by:
rrz earned 668 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

722 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