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 .. 86 87 88 89 90 91 < 92 > 93 94 95 96 97 98 .. 166 >> Next

28 File(getServletContext().getRealPath(filename));
29 BufferedReader reader =
30 new BufferedReader(new FileReader(file));
31 while(reader.ready()) out.println(reader.readLine());
32 } catch (Exception e){
33 out.println("ERROR: " + e.toString());
Listing 28.3 FileServlet.java
Problems with Filters 249
34 }
35 out.close();
36 }
37
38 public void init() throws ServletException {
39 filename = getInitParameter("filename");
40 }
41
42 }
43
Listing 28.3 (continued)
The Web display in Figure 28.2 is the result of the XSL stylesheet displayed in Listing 28.7 called htmlStates.xsl being applied to the xml.states file. A different view would be rendered if the alternative sample stylesheet xmlState.xsl was specified.
The XSLTransformFilter application in Listing 28.4 performs a transformation on the states.xml file with the user-specified XSLstylesheet in the filename parameter in the URL request.
3http://localhost:8080/filters^FileServlet?filename=htmlStates.xsl - Microsoft Internet Explorer provided by ^Home
File Edit View Favorites Tools Help
Back ' (J Search Favorites ^History | -)" j 21' _] ^ ^
Address |C] hpV/localhoa:8080/Mters^FiteServtet?flerwme=titmlStatetxsJ w| ^Go j Links **
Go gle-| | ^ Seaich Web ^ , d]Up >Higl
1 A
Name Flower Bird Capital
Alabama Camellia Yellowhammer Montgomery
Alaska Forget Me Not Willow Ptarmigan Juneau
Arizona Saguaro Cactus Blossom Cactus Wren Phoenix
Arkansas Apple Blossom Mockingbird Little Rock
California California Poppy California Valley Quail Sacramento
Colorado Rocky Mtn Columbine Lark Bunting Denver
Connecticut Montam Laurel Robin Hartford
Delaware Peach Blossom Blue Hen Chicken Dover
Florida Orange Blossom Mockingbird Tallahassee
Georgia Cherokee Rose Brown Thrasher Atlanta
Hawaii Pua Aloalo Nene Honolulu
Idaho Syringa - Mock Orange Mountain Bluebird Boise
Illinois Purple Violet Cardinal Sprmgfield
Indiana Peony Cardinal Indianapolis
Iowa Wild Prane Rose Eastern Goldfinch Des Moines
Kansas Sunflower Western Meadowlark Topeka
?* pr*nlrfiir+ jd
Done ^ Local intranet
#1 Start| 'J) <gi 3 g) images! .. ||#lhttp:., . aaCom. | Com | 20Tomcat| 9 11AM
Figure 28.2 XSLT output.
250 Item 28
01 package org.javapitfalls.item28;
02
03 import java.io.*;
04 //import java.util.logging.*;
05 import javax.servlet.*;
06 import javax.servlet.http.*;
07 import javax.xml.transform.*;
08 import javax.xml.transform.stream.*;
09
10 public class XSLTransformFilter implements Filter {
11
12 private FilterConfig filterConfig = null;
13 // Reference below renders a compile-time error [reference to
14 // Filter is ambiguous] because javax.servlet.Filter and
15 // java.util.logging.Filter conflict
16 // private static java.util.logging.Logger logger =
17 // java.util.logging.Logger.getLogger(XSLTFilter.class.getName());
18
19 public void doFilter(ServletRequest request,
20 ServletResponse response, FilterChain chain)
21 throws IOException, ServletException {
22
23 // commented out because of Filter conflict
24 // logger.setLevel(Level.ALL);
25 // logger.info("[XSLTFilter]");
26
27 String filename = request.getParameter("filename");
28 String contentType = "text/html";
29 String styleSheet = "/data/" + filename;
30 if (filename.startsWith("xml"))
31 contentType = "text/plain";
32
33 response.setContentType(contentType);
34 String stylePath = filterConfig.getServletContext().
35 getRealPath(styleSheet) ;
36 Source styleSource = new StreamSource(stylePath);
37 PrintWriter out = response.getWriter();
38 Wrapper wrapper = new Wrapper((HttpServletResponse)response) ;
39
40 chain.doFilter(request, wrapper);
41
42 StringReader sr = new StringReader(wrapper.toString());
43
44 StreamSource xmlSource = new StreamSource(
filterConfig.getServletContext().getRealPath("/data/states.xml") );
45
46 try {
47 TransformerFactory transformerFactory = TransformerFactory.newInstance(); 2
Listing 28.4 XSL TransformFilter.java
Problems with Filters 251
48 Transformer transformer =
49 transformerFactory.newTransformer(styleSource);
50 ByteArrayOutputStream baos = new ByteArrayOutputStream();
51 StreamResult result = new StreamResult(baos);
52 transformer.transform(xmlSource, result);
53 response.setContentLength(baos.toString().length());
54 out.write(baos.toString());
55
56 } catch(Exception ex) {
57 out.println(ex.toString());
58 out.write(wrapper.toString());
59 }
60 }
61 public void init(FilterConfig filterConfig) {
62 this.filterConfig = filterConfig;
63 }
64 public void destroy(){
65 this.filterConfig = null;
66 }
67 }
68
Listing 28.4 (continued)
The filter's FilterConfig object on line 61 has access to the ServletContext of the Web application, so the container can pass information to the application and state information can be stored.
Previous << 1 .. 86 87 88 89 90 91 < 92 > 93 94 95 96 97 98 .. 166 >> Next