Solved

servlet filter

Posted on 2014-04-07
5
438 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
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.
Suggested Courses

627 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