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 .. 101 102 103 104 105 106 < 107 > 108 109 110 111 112 113 .. 166 >> Next

07 /* Bad Voter Servlet Example */
08 public class BadVoterServlet extends HttpServlet
09 {
10
11 public void doGet(HttpServletRequest request,
12 HttpServletResponse response)
13 throws IOException, ServletException
14 {
15 doPost(request,response);
16 }
17
18
19 public void doPost(HttpServletRequest request,
20 HttpServletResponse response)
21 throws IOException, ServletException
22 {
23 String vote = request.getParameter("vote");
24
25 PrintWriter out = response.getWriter();
26
27 VoterApp voter = VoterApp.getInstance();
28
29 if ( vote == null )
30 {
31 //Letís print out the Poll of the Day!
32 response.setContentType("text/html");
33 out.println("<TITLE>Poll of the Day!</TITLE>");
34 out.println("<FORM METHOD=íPOSTí ACTION=í" +
35 request.getRequestURI() + "'>");
36
37 out.println(voter.getPollOfTheDay());
38
39 out.println("<INPUT TYPE=íSUBMITí VALUE=íVote Now!í>");
40 out.println("</FORM>");
41 }
Listing 33.1 BadVoterServlet.java
Attempting to Use Both Output Mechanisms in Servlets 293
42 else
43 {
44 //Have our voter object tally up the results
45 voter.addToPollResults(vote);
46
47 //Get the generated poll results graph
48 byte[] generatedGraph = voter.generateImageBytes();
49 if ( generatedGraph == null )
50 {
51 response.setContentType("text/html");
52 out.println("<B>Technical difficulties.. Please see" +
53 " your administrator for details.</B>");
54 return;
55 }
56 else
57 {
58 //We need to get the outputstream to write binary data
59
60 ServletOutputStream os = response.getOutputStream();
61 response.setContentType("image/gif");
62
63 os.write(generatedGraph, 0, generatedGraph.length);
64 os.flush();
65 }
66 }
67 }
68
69 }
70
Listing 33.1 (continued)
The first time we run this servlet (with no parameters), the browser shows the poll of the day, as seen in Figure 33.1. Once we vote, however, we see an error message that appears on our browser:
java.lang.IllegalStateException: Writer is already being used for this request at
org.apache.tomcat.facade.HttpServletResponseFacade.getOutputStream(HttpS
ervletResponseFacade.java:156)
at BadVoterServlet.doPost(BadVoterServlet.java:63)
What went wrong? The problem is that we requested the Printwriter on line 25 of our servlet, and then we requested the ServletOutputStream object on line 60 of our servlet in Listing 33.1. Servlet documentation tells us that we should use ServletOutputStream for printing binary data and use Printwriter for printing out character text. But our servlet writes both binary data and character text. What should we do?
294 Item 33
3
What is the best thing about Utah?
The Mormon Tabernacle Choir The Utah Jazz √ Winter Olympics „ Donnie Osmond Vote Now!
_ _d
Done [ ^ Local intranet
Figure 33.1 The first part works!
We can either use one or the other. ServletOutputStream is an abstract class that extends java.io.OutputStream, and PrintWriter is a class that extends java.io.Writer. The difference is that PrintWriter is character-based and ServletOutputStream, like java.io.OutputStream, is byte-based. To make life easier, ServletOutputStream adds print() and println() methods for primitive types and Strings, as shown in Table 33.1.
Table 33.1 javax.servlet.ServletOutputStream Methods
METHOD
void print(boolean b); void println(boolean b); void print(char c); void println(char c); void print(double d); void println(double d); void print(float f); void println(float f); void print(int i); void println(int i); void print(long l); void println(long l); void print(String s); void println(String s); void println();
Attempting to Use Both Output Mechanisms in Servlets 295
Because ServletOutputStream contains print() and println() methods, it is easy to see that you may print character-based data with the print() and println() methods, and binary data with the write() methods inherited from java.io.OutputStream. To mix binary and text data in a multipart response, you may use a ServletOutputStream and manage the character sections with the methods in Table 33.1. Still, the rule of thumb is that if you are only sending character data, use the PrintWriter object returned by getWriter().
If your servlet is used for both binary and character data, you may be wise to use ServletOutputStream, rather than getting one or the other in an if-then clause or a try-catch clause. We have seen instances where code asks for the ServletOutputStream right before printing binary data, but asks for the PrintWriter if an error occurs in processing. This is a bad idea and can introduce conditional complexity to your servlet code, making the lifetime of your servlet difficult to manage. Listing 33.2 shows a segment of servlet code that will compile but could introduce complexity and bugs as time goes on. In that listing, the developer gets the ServletOutputStream in the try{} segment, and if an error occurs, it asks for the PrintWriter in the catch() segment.
Previous << 1 .. 101 102 103 104 105 106 < 107 > 108 109 110 111 112 113 .. 166 >> Next