Two Laptop Bag*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Problem with web fragments Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Problem with web fragments" Watch "Problem with web fragments" New topic
Author

Problem with web fragments

Simran Dass
Ranch Hand

Joined: Jan 09, 2010
Posts: 183

Hi All,

I am new to this forum and need help with Servlet 3.0

I am facing a problem with web fragments .I cannot understand the ouput (am using Tomcat 7.0.23) . I
have read the web fragment part in servlet 3.0 spec "8.2.2 Ordering of web.xml and web-fragment.xml" but I think
I missed out on something.

My application contains 1 servlet , 2 filters ,web.xml and a web-fragment.xml.Following is the structure :--

There are two directories servlets and filters in WEB-INF/classes folder.
i) servlets directory contains a servlet - testservlet2.java and its class file .
ii) filters directory contains a filter - wf1Filter.java and its class file .


There is a jar file called frag2.jar in WEB-INF/lib folder. It contains--

i) A filter wf2Filter.java and its class file in the webfragment2 folder in WEB-INF\lib\frag2.jar.
ii) A web-fragment.xml in the WEB-INF\lib\frag2.jar\META-INF directory .


The code :-





web-fragment.xml in the META-INF folder in WEB-INF\lib\frag2.jar.
----------------------------------------------------------------------------




testservlet2.java and its class file in WEB-INF/classes/servlets folder
----------------




wf1Filter.java and its class file in WEB-INF/classes/filters folder
--------------




wf2Filter.java and its class file in the webfragment2 folder in WEB-INF\lib\frag2.jar.
-----------------------------------------------------




Output that I am getting in Tomcat 7.0.23 is :-
----------------------------------------------

Before chain.doFilter in wf1Filter
Before chain.doFilter in wf2Filter
in testservlet2
After chain.doFilter in wf2Filter
After chain.doFilter in wf1Filter

What I cannot understand is why is the wf1Filter firing first ? The web.xml has an <absolute-ordering> element which says that the
web-fragment with <name>A</name> should be executed first .So should not the wf2Filter be invoked first and then wf1Filter?
wf1Filter uses an annotation and should be processed after wf2Filter. Is not the precedence like this --> web.xml , web-fragment.xml and then
the anotations. Please help.

Please reply.

Regards
Simran



Simran Dass
Ranch Hand

Joined: Jan 09, 2010
Posts: 183


Hi

Am I supposed to post this question in the SCWCD forum. I
am not able to make progress.

Regards
Simran
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

Moving this to the appropriate forum...


SCJP, SCWCD.
|Asking Good Questions|
Eshwin Sukhdeve
Ranch Hand

Joined: Mar 15, 2012
Posts: 80
Hi Simran,
you can try with this
@WebFilter(filterName="wf1Filter")
public class wf1Filter implements Filter {}

@WebFilter(filterName="wf2Filter")
public class wf2Filter implements Filter {}

mention in your filter classes

and configure this in your web.xml file

<filter-mapping>
<filter-name>wf1Filter</filter-name>
<url-pattern>/*</filter-name>
</filter-mapping>
<filter-mapping>
<filter-name>wf2Filter</filter-name>
<url-pattern>/*</filter-name>
</filter-mapping>

I dont have much idea about web-fragment.xml ..so youi can try with..

Simran Dass
Ranch Hand

Joined: Jan 09, 2010
Posts: 183

Thanks Eshwin .My problem is web fragments.
Christian Ludt
Greenhorn

Joined: Apr 07, 2009
Posts: 11
Hi Simran

the last paragraph in 8.2.1 Modularity of web.xml says


[...] The order in which configuration from web-fragment.xml and annotations should be applied is undefined. [...]


See the first paragraph in 8.2.3 Assembling the descriptor from web.xml, web-fragment.xml and annotations, that is even more concrete.

You do not specify the wf1Filter in a web-fragment.xml, hence the order of wf1Filter (defined by annotation) and wf2Filter (defined by web-fragment.xml) is undefined. BTW, specifying the wf1Filter in the web.xml would not lead to what you are looking for, either, as web.xml must be processed before web-fragments. The only way to achieve what you want is to provide an additional jar with META-INF/web-fragment.xml and the definition of the wf1Filter:



Probably, you have to delete the annotation in your wf1Filter class; at least Glassfish has problems in that regard, and does not overwrite the annotation, but augments it, leading to

INFO: Before chain.doFilter in wf1Filter
INFO: Before chain.doFilter in wf2Filter
INFO: Before chain.doFilter in wf1Filter
INFO: in testservlet
INFO: After chain.doFilter in wf1Filter
INFO: After chain.doFilter in wf2Filter
INFO: After chain.doFilter in wf1Filter

Best regards
Christian
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Problem with web fragments