Books
in black and white
Main menu
Share a book About us Home
Books
Biology Business Chemistry Computers Culture Economics Fiction Games Guide History Management Mathematical Medicine Mental Fitnes Physics Psychology Scince Sport Technics
Ads

More Java Pitfalls Share Reactor - Daconta M,C.

Daconta M,C. More Java Pitfalls Share Reactor - Wiley publishing, 2003. - 476 p.
ISBN: 0-471-23751-5
Download (direct link): morejavapitfallssharereactor2003.pdf
Previous << 1 .. 87 88 89 90 91 92 < 93 > 94 95 96 97 98 99 .. 166 >> Next

In Listing 28.4, lines 04 and 17 attempt to use the Logger class that is part of the J2SDK 1.4 implementation but renders a compile-time error (reference to Filter is ambiguous) because the javax.servlet.Filter class conflicts with the java .util.logging.Filter class.
01 package 8 2 m e t i s l l a f t i p a v a j g. r o
02
03 import java.io.*;
04 import java.util.*;
05 import javax.servlet.*;
06 import javax.servlet.http.*;
07
08 public final class ControllerFilter extends HttpServlet
09 implements Filter {
10
11 private ServletContext ctx;
12
Listing 28.5 ControllerFilter.java (continued)
252 Item 28
13 public void doFilter(ServletRequest req,
14 ServletResponse res,
15 FilterChain chain)
16 throws IOException, ServletException {
17
18 PrintWriter out = res.getWriter();
19 Wrapper wrapper = new Wrapper((HttpServletResponse)res);
20 chain.doFilter(req, wrapper);
21 String filename = req.getParameter("filename");
22 String wts = wrapper.toString();
23
24 if (filename != null && filename.startsWith("html")) {
25 StringBuffer sb = new StringBuffer();
26 sb.append("<html><body>");
27 sb.append(wts);
28 sb.append("</body></html>");
29 res.setContentLength(sb.toString().length());
30 out.write(sb.toString());
31 } else {
32 out.write(wrapper.toString());
33 }
34 out.close();
35 }
36
37 public void destroy() {}
38 public void init(FilterConfig config) throws ServletException {
39 ctx = config.getServletContext();
40 }
41 }
42
Listing 28.5 (continued)
Because the filters' applications are run sequentially prior to the invocation of the requested servlet, the doFilter() method of XSLTFilter will be run prior to the same method in the ControllerFilter application. The invocation sequence can be seen in Listing 28.6 on lines 7 and 13.
01: <?xml version="1.0" encoding="ISO-8859-1"?>
02: <!DOCTYPE web-app
03: PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
04: "http://java.sun.com/dtd/web-app_2_3.dtd">
05: <web-app>
06: <filter>
07: <filter-name>XSLTranformFilter</filter-name>
Listing 28.6 Web.xml
Problems with Filters 253
08 <filter-class>
09 org.javapitfalls.item28.XSLTransformFilter
10 </filter-class>
11 </filter>
12 <filter>
13 <filter-name>ControllerFilter</filter-name>
14 <filter-class>
15 org.javapitfalls.item28.ControllerFilter
16 </filter-class>
17 </filter>
18 <filter-mapping>
19 <filter-name>XSLTranformFilter</filter-name>
20 <servlet-name>FilteredFileServlet</servlet-name>
21 </filter-mapping>
22 <filter-mapping>
23 <filter-name>ControllerFilter</filter-name>
24 <url-pattern>/ControllerFilter</url-pattern>
25 </filter-mapping>
26 <servlet>
27 <servlet-name>FilteredFileServlet</servlet-name>
28 <servlet-class>
29 org.javapitfalls.item28.FileServlet
30 </servlet-class>
31 <init-param>
32 <param-name>filename</param-name>
33 <param-value>/data/states.xml</param-value>
34 </init-param>
35 </servlet>
36 <servlet>
37 <servlet-name>Page1</servlet-name>
38 <jsp-file>/test1/jsp/first.jsp</jsp-file>
39 </servlet>
40
41 <!--- second.jsp, third.jsp, fourth.jsp excluded -- >
42
56 <servlet-mapping>
57 <servlet-name>FilteredFileServlet</servlet-name>
58 <url-pattern>/FileServlet</url-pattern>
59 </servlet-mapping>
60
61 </web-app>
62
Listing 28.6 (continued)
With respect to the Filter environment, initialization parameters can be associated with a filter using the init-params element in the deployment descriptor (web.xml). The names and values of those parameters are available to the filter at runtime using the getlnitParameter and getlnitParameterNames methods.
254 Item 28
01 <?xml version="1.0" ?>
02 <xsl:stylesheet version="1.0"
03 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
04
05 <xsl:output method="html"/>
06 <xsl:template match="/">
07 <xsl:choose>
08 <xsl:when test="//state">
09
10 <table border="0" width="100% ">
11
12 <tr>
13 <td bgcolor="#dcdcff" width= '25%">Name</td>
14 <td bgcolor="#dcdcff" width= '25%">Flower</td>
15 <td bgcolor="#dcdcff" width= '25%">Bird</td>
16 <td bgcolor="#dcdcff" width= '25%">Capital</td>
17 </tr>
18
19 <xsl:for-each select="//state ">
20 <xsl:if test="position() mod > 0 = 2
21 <tr>
22 <td bgcolor="#eeeeee" width= '25%">
23 <xsl:value-of select="name '/></td>
24 <td bgcolor="#eeeeee" width= '25%">
25 <xsl:value-of select="flower"/></td>
26 <td bgcolor="#eeeeee" width= '25%">
27 <xsl:value-of select="bird > t d V
28 <td bgcolor="#eeeeee" width= "25%">
29 <xsl:value-of select="capital"/></td>
30 </tr>
31 </xsl:if>
32 <xsl:if test="position() mod 2 = 0">
33 <tr>
34 <td width="25%"><xsl:value-of select="name"/></td>
35 <td width="25%"><xsl:value-of select="flower"/></td>
36 <td width="25%"><xsl:value-of select="bird"/></td>
37 <td width="25%"><xsl:value-of select="capital"/></td>
38 </tr>
39 </xsl:if>
40 </xsl:for-each>
41
42 </table>
43
44 </xsl:when>
45 </xsl:choose>
Previous << 1 .. 87 88 89 90 91 92 < 93 > 94 95 96 97 98 99 .. 166 >> Next