Java Reference
In-Depth Information
Now you have a reference to an object that will allow you to write HTML text that will be sent
back to the client in the HttpServletResponse object. The next few lines of code show how
this is done:
out.println(“<html>”);
out.println(“<head><title>BasicServlet</title></head>”);
out.println(“<body>”);
// Prints the REMOTE_ADDR sent by the client in the request
out.println(“Your request method was “ + request.getMethod()
+ “\n”);
out.println(“</body></html>”);
out.close();
This is a very straightforward method of sending HTML text back to the client. You simply
pass to the PrintWriter 's println() method the HTML text you want included in the
response and close the stream. The only thing that you might have a question about is the fol-
lowing few lines:
// Prints the REMOTE_ADDR sent by the client in the request
out.println(“Your request method was “ + request.getMethod()
+ “\n”);
3
This code takes advantage of the information sent from the client. It calls the
HttpServletRequest 's getMethod() method, which returns the HTTP method with which the
request was made. The HttpServletRequest object holds HTTP-protocol specific header
information.
The getServletInfo() Method
The getServletInfo() method is like the applet's getAppletInfo() method. It can be used to
provide version, copyright, author, and any other information about itself.
Summary
In this chapter, we were finally able to start examining some servlet code. We looked at the life
cycle of a servlet. We also dissected a basic servlet, which gave us a view of each integral part
of a servlet.
You should now be able to create your own servlets. You should also have a basic understand-
ing of the servlet life cycle and where your servlets will fit into the Java servlet framework.
In the next chapter we will take a look at Web applications and how to leverage them to deploy
servlet applications.
 
Search WWH ::




Custom Search