Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with web fragments

 
Simran Dass
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Hi

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

Regards
Simran
 
Amit Ghorpade
Bartender
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving this to the appropriate forum...
 
Eshwin Sukhdeve
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thanks Eshwin .My problem is web fragments.
 
Christian Ludt
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic