Bill Digman is a Java EE / Servlet enthusiast and Open Source enthusiast who loves working with Caucho's Resin Servlet Container, a Java EE Web Profile Servlet Container. Bill has posted 12 posts at DZone. You can read more from them at their website. View Full User Profile

Tutorial covering JSP 2.2 and Servlets 3.0 with OpenSource Resin Servlet Container: Part 1

10.25.2012
| 26565 views |
  • submit to reddit

Next create a class called BookRepositoryImpl as follows (don't study it too much unless you want to, it just simulates access to database):

package com.bookstore;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.math.BigDecimal;

import javax.enterprise.context.ApplicationScoped;

@ApplicationScoped
public class BookRepositoryImpl implements BookRepository {

	private SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
	private int count;
	private Map<String, Book> idToBookMap = new HashMap<String, Book>();

	public BookRepositoryImpl() {
		synchronized (this) {
			books(book("War and Peace", "blah blah blah", "5.50", "5/29/1970"),
				book("Pride and Prejudice", "blah blah blah", "5.50", "5/29/1960"),
				book("book1", "blah blah blah", "5.50", "5/29/1960"),
				book("book2", "blah blah blah", "5.50", "5/29/1960"),
				book("book3", "blah blah blah", "5.50", "5/29/1960"),
				book("book4", "blah blah blah", "5.50", "5/29/1960"),
				book("book5", "blah blah blah", "5.50", "5/29/1960"),
				book("book6", "blah blah blah", "5.50", "5/29/1960"),
				book("book7", "blah blah blah", "5.50", "5/29/1960"),
				book("book8", "blah blah blah", "5.50", "5/29/1960"),
				book("book9", "blah blah blah", "5.50", "5/29/1960"),
				book("Java for dummies", "blah blah blah", "1.99", "5/29/1960"));
		}
	}

	private Book book(String title, String description, String aPrice,
			String aPubDate) {

		Date pubDate = null;
		BigDecimal price = null;

		try {
			price = new BigDecimal(aPrice);
		}catch (Exception ex) {
		}

		try {
			pubDate = dateFormat.parse(aPubDate);
		}catch (Exception ex) {
		}

		return new Book("" + (count++), title, description, price, pubDate);

	}

	private void books(Book... books) {
		for (Book book : books) {
			doAddBook(book);
		}
	}

	private void doAddBook(Book book) {
		synchronized (this) {
			this.idToBookMap.put(book.getId(), book);
		}
	}

	@Override
	public Book lookupBookById(String id) {
		synchronized (this) {
			return this.idToBookMap.get(id).cloneMe();
		}
	}

	@Override
	public void addBook(String title, String description, String price,
			String pubDate) {
		doAddBook(book(title, description, price, pubDate));
	}

	@Override
	public void updateBook(String id, String title, String description,
			String price, String pubDate) {
		Book book = book(title, description, price, pubDate);
		synchronized (this) {
			book.setId(id);
			this.idToBookMap.put(id, book);
		}
	}

	private List<Book> doListBooks() {
		List<Book> books;
		synchronized (this) {

			books = new ArrayList<Book>(this.idToBookMap.size());
			for (Book book : this.idToBookMap.values()) {
				books.add(book.cloneMe());
			}
		}
		return books;
	}

	public List<Book> listBooks() {

		List<Book> books = doListBooks();

		Collections.sort(books, new Comparator<Book>() {
			public int compare(Book bookA, Book bookB) {
				return bookA.getId().compareTo(bookB.getId());
			}
		});
		return books;
	}

	@Override
	public void removeBook(String id) {
		synchronized(this) {
			this.idToBookMap.remove(id);
		}
	}
}



This BookRepositoryImpl is a fairly basic class. It is largely based on the collections API. You can find out more information about the Java collections API at this tutorial trail. A full discussion of the collection API is out of scope for this tutorial, and this class is mainly just for testing, later we will store items in the databases and such.

Bill Digman is a Java EE / Servlet enthusiast and Open Source enthusiast who loves working with Caucho's Resin Servlet Container, a Java EE Web Profile Servlet Container.

 

Caucho's Resin OpenSource Servlet Container

 

Java EE Web Profile Servlet Container

 

AttachmentSize
Tutorial covering JSP 2.2 and Servlets 3.0 with OpenSource Resin Servlet Container - Part 1.pdf849.79 KB
Published at DZone with permission of its author, Bill Digman.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Bruce Phillips replied on Thu, 2012/10/25 - 11:00am

This is a good article but it could be made much,  much better by providing two items

 

1.  Code download

2.  PDF version that a reader can save to read later or to refer to when needed.  It is very tedious to click through 15 web pages where on each  page the ads and other crap overwhelm the important content.

 

 

Bill Digman replied on Thu, 2012/10/25 - 3:12pm in response to: Bruce Phillips

I'll work on a PDF version right away.

How do you suggest I provide code download?  

Bill Digman replied on Thu, 2012/10/25 - 4:29pm in response to: Bill Digman

The PDF version is attached

Bruce Phillips replied on Thu, 2012/10/25 - 7:02pm in response to: Bill Digman

Thank you very much for the PDF version. 

For the code you could use Eclipse - Export as archived projected which creates a zip of your project.  Other Eclipse users can then import the project directly from the zip.  Non-Eclipse users can unzip the project and view the files also.

 

Henk De Boer replied on Fri, 2012/10/26 - 12:11pm

It's thorough and explains things well, but it's a somewhat confusing setup.

The material being explained; Servlets, JSP and then Scriptlets vs JSTL feels like it comes straight from the early 2000s, but then we do see relative modern annotations on the Servlets and even the use of CDI.

Bill Digman replied on Thu, 2012/11/01 - 11:38am in response to: Henk De Boer

Yep... This is true. In a future article we are going to cover a more CDI centric approach covering @Produces @RequestScoped. Later we will cover JPA, JAX-RS, JCache, etc. and yes maybe even JSF (grumble, grumble)....

Bill Digman replied on Thu, 2012/11/01 - 11:39am

Stay tuned. I have a lot more to say. I think Java EE is more of a cafeteria plan than a one size fits all. You can use Servlets, CDI and JSP for model 2 apps. JAX-RS for RIA clients. Websockets, JCache, etc. Most tutorials wrt Java EE seem to really push JSF and EJB. If you tie Java EE to JSF and EJB and I think it turns a lot of folks off. Java EE is more than this.

Sam Noor replied on Fri, 2012/12/21 - 4:38am

 I just found this blog and have high hopes for it to continue. Keep up the great work, its hard to find good ones. I have added to my favorites. Thank You. http://www.ileatherwear.com !! *Leather outfit* http://www.ileatherwear.com/offer/wwii-bomber-jacket.html !! *Wwii bomber jacket* http://www.ileatherwear.com/offer/white-leather-jackets-for-women.html !! *White leather jackets for women* http://www.ileatherwear.com/offer/tan-leather-jacket-women.html !! *Tan leather jacket women*

Keenee Madison replied on Thu, 2013/10/31 - 6:33am

JSP technology enables rapid development of web-based applications that are server- and platform-independent.  

WordPress Plugin development

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.