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 .. 105 106 107 108 109 110 < 111 > 112 113 114 115 116 117 .. 166 >> Next

01 public class OKReadingServlet extends HttpServlet {
02 private static final String CONTENT_TYPE = "text/html";
03 private StringBuffer strBuf;
04 private String header;
05
06 /**Initialize global variables*/
07 public void init(ServletConfig config) throws ServletException {
08 super.init(config);
09
10 ServletContext context = config.getServletContext();
11
12 //Explicit specification of the path via file property
13 String configFile = config.getInitParameter("Config-File");
14
15 strBuf = new StringBuffer();
16 try {
17
18 BufferedReader bufRead = new BufferedReader(new ^
FileReader(configFile));
19
20 while (bufRead.read() != -1){
21 strBuf.append(bufRead.readLine());
22 }
23
24 bufRead.close();
25
26 } catch (IOException ioe) {
27 ioe.printStackTrace();
28 };
29
30 header = strBuf.toString();
33 }
Listing 35.3 OKReadingServlet.java
306 Item 35
To alleviate this issue, the servlet specification adopted the paradigm first introduced in the Applet. As shown in Listing 35.4, the solution is to call getResource(String path), which returns a URL object of the file at the path—a relative URL to the file. This methodology coincides with the way applets retrieve resources from a JAR file.
01 public class GoodReadingServlet extends HttpServlet {
02 private static final String CONTENT_TYPE = "text/html";
03 private StringBuffer strBuf;
04 private String header;
05
06 /**Initialize global variables*/
07 public void init(ServletConfig config) throws ServletException {
08 super.init(config);
09
10 ServletContext context = config.getServletContext();
11
12 strBuf = new StringBuffer();
13 try {
14 // Using an init parameter
15 URL headerURL = context.getResource(config ^
.getInitParameter("header"));
16 // Getting the Content directly
17 strBuf.append(headerURL.getContent());
18
19 } catch (IOException ioe) {
20 ioe.printStackTrace();
21 };
22
23 header = strBuf.toString();
24
25 }
Listing 35.4 GoodReadingServletjava
Notice how this allows the user to avoid having to create a reader and iterate through the reader to populate the header string. When the purpose is merely to read the file into a variable, the getResource() method provides the most direct way.
However, often the developer wants to process the file, not just load its content into a variable. To perform this function, the developer has a better way: call getResourceAsStream(). This method, shown in Listing 35.5, returns an Input-Stream, which can be enclosed in whatever reader the developer needs to handle the processing.
Reading Files from Servlets 307
01 public class AnotherGoodReadingServlet extends HttpServlet {
02 private static final String CONTENT_TYPE = "text/html";
03 private StringBuffer strBuf;
04 private String header;
05
06 /**Initialize global variables*/
07 public void init(ServletConfig config) throws ServletException {
08 super.init(config);
09
10 ServletContext context = config.getServletContext();
11
12 strBuf = new StringBuffer();
13 try {
14 // Using an init parameter
15 BufferedReader bufRead = new BufferedReader(
16 new InputStreamReader( 2
17 context.getResourceAsStream(
config.getInitParameter("header"))));
18
19 //line by line reading allows for any additional processing 2
to occur
20 while (bufRead.read() != -1){
21 strBuf.append(bufRead.readLine());
22 }
23
24 bufRead.close();
28 } catch (IOException ioe) {
29 ioe.printStackTrace();
30 };
31
32 header = strBuf.toString();
34 }
Listing 35.5 AnotherGoodReadingServlet.java
Good software development practice demands avoiding the use of hard-coded configuration information. Because of this, strong consideration should be given to placing the information in the servlet engine's Web Application Deployment Descriptor.
This pitfall has introduced the concept of the Web Application Archive and briefly mentioned the Web Application Deployment Descriptor. Now we will discuss the Web Application Deployment Descriptor in greater detail.
308 Item 35
Web Application Deployment Descriptors
A primary part of a Web application is its Web Application Deployment Descriptor. This is euphemistically known as the "web.xml", as it is stored in a file by that name. Also, the file is stored in the mywebapp/WEB-INF directory.
Listing 35.6 is an annotated version of a Web Application Deployment Descriptor for a common Web application. See the comments in Listing 35.6 for an explanation of each part.
001 <?xml version="1.0"?>
002 <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web
Application 2.3//EN" "http://java.sun.com/j2ee/dtds/web-app_2_3 .dtd">
003 <web-app>
004
005 <!--
006 Basic Web Application Description information. Mostly used for 2
Tools to display the web app in a more user friendly manner.
007 -- >
008
009 <display-name>example</display-name>
010 <description>My example web application</description>
Previous << 1 .. 105 106 107 108 109 110 < 111 > 112 113 114 115 116 117 .. 166 >> Next