The J2EE BookShop
A detailed walk through of the J2EE BookShop
Functional Requirements - repetition The Bookshop is a web shop where the
customer can do the following Customers should log into the system Customers should be able to browse through all
available books Customers should be able to view detailed
information about a particular book
Functional Requirments - repetition
Customers should be able to add and remove books from their shopping cart
When customers want to checkout their order, they should supply shipping information
When checking out, the order should be written in the ORDER table and the different books in the ORDER_ITEMS table
Technical Requirments - repetition Use MVC, Model View Controller The result return from beans or servlet should
be XML that’s transformed to HTML with XSLT (use the JSTL tag library). Never print HTML in the beans or the Servlets. Output in JSPs are acceptable
Try use a Custom tag, for example to return the shopping cart
Fundamental design ideas
The book shop uses these fundamental design ideas Model View Controller (MVC) is used as the base All requests should be handled by a single
controller servlet Standard J2EE Security is used to secure the site
We don’t implement the security, we only tell the system to use it
All dynamic content should be returned to the view as XML
All XML is transformed into XHTML with XSLT
Fundamental design ideas
Static layout can be performed in the JSP pages for simplicity
Only control logic in the controller servlet All business logic in Java Beans All presentation logic in JSPs and XSLT The shopping cart is accessed through a
Custom Tag
Fundamental design ideas
Environment variables (like JDBC Url and the different JSP pages) should be configurable at deploy time, i.e. should be defined in web.xml
web.xml
One servlet Named Shop and mapped as /shop se.upright.education.uu.pvk.assignmenttwo.servle
ts.ShopServlet Five init-param entries
JDBC_URL CHECKOUT_PAGE SHOW_PAGE THANKYOU_PAGEDETAIL_PAGE
web.xml
Three Tag Libraries JSTL – core
c.tld JSTL – xml
x.tld BookShop
bookshop.tld
web.xml
The URL-pattern /* is secured Only users in the role tomcat is allowed Form-login is used
login.jsp is the login form login_error.jsp is the error page in case of a login
failure
web.xml
<security-constraint> <web-resource-collection> <web-resource-name>TheShop</web-resource-name> <url-pattern>/*</url-pattern> </web-resource-collection> <auth-constraint> <role-name>tomcat</role-name> </auth-constraint> <user-data-constraint> <transport-guarantee>NONE</transport-guarantee> </user-data-constraint> </security-constraint> <login-config> <auth-method>FORM</auth-method> <form-login-config> <form-login-page>/login.jsp</form-login-page> <form-error-page>/login_error.jsp</form-error-page> </form-login-config> </login-config>
Database access
All database access is handled by two beans BookListBean OrderBean
The book list is only fetched once and then added to the application context Bad performance to get the list for each request Unnecessary memory usage if each user have a
book list of their own Read-only data
BookBean
A JavaBean that represent one book All properties of the book available with
getXXX() and setXXX() methods getXml() returns the book in XML format
BookListBean
The implementation of the book list A Collection with BookBeans is the actual list When created, the BookListBean() connects
to the database and fetches all books getXml() returns the entire book list as XML getXml() uses BookBean.getXml() to build it’s
XML representation
ShoppingBean
The implementation of the shopping cart Each user should have his own instance of
the shopping cart addBook(book, quantity) adds a book to the
cart If the exists in the cart, only increase the quantity
removeBook(id, quantity) removes a book If the quantity to remove is more or equal to the
present, remove the book, otherwise decrease the quantity in the cart
ShoppingBean
getCart() returns the Collection that holds the actual cart
getXml() returns the shopping cart as XML clear() removes all books from the cart
OrderBean
Used to create a new order based on the shopping cart and the shipping information entered by the user
The order is written to the ORDER-table The different books are written to the ORDER_ITEM
table Explicit transaction handling is used
Inserting an order is one operation Each book is one operation All operations in one transaction, i.e. write the order and all
books, or write nothing
ShopingCartTag
The implementation of the Custom tag to output the shopping cart
Uses ShoppingBean.getXml() to get the cart
XML – the book
<book> <id>1</id> <title>Javaprogramming</title> <authorname>Fredrik</authorname> <authorsurname>Alund</authorsurname> <price>23</price> <pages>234</pages> <description>Bla bla bla</description> </book>
XML – the book list
<booklist> <book> <id>1</id> <title>Javaprogramming</title> <authorname>Fredrik</authorname> <authorsurname>Alund</authorsurname> <price>23</price> <pages>234</pages> <description>Bla bla bla</description> </book> <book> <id>2</id> <title>Javaprogramming2</title> <authorname>Kalle</authorname> <authorsurname>Svensson</authorsurname> <price>234</price> <pages>100</pages> <description>Bla bla bla</description> </book> </booklist>
XSL files
booklist_xsl.xslt Format the book list BookListBean.getXml()
bookdetail_xsl.xslt Format the details about a particular book BookBean.getXml()
shoppingcart_xsl.xslt The shopping cart shown in the show page ShoppingBean.getXml()
shoppingcart_checkout_xsl.xslt The shopping cart shown in the checkout page ShoppingBean.getXml()