Time for action - implementing the servlet

The bundle will contain one class, the servlet implementation; we'll configure the rest as iPOJO declarations.

In the accompanying code, the servlet class is defined in the package com.packtpub.felix.bookshelf.servlet. We'll start with a skeleton to define the iPOJO injection points and complete the configuration.

public class BookshelfServletImpl extends HttpServlet
{
private String alias;
private BookshelfService service;
private BookshelfLogHelper logger;
private String sessionId;
public void init(ServletConfig config) {
}
protected void doGet(
HttpServletRequest req, HttpServletResponse resp)
{
}
}

We'll give the method doGet() a fuller body in a bit. Let's configure the iPOJO declarations.

The iPOJO configuration

The iPOJO configuration declares the component as providing a service. From the point of view of iPOJO, this component is a regular service that has requirements and capabilities. The fact that it extends HttpServlet will be of interest to the Http Whiteboard at activation time.

<ipojo>
<component
name="BookshelfServletImpl"
classname=
"com.packtpub.felix.bookshelf.servlet.BookshelfServletImpl"
immediate="true">
<provides>
<property name="alias" field="alias" />
</provides>

The alias property will be used by the whiteboard pattern implementation to publish the servlet.

The component also requires injection of the service field (a BookshelfService) and the logger field (a BookshelfLogHelper):

<requires field="service" />
<requires field="logger" />
</component>

The instance declaration sets the alias value to /bookshelf.

<instance
name="bookshelf.servlet"
component="BookshelfServletImpl">
<property name="alias" value="/bookshelf" />
</instance>
</ipojo>

This will delegate processing of requests to http://localhost:8080/bookshelf to our servlet implementation BookshelfServletImpl.

Implementing the operations

Okay, let's implement the servlet's doGet() method to process the following requests:

  • Listing of categories: Requested with the operation categories.
  • Listing of books by category: Requested using the byCategory operation, with the parameter category as the search filter.
  • Listing of books by author: Requested using the byAuthor operation, with the parameter author as the search filter.
  • Adding a book: Requested using the addBook operation, with the parameters isbn, author, title, category, and rating. An additional operation, addBookForm, provides the html form for submitting the addBook operation.

Let's start by preparing the constants for those operations.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.12.232