[ Team LiB ] Previous Section Next Section

Workshop

The Workshop is designed to help you review what you have learned and help you to further increase your understanding of the material covered in this hour.

Quiz

1:

What are the methods that the Servlet interface declares?

2:

Why did the example use HttpServlet?

3:

Where do the object references used to work with requests and responses come from?

4:

How do you obtain a reference to an object that can be used to output to?


Answers

A1:

The Servlet interface declares the following methods: init, destroy, service, getServletConfig, and getServletInfo.

A2:

HttpServlet is an implementation of Servlet that provides methods that manage the ServletConfig object, and it is specialized to work with HTTP. It allows a servlet to handle GET or POST requests and works with other operations specific to HTTP. Using HttpServlet, the example only needed to implement doGet (overriding a method of HttpServlet) to be able to produce a response to an HTTP GET request from a browser.

A3:

References to instances of the classes ServletRequest and ServletResponse are passed as parameters to the service method. In the case of an HttpServlet object, references to instances of HttpServletRequest and HttpServletResponse are passed to the doGet method (as well as other methods that may need to interact with these objects).

A4:

The getWriter method of the ServletResponse or HttpServletResponse objects will return a PrintWriter that you can output text to. If you need to obtain a stream to perform binary output, you can use getOuputStream.


Activities

  1. Write another simple servlet that produces a slightly more complicated output. For example, produce a servlet that outputs an "address card" that consists of your name, an HTML horizontal rule (<HR>), your address and phone number.

  2. Use the Tomcat application manager to deploy the servlet above.

    [ Team LiB ] Previous Section Next Section