Java EE Servlet/JSP Tutorial Cookbook 2
Java EE Servlet/JSP Tutorial Cookbook 2
EE
Servlet/JSP
Tutorial
Second cookbook, getting started with Model 2: Servlet and JSP Implementing the Edit/Update, Add
This
is
part
2,
must
do
part
1
rst Part
1
Slides Covers
working
with
Servlet
doGet/doPost
methods,
JSTL,
redirec8on
versus
forwarding
Focus
is
Java
Servlets
and
JSP
(Java
Server
Pages) Use
whatever
IDE
you
would
like,
but
direc8ons
focus
on
Eclipse
Remove
Outline
1 Java EE Servlet Tutorial: Implementing a basic CRUD listing 2 Adding a link to the book listing to edit a book 2.1 Adding an edit book link to book-list.jsp listing 3 Adding a link to the book listing to add a book 3.1 Adding an add book link to book-list.jsp listing 4 Servlet doGet to load a Book form 4.1 BookEditorServlet.java doGet 4.2 BookEditorServlet.java doGet() delegate to book-form.jsp page 4.3 BookEditorServlet.java doGet() delegate to book-form.jsp page 5 Rendering the book form HTML 5.1 book-form.jsp Renders form to update or add a Book 5.2 book-form.jsp using JSTL c:choose to display update or add status 5.3 book-form.jsp using JSTL c:if to hidden id eld for edit/update operation 6 Creating a doPost method to handle the form submission 6.1 BookEditorServlet.java doPost 7 Quick review of what we have so far 7.1 ./WebContent/WEB-INF/pages/book-form.jsp full listing 7.2 ./WebContent/WEB-INF/pages/book-list.jsp full listing 7.3 ./src/META-INF/beans.xml full listing 7.4 ./src/com/bookstore/Book.java 7.5 ./src/com/bookstore/BookRepositoryImpl.java full listing (testing only) 7.6 ./src/com/bookstore/BookRepository.java full listing 7.7 ./src/com/bookstore/web/BookEditorServlet.java 7.8 ./src/com/bookstore/web/BookListServlet.java 8 Technical debt 9 Cookbooks and Tutorials
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 lis9ng
Model
2
MVC
Model View Controller
book-form.jsp book-list.jsp
BookListingServlet BookEditorServlet
The following links with URI (/bookstore) of webapp get rendered when book- lis8ng.jsp loads
Now that links are going to URI /book, You need a Servlet that handles links
New BookEditorServlet will handle both add and edit book func8ons
BookEditorServlet
@WebServlet("/book")
maps
BookEditorServlet
to
the
URI
/
book
BookEditorServlet
Edit Link
book-form.jsp
Notice book is mapped into request scope book will get used from book-form.jsp
book-form.jsp
book-form.jsp
(1
of
3)
book-form.jsp
renders
form
to
edit
book
If
book.id
present
then
edit
opera8on,
otherwise
add
opera8on
JSTL c:choose, c:otherwise to display correct 9tle based on Update (Edit) or Add opera9on
book-form.jsp (2 of 3)
Uses Unied EL to render values and then just plain HTML for form elds
book-form.jsp
(3
of
3)
hidden
id
property
is
rendered
if
edit
(Update)
opera8on Cancel
buNon
takes
them
back
to
lis8ng
(/book/
is
lis8ng). Submit
buNon
POST
form
to
BookEditorServlet.doPost
(dened
next)
BookEditorServlet.doPost()
if
id
request
parameter
is
null
then
BookEditorServlet.doPost
calls
bookRepo.addBook,
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
lis9ng
If
doPost
did
a
forward,
then
browser
would
show
wrong
link
for
Use
sendRedirect
instead
of
a
forward
for
bookmarking
Caucho
Home
|
Contact
Us
|
Caucho
Blog
|
Wiki
|
Applica8on
Server
BookEditorServlet.doPost()
BookEditorServlet.doPost
book-form.jsp
BookListServlet
Caucho
Home
|
Contact
Us
|
Caucho
Blog
|
Wiki
|
Applica8on
Server
./WebContent/WEB-INF/pages/book-form.jsp
Servlet that loads Book (doGet) form and handles Book form submissions (doPost). ./src/com/bookstore/web/ BookEditorServlet.java
Servlet that looks up a list of books and displays the lis8ng ./src/com/bookstore/web/ BookListServlet.java
./src/com/bookstore/BookRepositoryImpl.java
Interface to Book Repository so we can swap it out with JDBC, JPA, JCache and MongoDB version later
./src/com/bookstore/BookRepository.java
Open%Source,%Reliable%and%Lightweight% Java%EE%Applica;on%Server%
RESIN PRO
Web Profile
Health System
Cloud Support
More
Info
Caucho
Technology
|
Home
Page Resin
|
Applica8on
Server Resin
|
Java
EE
Web
Prole
Applica8on
Server Resin
-
Cloud
Support
|
3G
-
Java
Clustering Resin
|
Java
CDI
|
Dependency
Injec8on
/
IoC Resin
-
Health
System
|
Java
Monitoring
and
Server
Monitoring Download
Resin
|
Applica8on
Server Watch
Resin
|
Applica8on
Server
Featured
Video
Caucho
Home
|
Contact
Us
|
Caucho
Blog
|
Wiki
|
Applica8on
Server