This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Q: Filter on Page 668 and page 695 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Q: Filter on Page 668 and page 695" Watch "Q: Filter on Page 668 and page 695" New topic
Author

Q: Filter on Page 668 and page 695

Romy Huang
Ranch Hand

Joined: Sep 30, 2003
Posts: 35
Filter with matching URL pattern are placed in the chain in the order in which they are declared in the DD. right?

Page 695 for page 668's answer 2,3
2. 1,2,5 instead of 1,5,2
3. 1,4,5 instead of 1,5,4

right?


Thanks in advance!
[ October 19, 2004: Message edited by: Romy Huang ]
Kathy Sierra
Cowgirl and Author
Ranch Hand

Joined: Oct 10, 2002
Posts: 1572
I think the answers in the book *are* correct, but the rules are not as simple as "the order in which they're declared in the DD". It's confusing, for sure, and just about everybody gets caught on this one.

Look at page 678 and see if that changes your answer

Remember that there is a difference between URL pattern matches and <servlet-name> pattern matches.

See if that helps,
cheers,
Kathy
Romy Huang
Ranch Hand

Joined: Sep 30, 2003
Posts: 35
Thank Kathy.

I have write a little programming to test it. The resulte is 1,2,5 for the second. I am confused? any help is appreciated. Thanks.

Filter1:
package foo;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

public class Filter1 implements Filter
{
private FilterConfig filterConfig = null;

public void init(FilterConfig filterConfig) throws ServletException {

this.filterConfig = filterConfig;

}

public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)
throws IOException, ServletException
{
ArrayList item = new ArrayList();

String fitem = new String("first");

item.add(fitem);

request.setAttribute("filteritem", item);

// Pass control on to the next filter
chain.doFilter(request, response);
}

public void destroy() {
this.filterConfig = null;
}
}

Filter2:
package foo;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

public class Filter2 implements Filter
{
private FilterConfig filterConfig = null;

public void init(FilterConfig filterConfig) throws ServletException {

this.filterConfig = filterConfig;

}

public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)
throws IOException, ServletException
{
ArrayList item = (ArrayList)request.getAttribute("filteritem");

String fitem = new String("Secnond");

item.add(fitem);

request.setAttribute("filteritem", item);

// Pass control on to the next filter
chain.doFilter(request, response);
}

public void destroy() {

this.filterConfig = null;

}

}

Filter3:
package foo;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

public class Filter3 implements Filter
{
private FilterConfig filterConfig = null;

public void init(FilterConfig filterConfig) throws ServletException {

this.filterConfig = filterConfig;

}

public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)
throws IOException, ServletException
{
ArrayList item = (ArrayList)request.getAttribute("filteritem");

String fitem = new String("Third");

item.add(fitem);

request.setAttribute("filteritem", item);

// Pass control on to the next filter
chain.doFilter(request, response);
}

public void destroy() {

this.filterConfig = null;

}

}

Filter4:
package foo;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

public class Filter4 implements Filter
{
private FilterConfig filterConfig = null;

public void init(FilterConfig filterConfig) throws ServletException {

this.filterConfig = filterConfig;

}

public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)
throws IOException, ServletException
{
ArrayList item = (ArrayList)request.getAttribute("filteritem");

String fitem = new String("Fourth");

item.add(fitem);

request.setAttribute("filteritem", item);

// Pass control on to the next filter
chain.doFilter(request, response);
}

public void destroy() {

this.filterConfig = null;

}

}

Filter5:
package foo;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

public class Filter5 implements Filter
{
private FilterConfig filterConfig = null;

public void init(FilterConfig filterConfig) throws ServletException {

this.filterConfig = filterConfig;

}

public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)
throws IOException, ServletException
{
ArrayList item = (ArrayList)request.getAttribute("filteritem");

String fitem = new String("Fifth");

item.add(fitem);

request.setAttribute("filteritem", item);

// Pass control on to the next filter
chain.doFilter(request, response);
}

public void destroy() {

this.filterConfig = null;

}

}

Test Servlet:

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;

public class Ch13Servlet extends HttpServlet
{
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException
{
ArrayList item = (ArrayList)request.getAttribute("filteritem");
PrintWriter pw = response.getWriter();
java.util.Date today=new java.util.Date();
pw.println("<html>");
pw.println("<head>");
pw.println("</head>");
pw.println("<body>");
pw.println("<h1 align=center>HF\'s Chapter13 Servlet</h1>");
pw.println("<br>"+item);
pw.println("</body>");
pw.println("</html>");
}
}

web.xml:

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2eeweb-app_2_4.xsd"
version="2.4">

<filter>
<filter-name>Filter1</filter-name>
<filter-class>foo.Filter1</filter-class>
</filter>

<filter>
<filter-name>Filter2</filter-name>
<filter-class>foo.Filter2</filter-class>
</filter>

<filter>
<filter-name>Filter3</filter-name>
<filter-class>foo.Filter3</filter-class>
</filter>

<filter>
<filter-name>Filter4</filter-name>
<filter-class>foo.Filter4</filter-class>
</filter>

<filter>
<filter-name>Filter5</filter-name>
<filter-class>foo.Filter5</filter-class>
</filter>

<filter-mapping>
<filter-name>Filter1</filter-name>
<url-pattern>/Recipes/*</url-pattern>
</filter-mapping>

<filter-mapping>
<filter-name>Filter2</filter-name>
<url-pattern>/Recipes/HopsList.do</url-pattern>
</filter-mapping>

<filter-mapping>
<filter-name>Filter3</filter-name>
<url-pattern>/Recipes/Add/*</url-pattern>
</filter-mapping>

<filter-mapping>
<filter-name>Filter4</filter-name>
<url-pattern>/Recipes/Modify/ModRecipes.do</url-pattern>
</filter-mapping>

<filter-mapping>
<filter-name>Filter5</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<servlet>
<servlet-name>HelloWorldExample</servlet-name>
<servlet-class>Ch13Servlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>HelloWorldExample</servlet-name>
<url-pattern>/Recipes/HopsList.do</url-pattern>
</servlet-mapping>
</web-app>


Test link: http://localhost:8080/ch12/Recipes/HopsList.do

ch12 is my web app.

Thanks.

[ October 20, 2004: Message edited by: Romy Huang ]

[ October 20, 2004: Message edited by: Romy Huang ]
[ October 20, 2004: Message edited by: Romy Huang ]
Colin Fletcher
Ranch Hand

Joined: Sep 10, 2004
Posts: 200
From the "IMPORTANT: The Container's rules for ordering filters:" at the bottom of pg 678:


1) ALL filters with matching URL patterns are located first. ...


Hope this helps.


SCJP 1.4 SCWCD 1.4
Romy Huang
Ranch Hand

Joined: Sep 30, 2003
Posts: 35
quote:
--------------------------------------------------------------------------------

1) ALL filters with matching URL patterns are located first. ...

--------------------------------------------------------------------------------


In page 678, it also states, all filters that march will be place in chain!! Filter with marching URL patterns are placed in the chain in the order in which they are declared in the DD.

In the servlet Spec. 2.4 page 54,55 also state same thing.

Also, you can test the programming I wrote. It is not good programming, but the result is 1,2,5 for second question.

Thanks.
[ October 20, 2004: Message edited by: Romy Huang ]
Roger Yates
Ranch Hand

Joined: Aug 31, 2004
Posts: 118
Romy,

I think I see your problem. You are currently testing *all* url-pattern matching. The HFS example has a mix of url-patterns and servlet names, which causes the difference.

Your DD states:
<filter-mapping>
<filter-name>Filter2</filter-name>
<url-pattern>/Recipes/HopsList.do</url-pattern>
</filter-mapping>

<filter-mapping>
<filter-name>Filter3</filter-name>
<url-pattern>/Recipes/Add/*</url-pattern>
</filter-mapping>

<filter-mapping>
<filter-name>Filter4</filter-name>
<url-pattern>/Recipes/Modify/ModRecipes.do</url-pattern>
</filter-mapping>


Change it as follows and see what results you get:
<filter-mapping>
<filter-name>Filter2</filter-name>
<servlet-name>/Recipes/HopsList.do</url-pattern>
</filter-mapping>

<filter-mapping>
<filter-name>Filter3</filter-name>
<url-pattern>/Recipes/Add/*</url-pattern>
</filter-mapping>

<filter-mapping>
<filter-name>Filter4</filter-name>
<servlet-name>/Recipes/Modify/ModRecipes.do</url-pattern>
</filter-mapping>


HTH


Cheers,<br />Roger<br />SCJP 1.2, 1.4, SCBCD 1.3, SCWCD 1.4
Romy Huang
Ranch Hand

Joined: Sep 30, 2003
Posts: 35
quote:
--------------------------------------------------------------------------------
<filter-mapping>
<filter-name>Filter2</filter-name>
<servlet-name>/Recipes/HopsList.do</url-pattern>
</filter-mapping>

<filter-mapping>
<filter-name>Filter3</filter-name>
<url-pattern>/Recipes/Add/*</url-pattern>
</filter-mapping>

<filter-mapping>
<filter-name>Filter4</filter-name>
<servlet-name>/Recipes/Modify/ModRecipes.do</url-pattern>
</filter-mapping>
----------------------------------------------------------------------------

if changing it above, I know Filter2 will come after filter5 because it will following the rule 2 in page 678, first marching url-pattern, then matching servlet-name.

in my case, it is exact the test in page 680.
[ October 20, 2004: Message edited by: Romy Huang ]
Roger Yates
Ranch Hand

Joined: Aug 31, 2004
Posts: 118
Er, that *is* the test on page 680!
Nicholas Cheung
Ranch Hand

Joined: Nov 07, 2003
Posts: 4982
For the pattern issues, if you got SCWCD Exam Study Kit as well, you may refer to my post before:
http://www.coderanch.com/t/168369/java-Web-Component-SCWCD/certification/Filter-execution-priority
to get more information.

Nick


SCJP 1.2, OCP 9i DBA, SCWCD 1.3, SCJP 1.4 (SAI), SCJD 1.4, SCWCD 1.4 (Beta), ICED (IBM 287, IBM 484, IBM 486), SCMAD 1.0 (Beta), SCBCD 1.3, ICSD (IBM 288), ICDBA (IBM 700, IBM 701), SCDJWS, ICSD (IBM 348), OCP 10g DBA (Beta), SCJP 5.0 (Beta), SCJA 1.0 (Beta), MCP(70-270), SCBCD 5.0 (Beta), SCJP 6.0, SCEA for JEE5 (in progress)
Roger Yates
Ranch Hand

Joined: Aug 31, 2004
Posts: 118
Hmmm. Thinking further on this, I think the book may be misleading. I'm not sure how valid an entry like the following is:

<filter-mapping>
<filter-name>Filter4</filter-name>
<servlet-name>/Recipes/Modify/ModRecipes.do</servlet-name>
</filter-mapping>


Since the servlet-name doesn't tell you what requests would be filtered - unless this is the authors' shorthand for:

<filter-mapping>
<filter-name>Filter4</filter-name>
<servlet-name>MyServlet</servlet-name>
</filter-mapping>

<servlet>
<servlet-name>MyServlet</servlet-name>
<servlet-class>foo.MyFilterClass</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/Recipes/Modify/ModRecipes.do</url-pattern>
</servlet-mapping>


Kathy...?
Romy Huang
Ranch Hand

Joined: Sep 30, 2003
Posts: 35
quote:
--------------------------------------------------------------------------------
<filter-mapping>
<filter-name>Filter4</filter-name>
<servlet-name>/Recipes/Modify/ModRecipes.do</servlet-name>
</filter-mapping>
--------------------------------------------------------------------------------


Roger, you are right, I didn't pay attention on what you write before. It should be servlet name, not url-patter here.

But my question is not for this. Would you please check the page 680 for the question 2. what answer you get from that?
Romy Huang
Ranch Hand

Joined: Sep 30, 2003
Posts: 35
quote:
--------------------------------------------------------------------------------
<servlet>
<servlet-name>MyServlet</servlet-name>
<servlet-class>foo.MyFilterClass</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/Recipes/Modify/ModRecipes.do</url-pattern>
</servlet-mapping>

--------------------------------------------------------------------------------


Roger: and also you should put servlet class on the classes directory, not under fo directory if you want to define DD like this. you will find error on this DD.
Roger Yates
Ranch Hand

Joined: Aug 31, 2004
Posts: 118
Romy,

In the test on p680, two of the filter mappings are servlet-name mappings, hence these will get processed last in the chain.
Did you notice them?
Romy Huang
Ranch Hand

Joined: Sep 30, 2003
Posts: 35
Originally posted by Roger Yates:
Romy,

In the test on p680, two of the filter mappings are servlet-name mappings, hence these will get processed last in the chain.
Did you notice them?


Sorry, I didn't. Now I know why the answer is that. But the servlet-name?
Roger Yates
Ranch Hand

Joined: Aug 31, 2004
Posts: 118
Quite!
Hence my post of:
Hmmm. Thinking further on this, I think the book may be misleading. I'm not sure how valid an entry like the following is:


So it doesn't get mixed up with the rest of this topic, I've started a new topic for just that point!
 
GeeCON Prague 2014
 
subject: Q: Filter on Page 668 and page 695