Solved

servlet filter

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
designing in object programming 12 94
Tomcat: Unable to run tomcat service. 2 24
Desingning Refactoring existing code 2 26
java imports not found 4 30
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

820 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