{
str.append(line);
str.append("
");
}
str.append("The width is ");
str.append(width);
str.append(" and the height is ");
str.append(height);
str.append(".
");
// write output to the response object
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.println(str);
out.close();
}
}
The service() method (here called  doGet() ) always has two arguments, which are an input and 
an output object. For an HTTP servlet, those objects are the HTTP request and the HTTP 
response. The HTTP request object passes all arguments to the servlet that were received by 
the Web server with the request. The response object contains a PrintWriter stream to hold the 
response that is generated by the servlet. 
The simple servlet shown above reads two parameters width  and height from the request 
object.  Then, it draws a rectangle of these dimensions to a string buffer. Finally, it writes the 
string buffer containing the rectangle to the response object. 
While this servlet does not seem particularly useful, and the same functionality could be 
achieved through some browser side scripting, it does already show some of the servlet 
architecture's power: The doGet() method uses fill(char[ ] a, char val) of the Java Arrays 
package. All of Java's extensive library packages can be accessed through servlets, as well as 
any other classes or methods available on the server (custom classes, J2EE beans, etc.).  
28 




Java Web Hosting Application Development Using Java Technologies Hosting




 
TotalRoute.net Business web hosting division of Vision Web Hosting Inc. All rights reserved.