This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Little Book of Impediments (e-book only) and have Tom Perry on-line!
See this thread for details.
Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Q: Filter on Page 668 and page 695

 
Romy Huang
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1589
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 200
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Romy Huang
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Romy Huang
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Er, that *is* the test on page 680!
 
Nicholas Cheung
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Roger Yates
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic