Transcript
Page 1: Java EE Servlet/JSP Tutorial Cookbook 2

Caucho  Home    |    Contact  Us    |    Caucho  Blog    |    Wiki    |  Applica8on  Server

Java  EE  Servlet/JSP  Tutorial

Second cookbook, getting started with Model 2: Servlet and JSPImplementing the Edit/Update, Add

Page 2: Java EE Servlet/JSP Tutorial Cookbook 2

Caucho  Home    |    Contact  Us    |    Caucho  Blog    |    Wiki    |  Applica8on  Server

•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  first

•Part  1  Slides

•Covers  working  with  Servlet  doGet/doPost  methods,  JSTL,  redirec8on  versus  forwarding

Cookbook:  Intro  to  Serlvets  and  JSP

Page 3: Java EE Servlet/JSP Tutorial Cookbook 2

Caucho  Home    |    Contact  Us    |    Caucho  Blog    |    Wiki    |  Applica8on  Server

• 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  Eclipse

Redux:  About  tutorial

Page 4: Java EE Servlet/JSP Tutorial Cookbook 2

Caucho  Home    |    Contact  Us    |    Caucho  Blog    |    Wiki    |  Applica8on  Server

Redux:  App  you  are  building

Sorting

Remove

Page 5: Java EE Servlet/JSP Tutorial Cookbook 2

Caucho  Home    |    Contact  Us    |    Caucho  Blog    |    Wiki    |  Applica8on  Server

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 field 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

Page 6: Java EE Servlet/JSP Tutorial Cookbook 2

Caucho  Home    |    Contact  Us    |    Caucho  Blog    |    Wiki    |  Applica8on  Server

Where  we  leX  off  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  Unified  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  lis9ng

Page 7: Java EE Servlet/JSP Tutorial Cookbook 2

Caucho  Home    |    Contact  Us    |    Caucho  Blog    |    Wiki    |  Applica8on  Server

Model  2  MVCModel View Controller

•Book•BookRepositoryImpl•BookRepository

•book-form.jsp•book-list.jsp

•BookListingServlet•BookEditorServlet

Page 8: Java EE Servlet/JSP Tutorial Cookbook 2

Caucho  Home    |    Contact  Us    |    Caucho  Blog    |    Wiki    |  Applica8on  Server

Add  a  new  link  to  book-­‐lis8ng.jsp• Add  link  to  edit  opera8on

• Edit  opera8on  pulls  up  form  with  details  of  Book  8tle  that  is  clicked

• Uses  <a  href=””

• Uses  expression  pageContext.request.contextPath/book  to  address  new  Edit  Servlet

• Servlet  created  later,  id  parameter  implies  edit  opertaion

• EL  expression  pageContext.request.contextPath  refers  to  the  URI,  web  app  (war  file)  is  mapped  to  in  Servlet  container

Page 9: Java EE Servlet/JSP Tutorial Cookbook 2

Caucho  Home    |    Contact  Us    |    Caucho  Blog    |    Wiki    |  Applica8on  Server

Edit  Link  on  Title

Page 10: Java EE Servlet/JSP Tutorial Cookbook 2

Caucho  Home    |    Contact  Us    |    Caucho  Blog    |    Wiki    |  Applica8on  Server

What  gets  rendered• The  following  links  with  URI  (/bookstore)  of  webapp  get  rendered  when  book-­‐

lis8ng.jsp  loads

Page 11: Java EE Servlet/JSP Tutorial Cookbook 2

Caucho  Home    |    Contact  Us    |    Caucho  Blog    |    Wiki    |  Applica8on  Server

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  func8ons

Page 12: Java EE Servlet/JSP Tutorial Cookbook 2

Caucho  Home    |    Contact  Us    |    Caucho  Blog    |    Wiki    |  Applica8on  Server

Add  Link  Above  Table

Page 13: Java EE Servlet/JSP Tutorial Cookbook 2

Caucho  Home    |    Contact  Us    |    Caucho  Blog    |    Wiki    |  Applica8on  Server

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  modifies  data

• doGet  method  uses  id  being  empty  or  not  to  

• determine  if  this  is  a  load  "Add  Book  Form"  or  

• load  "Update  Book  Form"  opera9on

Page 14: Java EE Servlet/JSP Tutorial Cookbook 2

Caucho  Home    |    Contact  Us    |    Caucho  Blog    |    Wiki    |  Applica8on  Server

BookEditorServlet

Page 15: Java EE Servlet/JSP Tutorial Cookbook 2

Caucho  Home    |    Contact  Us    |    Caucho  Blog    |    Wiki    |  Applica8on  Server

BookEditorServlet.doGet  loads  edit/add  form

Edit Link

Add Link

doGet is load form operation

book-form.jsp

Page 16: Java EE Servlet/JSP Tutorial Cookbook 2

Caucho  Home    |    Contact  Us    |    Caucho  Blog    |    Wiki    |  Applica8on  Server

Model  2  and  BookEditorServlet.doGet• In  Model  2,  Servlets  (controllers/ac8ons)  prepares  model  data  for  the  view

• This  includes  date  formagng

Notice “book” is mapped into request scope

“book” will get used from book-form.jsp

Page 17: Java EE Servlet/JSP Tutorial Cookbook 2

Caucho  Home    |    Contact  Us    |    Caucho  Blog    |    Wiki    |  Applica8on  Server

Model  2  and  BookEditorServlet.doGet  (cont)• To  render  the  HTML  form,  the  servlet  delegates  to  book-­‐form.jsp

book-form.jsp

Page 18: Java EE Servlet/JSP Tutorial Cookbook 2

Caucho  Home    |    Contact  Us    |    Caucho  Blog    |    Wiki    |  Applica8on  Server

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

Page 19: Java EE Servlet/JSP Tutorial Cookbook 2

Caucho  Home    |    Contact  Us    |    Caucho  Blog    |    Wiki    |  Applica8on  Server

book-­‐form.jsp  (2  of  3)• Uses  Unified  EL  to  render  values  and  then  just  plain  HTML  for  form  fields

Page 20: Java EE Servlet/JSP Tutorial Cookbook 2

Caucho  Home    |    Contact  Us    |    Caucho  Blog    |    Wiki    |  Applica8on  Server

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  (defined  next)

Page 21: Java EE Servlet/JSP Tutorial Cookbook 2

Caucho  Home    |    Contact  Us    |    Caucho  Blog    |    Wiki    |  Applica8on  Server

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  bookmarking

Page 22: Java EE Servlet/JSP Tutorial Cookbook 2

Caucho  Home    |    Contact  Us    |    Caucho  Blog    |    Wiki    |  Applica8on  Server

BookEditorServlet.doPost()

Page 23: Java EE Servlet/JSP Tutorial Cookbook 2

Caucho  Home    |    Contact  Us    |    Caucho  Blog    |    Wiki    |  Applica8on  Server

BookEditorServlet.doPostbook-form.jsp

BookListServlet

Page 24: Java EE Servlet/JSP Tutorial Cookbook 2

Caucho  Home    |    Contact  Us    |    Caucho  Blog    |    Wiki    |  Applica8on  Server

• Book  Form

• ./WebContent/WEB-­‐INF/pages/book-­‐form.jsp                    

• Book  Lis8ng

• ./WebContent/WEB-­‐INF/pages/book-­‐list.jsp                          

• Needed  for  Java  EE  dependency  injec8on  (CDI)

• ./src/META-­‐INF/beans.xml                                                                                

• Domain/model  object

• ./src/com/bookstore/Book.java                                                                

• Repository  implementa8on  using  Java  collec8ons  (just  for  tes8ng)

• ./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              

• 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                  

Review  of  CRUD  lis8ng

Page 25: Java EE Servlet/JSP Tutorial Cookbook 2

RESIN PRO Web Profile Health System Cloud Support

Open%Source,%Reliable%and%Lightweight%Java%EE%Applica;on%Server%

Page 26: Java EE Servlet/JSP Tutorial Cookbook 2

Caucho  Home    |    Contact  Us    |    Caucho  Blog    |    Wiki    |  Applica8on  Server

• Caucho  Technology  |  Home  Page

• Resin  |  Applica8on  Server

• Resin  |  Java  EE  Web  Profile  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

More  Info


Top Related