Make a serializable class Book
that holds the following information:
title
author
year
pages
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.
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.
BookStore
.addSomeBooks
true
to the client, then the book itself.false
to the client.The client connects to the server, and takes the appropriate steps, mirroring the server.
true
, reads the book and prints itfalse
, prints a message about the book missingMake a server that communicates using TCP that serves the clients using a separate thread for each client.
The server has to have an internal state, a data field of type java.util.HashMap<String, Integer>
.
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);
}
}
}
startNetworkServer
.db/bin
directory within the JDK.ij
.ij
is short for i
nteractive J
DBC scripting tool.db/bin
directory within the JDK.CONNECT 'jdbc:derby://localhost:1527/books;create=true';
BOOK
that stores books.Book
.pages
should be optional.CREATE TABLE
SQL command.year
is reserved; we have to use quotes: "year"
, or choose another name.Use SHOW TABLES
and DESCRIBE BOOK
to get more information.
Use basic SQL operations (SELECT
, INSERT
, UPDATE
, DELETE
) to experiment with the database.
You can stop the server with the stopNetworkServer
command.
ij
with:CONNECT 'jdbc:derby:books;create=true';
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();
BookStore
class, and explore the created database using ij
.db/lib/derbyclient.jar
within the JDK.Make a Book
entity the following way:
id
: integer, primary key, automatically generatedtitle
: string, at most 255 characters long, must be presentauthor
: string, at most 255 characters long, must be presentpublishedAt
: date, optionalpages
: integer, optionalprice
: java.math.BigDecimal
with precision "10,2", optionalStockStatus
: values stored as strings
IN_STOCK
OUT_OF_STOCK
UNKNOWN
Store the books in the database table books
.
equals()
and hashCode()
so that they function based on the primary key.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.
db/lib/derbyclient.jar
within the JDK.make_and_run.sh
.Create an entity Author
that represents the author of a book:
id
: integer, primary key, automatically generatedname
: string, at most 255 characters long, compulsorybirthDate
: date - only year/month/day, optionalbooks
: list that consists of Book
entitiesCreate a Book
entity that contains the following data
id
: integer, primary key, automatically generatedtitle
: string, max. 255 karakter, compulsoryauthor
: an Author
entity
publishedAt
: date, optionalpages
: integer, optionalprice
: java.math.BigDecimal
with precision "10,2", optionalStockStatus
: values stored as strings
IN_STOCK
OUT_OF_STOCK
UNKNOWN
equals()
and hashCode()
so that they function based on the primary key.Author
and Book
entities so that a book may have multiple authors.// 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);
books
.CascadeType
settings for the connections between the books and the authors.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:
GET
requestsExperiment 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.
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:
GET
request, it should print a static HTML form that contains two input fields and sends its data using the POST
method.POST
request, ensure that we have got both parameters (from the input fields) and that they contain numbers. If so, add them, save them into the session, and route the request to the actual page with external redirection (now using the GET
method).Extend the GET
method so that it controls whether the session has a stored value. If it does, print it, then remove the attribute from the session. The formerly described contents of the page should still be printed.
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.
Make a serializable Book
class that stores the data of a book:
title
author
year
pages
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!
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.