Lesson 1

Serialization

Make a serializable class Book that holds the following information:

The class BookStore stores books in a hash table, identifying them by their names. The class should implement the following methods:

// Initialises the hash map with some predefined data.
void addSomeBooks();

// Loads the books from the given file.
// Returns whether the loading was successful.
boolean loadBooks(String fileName);

// Saves the books into the given file.
// Returns whether the saving was successful.
boolean saveBooks(String fileName);

Make a command line application that tests the above operations.

Socket

Make a simple client-server application, whose parts communicate using TCP/IP. Use the classes Book and BookStore from the previous exercise.

The server accepts clients one after the other. When a client connects, the server does the following.

  1. Takes the name of a book from the client.
  2. Creates a BookStore.
  3. Tries to load the books from a file.
  4. Looks up the requested book by name in the store.
    1. If found, sends true to the client, then the book itself.
    2. Otherwise, sends false to the client.
  5. Saves the books to the file.

The client connects to the server, and takes the appropriate steps, mirroring the server.

  1. Connects to the server.
  2. Sends the name of the book.
  3. Processes the reply. To do that, first it reads a boolean value.

Lesson 2

RPC

Make a server that communicates using TCP that serves the clients using a separate thread for each client.

  1. The client sends a string to the server.
  2. As reply, the server sends how many times it has received the same text before.

The server has to have an internal state, a data field of type java.util.HashMap<String, Integer>.

RMI

Make a server that is accessible through RMI through the interface BookService, which makes the following service available:

// Add a new book. If such a book is already present, return false, otherwise return true.
boolean add(Book newBook);

// If the book is present, return its title, otherwise, return null.
Book getByTitle(String title);

// Remove the book if present. Return whether the book was present.
boolean removeByTitle(String title);

Also make a client that accesses the server and tries its features.

You may use the following, simplified versions of Book and BookStore:

// Book.java ///////////////////////////////////////////////////////////////////

import java.io.Serializable;

public class Book implements Serializable {
  public String title;
  public String author;
  public int year;
  public int pages;
}
// BookStore.java //////////////////////////////////////////////////////////////

import java.util.HashMap;

public class BookStore {
  public HashMap<String, Book> books = new HashMap<>();

  public void BookStore() {
    for (int i = 0; i < 10; ++i) {
      Book book = new Book("Book #" + i, "Author #" + i, 2000 + i,
          300 + 10 * i);
      books.put(book.getTitle(), book);
    }
  }
}

Lesson 3

Get familiar with Derby

  1. Start an Apache Derby database server instance using the command startNetworkServer.
  1. Connect to the Derby server using the command ij.
  1. First, create a database by executing
CONNECT 'jdbc:derby://localhost:1527/books;create=true';
  1. Create a table BOOK that stores books.
  1. Use SHOW TABLES and DESCRIBE BOOK to get more information.

  2. Use basic SQL operations (SELECT, INSERT, UPDATE, DELETE) to experiment with the database.

  3. You can stop the server with the stopNetworkServer command.

CONNECT 'jdbc:derby:books;create=true';

JDBC

Create an application that uses JDBC which persists the previous Book entities in a Derby database.

Implement a new BookStore class that uses the database:

// Constructor, connects to the database. In case of an error, prints an error message to the console.
BookStore(String host, int port);

// Adds a new book into the database. Returns true if there was no error, otherwise false.
boolean addBook(Book newBook);

// Deletes a book from the database. Returns true if there was no error, otherwise false.
boolean removeBookByTitle(String title);

// Gets all books from the database. In case of an error, returns the empty list.
java.util.List<Book> getAllBooks();

Lesson 4

Database

Make a Book entity the following way:

Store the books in the database table books.

JPA

Make a class BookStore that handles the above Book entities via JPA. Implement the following operations:

// Constructor, gets the name of a persistence unit, and creates an appropriate `EntityManager` for it.
BookStore(String persistenceUnitName);

// Adds a new book into the database, and returns its identifier.
int addBook(Book newBook);

// Deletes the book with the given id (if it exists in the database).
void removeBookById(int bookId);

// Gets all books present in the database.
java.util.List<Book> getAllBooks();

Make a command line application that tests the BookStore class.

Lesson 5

JPA relations

Create an entity Author that represents the author of a book:

Create a Book entity that contains the following data

// Adds an author.
Author addAuthor(String name, Date birthDate /* can be null */);

// Removes an author.
void removeAuthor(Author author);

// Adds a book with one author. It will be UNKNOWN in stock.
Book addBook(String title, Author author);

// Associates a book with an author.
void assignAuthorToBook(Author author, Book book);

// Removes a book.
void removeBook(Book book);

Lesson 6

Servlet

Make a web application that contains a servlet called ScopeTest. The servlet has to be reachable at the URL /test from the web application's root. The server has to do the following:

Experiment with the server: try it using several different browsers; try removing the session cookies from time to time; try restarting the application server! Observe how the attributes behave upon these events.

Web application

Create a web application that contains a servlet called FormTest. The servlet has to be reachable at the URL /form from the web application's root. The server has to do the following:

The application will use the POST-Redirect-GET mechanism. The session attribute demonstrates the lifetime named "flash scope", that is, a value which only stays in the session until the next request.

Lesson 7

EJB: stateless EJB

Make a serializable Book class that stores the data of a book:

Make an interface BookStore with the following operations:

// Adds a book. If it already exists, returns false, otherwise it saves the book, and returns true.
boolean addBook(Book newBook);

// Gets all the books.
List<Book> getAllBooks();

// Gets a book by its title. If it cannot be found, returns null.
Book getBookByTitle(String title);

Implement the interface with a stateless session bean named BookStoreBean. Store the books in a java.util.HashMap named books, identifying them by their title.

Test the bean using a command line application!

EJB: local EJB

Extend the previous exercise with another local bean that shows if the book is over 200 pages long:

boolean isALongBook(Book book);

Use the new, local LongBookFilterBean in BookStoreBean. The getAllBooks operation in the latter should only return the long books. Inject the new bean into the book store using the @EJB annotation.

Make a client that uses dependency injection to test the bean. This client should run in the application client container.