Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our User Agreement and Privacy Policy.

Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our Privacy Policy and User Agreement for details.

Java EE Servlet/JSP Tutorial- Cookbook 2

This tutorial focuses on using Servlet's and JSP the right way. Servlet and JSP have evolved over the years, and now there is often more than one way to do things. For example, this tutorial uses EL and JSTL not JSP scriptlets, it uses JSPs in a Model 2/MVC style not in a model 1 style, etc. Consider it a tutorial that focuses only on the best practices and not the legacy ways to do things.

There are other tutorials on this subject, but this tutorial is going to be different in that it is going to put all code into github, and you can follow along with Eclipse. Also instead of focusing on JSF, we are going to focus on JSP and Servlets as the main view technology.

Java EE, JSP and Servlets have added a lot of features that are in other frameworks, making those other frameworks less relevant then they were before Java EE garnered these extra abilities. Even is you decide to use JSF, Struts, Stripes, Spring MVC, JSF, etc., this tutorial should help you have a better understanding of the JSP/Servlets core that they build on.

We are going to start by building a simple bookstore. We will progressively add more features to the bookstore and as we do we will use more of Java EE/CDI, JSP and Servlets.

For this tutorial, I am going to use Resin 4.0.x, but you could use any Java EE 6 Web Profile compliant application server.

Java EE Servlet/JSP Tutorial- Cookbook 2

2.
Cookbook: Intro to Serlvets and JSP • This cookbook in the Java EE Servlet /JSP tutorial covers building CRUD Operations in a Model 2 architecture • This is a continuation of Building a simple listing in JSP using Java EE and Servlets (Part 1). • This is part 2, must do part 1 ﬁrst • Part 1 Slides • Covers working with Servlet doGet/doPost methods, JSTL, redirec8on versus forwardingCaucho Home | Contact Us | Caucho Blog | Wiki | Applica8on Server

3.
Redux: About tutorial • Very liNle knowledge of HTML, Java and JSP is assumed • HTML and Java not covered length, but pointers in the right direc8on • Focus is Java Servlets and JSP (Java Server Pages) • Use whatever IDE you would like, but direc8ons focus on EclipseCaucho Home | Contact Us | Caucho Blog | Wiki | Applica8on Server

6.
Where we leX oﬀ in last example • BookListServlet uses a BookRepository object (DAO) to load a list of books • BookListServlet then delegated to book-­‐list.jsp to render the book lis8ng with JSTL and Uniﬁed EL • In this cookbook, • add a link to the book lis9ng for edi9ng a book • add a link so that the end user can add a new book to the lis9ng • Create backend Servlets to handle new links on book lis9ngCaucho Home | Contact Us | Caucho Blog | Wiki | Applica8on Server

11.
Adding a “add a book” link to book lis8ng • Now that links are going to URI /book, • You need a Servlet that handles links • For add opera9on and edit opera9on • New BookEditorServlet will handle both add and edit book func8onsCaucho Home | Contact Us | Caucho Blog | Wiki | Applica8on Server

13.
BookEditorServlet • @WebServlet("/book") maps BookEditorServlet to the URI / book • Common to load a form from a doGet method, and to handle the form submission via doPost • Follows REST and HTTP principles GET opera8ons reads data, • later POST data modiﬁes data • doGet method uses id being empty or not to • determine if this is a load "Add Book Form" or • load "Update Book Form" opera9onCaucho Home | Contact Us | Caucho Blog | Wiki | Applica8on Server

21.
BookEditorServlet.doPost() • if id request parameter is null then BookEditorServlet.doPost calls bookRepo.addBook, • otherwise it calls bookRepo.updateBook • Then, doPost redirects to /book/ • redirect means an extra hit to the server, • basically telling browser to load another link • Not forward like before because of bookmarking • Remember URL /book/ (ending in slash) represents a collec9on of books, while /book (no slash) represents a single book • If doPost did a forward, then browser would show wrong link for lis9ng • Use sendRedirect instead of a forward for bookmarkingCaucho Home | Contact Us | Caucho Blog | Wiki | Applica8on Server