Sending Output Back to the Client Machine


Once the server-side application has been run, the only remaining task for the servlet is to send the output data back to the client machine. The HttpServletResponse object that was initially passed to the doGet() or doPost() method sends output data to the client in a most interesting way. It first opens an I/O data stream between the server and client. If the HttpServletResponse instance were named response , the syntax would be ”

 response.setContentType("text/html"); PrintWriter writer = response.getWriter(); 

The getWriter() method returns a PrintWriter instance that is used to write character data to an output stream. The PrintWriter object writes the output to the client web browser in the form of an HTML file. The client web browser reads the HTML form as if it were loading a standard HTML file and displays it on the screen.

 writer.println("<HTML><HEAD><TITLE>"+          "Atmospheric Model Application</TITLE>"); writer.println("</HEAD><BODY>"); // more println() statements follow 

In the example code fragment, the PrintWriter object calls the println() method to print HTML header and title information to the client web browser. Each line of the HTML file is written by the println() method as a String .



Technical Java. Applications for Science and Engineering
Technical Java: Applications for Science and Engineering
ISBN: 0131018159
EAN: 2147483647
Year: 2003
Pages: 281
Authors: Grant Palmer

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net