SlideShare a Scribd company logo
Java	
  EE	
  Servlet/JSP	
  Tutorial

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




Caucho	
  Home	
  	
  |	
  	
  Contact	
  Us	
  	
  |	
  	
  Caucho	
  Blog	
  	
  |	
  	
  Wiki	
  	
  |	
  Applica8on	
  Server
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	
  first
                • Part	
  1	
  Slides
              • Covers	
  working	
  with	
  Servlet	
  doGet/doPost	
  methods,	
  JSTL,	
  
                       redirec8on	
  versus	
  forwarding




Caucho	
  Home	
  	
  |	
  	
  Contact	
  Us	
  	
  |	
  	
  Caucho	
  Blog	
  	
  |	
  	
  Wiki	
  	
  |	
  Applica8on	
  Server
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	
  
                       Eclipse




Caucho	
  Home	
  	
  |	
  	
  Contact	
  Us	
  	
  |	
  	
  Caucho	
  Blog	
  	
  |	
  	
  Wiki	
  	
  |	
  Applica8on	
  Server
Redux:	
  App	
  you	
  are	
  building
                                                                                                                                    Sorting




                                                                                                                                              Remove




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

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




Caucho	
  Home	
  	
  |	
  	
  Contact	
  Us	
  	
  |	
  	
  Caucho	
  Blog	
  	
  |	
  	
  Wiki	
  	
  |	
  Applica8on	
  Server
Model	
  2	
  MVC
Model                                                                                                              View                          Controller
                                                                                                                                                   •BookListingServlet
  •Book                                                                                                                 •book-form.jsp             •BookEditorServlet
  •BookRepositoryImpl                                                                                                   •book-list.jsp
  •BookRepository




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




Caucho	
  Home	
  	
  |	
  	
  Contact	
  Us	
  	
  |	
  	
  Caucho	
  Blog	
  	
  |	
  	
  Wiki	
  	
  |	
  Applica8on	
  Server
Edit	
  Link	
  on	
  Title




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




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




Caucho	
  Home	
  	
  |	
  	
  Contact	
  Us	
  	
  |	
  	
  Caucho	
  Blog	
  	
  |	
  	
  Wiki	
  	
  |	
  Applica8on	
  Server
Add	
  Link	
  Above	
  Table




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




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




Caucho	
  Home	
  	
  |	
  	
  Contact	
  Us	
  	
  |	
  	
  Caucho	
  Blog	
  	
  |	
  	
  Wiki	
  	
  |	
  Applica8on	
  Server
BookEditorServlet.doGet	
  loads	
  edit/add	
  form
                                                                                                                                    Add Link


                                                                                                                                    Edit Link




                                                                                                     doGet is load form operation


                                                                                                                                                book-form.jsp




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




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




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




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




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)




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


Caucho	
  Home	
  	
  |	
  	
  Contact	
  Us	
  	
  |	
  	
  Caucho	
  Blog	
  	
  |	
  	
  Wiki	
  	
  |	
  Applica8on	
  Server
BookEditorServlet.doPost()




Caucho	
  Home	
  	
  |	
  	
  Contact	
  Us	
  	
  |	
  	
  Caucho	
  Blog	
  	
  |	
  	
  Wiki	
  	
  |	
  Applica8on	
  Server
BookEditorServlet.doPost
                                                                                                                                                      book-form.jsp




                                                                                                                                    BookListServlet
Caucho	
  Home	
  	
  |	
  	
  Contact	
  Us	
  	
  |	
  	
  Caucho	
  Blog	
  	
  |	
  	
  Wiki	
  	
  |	
  Applica8on	
  Server
Review	
  of	
  CRUD	
  lis8ng
  • Book	
  Form
                                                                                      • Servlet	
  that	
  loads	
  Book	
  (doGet)	
  form	
  
    • ./WebContent/WEB-­‐INF/pages/book-­‐form.jsp	
  	
  	
  	
  	
  	
  	
  	
  	
   and	
  handles	
  Book	
  form	
  submissions	
  
                                                                                                                                                                      (doPost).
  • Book	
  Lis8ng
    • ./WebContent/WEB-­‐INF/pages/book-­‐list.jsp	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
   ./src/com/bookstore/web/
                                                                                                                                                                      BookEditorServlet.java	
  	
  	
  	
  	
  	
  
  • Needed	
  for	
  Java	
  EE	
  dependency	
  injec8on	
  (CDI)
    • ./src/META-­‐INF/beans.xml	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
   • Servlet	
  that	
  looks	
  up	
  a	
  list	
  of	
  books	
  and	
  
                                                                                                                                                                      displays	
  the	
  lis8ng
  • Domain/model	
  object
                                                                                                                                                                      ./src/com/bookstore/web/
    • ./src/com/bookstore/Book.java	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
     BookListServlet.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	
  	
  	
  	
  	
  	
  	
  
Caucho	
  Home	
  	
  |	
  	
  Contact	
  Us	
  	
  |	
  	
  Caucho	
  Blog	
  	
  |	
  	
  Wiki	
  	
  |	
  Applica8on	
  Server
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	
  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

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

More Related Content

PDF
Java EE Servlet JSP Tutorial- Cookbook 1
KEY
Resin Outperforms NginX
PDF
Alfresco monitoring with Nagios and ELK stack
PPTX
Using Apache Camel as AKKA
PDF
Reactor grails realtime web devoxx 2013
PDF
Event Driven Architecture with Apache Camel
PDF
Torquebox OSCON Java 2011
PDF
Torquebox @ Raleigh.rb - April 2011
Java EE Servlet JSP Tutorial- Cookbook 1
Resin Outperforms NginX
Alfresco monitoring with Nagios and ELK stack
Using Apache Camel as AKKA
Reactor grails realtime web devoxx 2013
Event Driven Architecture with Apache Camel
Torquebox OSCON Java 2011
Torquebox @ Raleigh.rb - April 2011

What's hot (20)

PDF
Building Asynchronous Applications
ODP
Developing Microservices with Apache Camel
PDF
ActiveMQ Performance Tuning
PPTX
Full stack development with node and NoSQL - All Things Open - October 2017
PPT
Simplify your integrations with Apache Camel
PDF
Apache Camel - FUSE community day London 2010 presentation
PDF
Profiling JavaScript Performance
PDF
Polyglot Messaging with Apache ActiveMQ
PPTX
Why jakarta ee matters (ConFoo 2021)
PDF
Java Tech & Tools | Grails in the Java Enterprise | Peter Ledbrook
PDF
Apache Camel Introduction & What's in the box
PDF
Crank Up Your Apps With TorqueBox
PDF
Torquebox @ Charlotte.rb May 2011
PDF
Developing, Testing and Scaling with Apache Camel - UberConf 2015
PDF
HyperLight Websites
PPTX
Faster java ee builds with gradle [con4921]
PDF
FITC - Here Be Dragons: Advanced JavaScript Debugging
PDF
A year in the life of a Grails startup
PPTX
WebSocket protocol
PDF
Enterprise Integration Patterns with Apache Camel
Building Asynchronous Applications
Developing Microservices with Apache Camel
ActiveMQ Performance Tuning
Full stack development with node and NoSQL - All Things Open - October 2017
Simplify your integrations with Apache Camel
Apache Camel - FUSE community day London 2010 presentation
Profiling JavaScript Performance
Polyglot Messaging with Apache ActiveMQ
Why jakarta ee matters (ConFoo 2021)
Java Tech & Tools | Grails in the Java Enterprise | Peter Ledbrook
Apache Camel Introduction & What's in the box
Crank Up Your Apps With TorqueBox
Torquebox @ Charlotte.rb May 2011
Developing, Testing and Scaling with Apache Camel - UberConf 2015
HyperLight Websites
Faster java ee builds with gradle [con4921]
FITC - Here Be Dragons: Advanced JavaScript Debugging
A year in the life of a Grails startup
WebSocket protocol
Enterprise Integration Patterns with Apache Camel
Ad

Viewers also liked (20)

PPS
Jsp chapter 1
PPS
Web Component Development with Servlet and JSP Technologies Unit 01
PDF
Iaetsd a survey of various design patterns for improving quality and
PDF
Model-Driven Development of Web Applications
PDF
J2ee (java ee) tutorial for beginners
PDF
Servidores web
PPT
Web engineering 2(lect 0)
PPT
Jsp 2 Research Methods
PPT
Web engineering 2(lect 2)
PDF
OOWS 2.0: A Model-driven Web Engineering Method for the Development of Web 2....
PDF
Créer une application web en asp.net mvc 2
PPTX
Web engineering (2)
PPT
Servlet/JSP course chapter 2: Introduction to JavaServer Pages (JSP)
PDF
Basic Version Control Using Git - Bengkel Gamelan
PPTX
2D Art Dalam Video Game - Kudit
PPT
Online Security - The Good, the Bad, and the Crooks
PDF
Presentasi seminar mobile games
PDF
Bengkel 6 pengetahuan dasar audio pada game (1)
PDF
5 steps into creating your first mobile game
PDF
Bengkel Gamelan : Press Release 101
Jsp chapter 1
Web Component Development with Servlet and JSP Technologies Unit 01
Iaetsd a survey of various design patterns for improving quality and
Model-Driven Development of Web Applications
J2ee (java ee) tutorial for beginners
Servidores web
Web engineering 2(lect 0)
Jsp 2 Research Methods
Web engineering 2(lect 2)
OOWS 2.0: A Model-driven Web Engineering Method for the Development of Web 2....
Créer une application web en asp.net mvc 2
Web engineering (2)
Servlet/JSP course chapter 2: Introduction to JavaServer Pages (JSP)
Basic Version Control Using Git - Bengkel Gamelan
2D Art Dalam Video Game - Kudit
Online Security - The Good, the Bad, and the Crooks
Presentasi seminar mobile games
Bengkel 6 pengetahuan dasar audio pada game (1)
5 steps into creating your first mobile game
Bengkel Gamelan : Press Release 101
Ad

Similar to Java EE Servlet/JSP Tutorial- Cookbook 2 (20)

PDF
Migrating to Jakarta EE 10
PDF
Jeetrainers.com coursejspservlets00
PDF
Coursejspservlets00
PPT
192563547-Servletsjhb,mnjhjhjm,nm,-Pres-ppt.ppt
PPTX
DotNet MVC and webpack + Babel + react
PPTX
Show Some Spine!
PDF
PLAT-8 Spring Web Scripts and Spring Surf
PPTX
React js Online Training
PPSX
React-Js-Online-Training-9028522.ppsx
PPTX
What is flux architecture in react
PPTX
Taverna as a service
PPTX
04 integrate entityframework
PPTX
Dev-Friendly Ops
PDF
Introduction to Cooking with Chef
PPTX
Spring Framework1.pptx java notes collection
PDF
Streaming to a New Jakarta EE
PDF
Streaming to a new Jakarta EE
PPT
Ppt for Online music store
PDF
005432796.pdf
PDF
Threading Made Easy! A Busy Developer’s Guide to Kotlin Coroutines
Migrating to Jakarta EE 10
Jeetrainers.com coursejspservlets00
Coursejspservlets00
192563547-Servletsjhb,mnjhjhjm,nm,-Pres-ppt.ppt
DotNet MVC and webpack + Babel + react
Show Some Spine!
PLAT-8 Spring Web Scripts and Spring Surf
React js Online Training
React-Js-Online-Training-9028522.ppsx
What is flux architecture in react
Taverna as a service
04 integrate entityframework
Dev-Friendly Ops
Introduction to Cooking with Chef
Spring Framework1.pptx java notes collection
Streaming to a New Jakarta EE
Streaming to a new Jakarta EE
Ppt for Online music store
005432796.pdf
Threading Made Easy! A Busy Developer’s Guide to Kotlin Coroutines

Recently uploaded (20)

PPTX
Understanding_Digital_Forensics_Presentation.pptx
PPTX
Cloud computing and distributed systems.
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PPTX
Big Data Technologies - Introduction.pptx
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
MYSQL Presentation for SQL database connectivity
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Empathic Computing: Creating Shared Understanding
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Machine learning based COVID-19 study performance prediction
PDF
cuic standard and advanced reporting.pdf
PPTX
Programs and apps: productivity, graphics, security and other tools
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Approach and Philosophy of On baking technology
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Encapsulation theory and applications.pdf
Understanding_Digital_Forensics_Presentation.pptx
Cloud computing and distributed systems.
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Big Data Technologies - Introduction.pptx
Agricultural_Statistics_at_a_Glance_2022_0.pdf
MYSQL Presentation for SQL database connectivity
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Spectral efficient network and resource selection model in 5G networks
Empathic Computing: Creating Shared Understanding
MIND Revenue Release Quarter 2 2025 Press Release
Machine learning based COVID-19 study performance prediction
cuic standard and advanced reporting.pdf
Programs and apps: productivity, graphics, security and other tools
Digital-Transformation-Roadmap-for-Companies.pptx
Chapter 3 Spatial Domain Image Processing.pdf
Approach and Philosophy of On baking technology
Per capita expenditure prediction using model stacking based on satellite ima...
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Review of recent advances in non-invasive hemoglobin estimation
Encapsulation theory and applications.pdf

Java EE Servlet/JSP Tutorial- Cookbook 2

  • 1. Java  EE  Servlet/JSP  Tutorial Second cookbook, getting started with Model 2: Servlet and JSP Implementing the Edit/Update, Add Caucho  Home    |    Contact  Us    |    Caucho  Blog    |    Wiki    |  Applica8on  Server
  • 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  first • Part  1  Slides • Covers  working  with  Servlet  doGet/doPost  methods,  JSTL,   redirec8on  versus  forwarding Caucho  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   Eclipse Caucho  Home    |    Contact  Us    |    Caucho  Blog    |    Wiki    |  Applica8on  Server
  • 4. Redux:  App  you  are  building Sorting Remove Caucho  Home    |    Contact  Us    |    Caucho  Blog    |    Wiki    |  Applica8on  Server
  • 5. 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 Caucho  Home    |    Contact  Us    |    Caucho  Blog    |    Wiki    |  Applica8on  Server
  • 6. 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 Caucho  Home    |    Contact  Us    |    Caucho  Blog    |    Wiki    |  Applica8on  Server
  • 7. Model  2  MVC Model View Controller •BookListingServlet •Book •book-form.jsp •BookEditorServlet •BookRepositoryImpl •book-list.jsp •BookRepository Caucho  Home    |    Contact  Us    |    Caucho  Blog    |    Wiki    |  Applica8on  Server
  • 8. 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 Caucho  Home    |    Contact  Us    |    Caucho  Blog    |    Wiki    |  Applica8on  Server
  • 9. Edit  Link  on  Title Caucho  Home    |    Contact  Us    |    Caucho  Blog    |    Wiki    |  Applica8on  Server
  • 10. What  gets  rendered • The  following  links  with  URI  (/bookstore)  of  webapp  get  rendered  when  book-­‐ lis8ng.jsp  loads Caucho  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  func8ons Caucho  Home    |    Contact  Us    |    Caucho  Blog    |    Wiki    |  Applica8on  Server
  • 12. Add  Link  Above  Table Caucho  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  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 Caucho  Home    |    Contact  Us    |    Caucho  Blog    |    Wiki    |  Applica8on  Server
  • 14. BookEditorServlet Caucho  Home    |    Contact  Us    |    Caucho  Blog    |    Wiki    |  Applica8on  Server
  • 15. BookEditorServlet.doGet  loads  edit/add  form Add Link Edit Link doGet is load form operation book-form.jsp Caucho  Home    |    Contact  Us    |    Caucho  Blog    |    Wiki    |  Applica8on  Server
  • 16. 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 Caucho  Home    |    Contact  Us    |    Caucho  Blog    |    Wiki    |  Applica8on  Server
  • 17. Model  2  and  BookEditorServlet.doGet  (cont) • To  render  the  HTML  form,  the  servlet  delegates  to  book-­‐form.jsp book-form.jsp Caucho  Home    |    Contact  Us    |    Caucho  Blog    |    Wiki    |  Applica8on  Server
  • 18. 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 Caucho  Home    |    Contact  Us    |    Caucho  Blog    |    Wiki    |  Applica8on  Server
  • 19. book-­‐form.jsp  (2  of  3) • Uses  Unified  EL  to  render  values  and  then  just  plain  HTML  for  form  fields Caucho  Home    |    Contact  Us    |    Caucho  Blog    |    Wiki    |  Applica8on  Server
  • 20. 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) Caucho  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  bookmarking Caucho  Home    |    Contact  Us    |    Caucho  Blog    |    Wiki    |  Applica8on  Server
  • 22. BookEditorServlet.doPost() Caucho  Home    |    Contact  Us    |    Caucho  Blog    |    Wiki    |  Applica8on  Server
  • 23. BookEditorServlet.doPost book-form.jsp BookListServlet Caucho  Home    |    Contact  Us    |    Caucho  Blog    |    Wiki    |  Applica8on  Server
  • 24. Review  of  CRUD  lis8ng • Book  Form • Servlet  that  loads  Book  (doGet)  form   • ./WebContent/WEB-­‐INF/pages/book-­‐form.jsp                   and  handles  Book  form  submissions   (doPost). • Book  Lis8ng • ./WebContent/WEB-­‐INF/pages/book-­‐list.jsp                           ./src/com/bookstore/web/ BookEditorServlet.java             • Needed  for  Java  EE  dependency  injec8on  (CDI) • ./src/META-­‐INF/beans.xml                                                                                 • Servlet  that  looks  up  a  list  of  books  and   displays  the  lis8ng • Domain/model  object ./src/com/bookstore/web/ • ./src/com/bookstore/Book.java                                                                 BookListServlet.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               Caucho  Home    |    Contact  Us    |    Caucho  Blog    |    Wiki    |  Applica8on  Server
  • 25. Open%Source,%Reliable%and%Lightweight% Java%EE%Applica;on%Server% RESIN PRO Web Profile Health System Cloud Support
  • 26. More  Info • 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 Caucho  Home    |    Contact  Us    |    Caucho  Blog    |    Wiki    |  Applica8on  Server