Java	  EE	  Servlet/JSP	  Tutorial                                                       Second cookbook, getting started ...
Cookbook:	  Intro	  to	  Serlvets	  and	  JSP              • This cookbook in the Java EE Servlet /JSP tutorial           ...
Redux:	  About	  tutorial              • Very	  liNle	  knowledge	  of	  HTML,	  Java	  and	  JSP	  is	  assumed          ...
Redux:	  App	  you	  are	  building                                                                                       ...
Outline           • 1 Java EE Servlet Tutorial: Implementing a basic CRUD listing           • 2 Adding a link to the book ...
Where	  we	  leX	  off	  in	  last	  example              • BookListServlet	  uses	  a	  BookRepository	  object	  (DAO)	  ...
Model	  2	  MVCModel                                                                                                      ...
Add	  a	  new	  link	  to	  book-­‐lis8ng.jsp             • Add	  link	  to	  edit	  opera8on             • Edit	  opera8o...
Edit	  Link	  on	  TitleCaucho	  Home	  	  |	  	  Contact	  Us	  	  |	  	  Caucho	  Blog	  	  |	  	  Wiki	  	  |	  Applica...
What	  gets	  rendered             • The	  following	  links	  with	  URI	  (/bookstore)	  of	  webapp	  get	  rendered	  ...
Adding	  a	  “add	  a	  book”	  link	  to	  book	  lis8ng     • Now	  that	  links	  are	  going	  to	  URI	  /book,	     ...
Add	  Link	  Above	  TableCaucho	  Home	  	  |	  	  Contact	  Us	  	  |	  	  Caucho	  Blog	  	  |	  	  Wiki	  	  |	  Appli...
BookEditorServlet              • @WebServlet("/book")	  maps	  BookEditorServlet	  to	  the	  URI	  /                     ...
BookEditorServletCaucho	  Home	  	  |	  	  Contact	  Us	  	  |	  	  Caucho	  Blog	  	  |	  	  Wiki	  	  |	  Applica8on	  S...
BookEditorServlet.doGet	  loads	  edit/add	  form                                                                         ...
Model	  2	  and	  BookEditorServlet.doGet             • In	  Model	  2,	  Servlets	  (controllers/ac8ons)	  prepares	  mod...
Model	  2	  and	  BookEditorServlet.doGet	  (cont)             • To	  render	  the	  HTML	  form,	  the	  servlet	  delega...
book-­‐form.jsp	  (1	  of	  3)             • book-­‐form.jsp	  renders	  form	  to	  edit	  book	               • If	  boo...
book-­‐form.jsp	  (2	  of	  3)             • Uses	  Unified	  EL	  to	  render	  values	  and	  then	  just	  plain	  HTML	...
book-­‐form.jsp	  (3	  of	  3)             • hidden	  id	  property	  is	  rendered	  if	  edit	  (Update)	  opera8on     ...
BookEditorServlet.doPost()              • if	  id	  request	  parameter	  is	  null	  then	  BookEditorServlet.doPost	    ...
BookEditorServlet.doPost()Caucho	  Home	  	  |	  	  Contact	  Us	  	  |	  	  Caucho	  Blog	  	  |	  	  Wiki	  	  |	  Appli...
BookEditorServlet.doPost                                                                                                  ...
Review	  of	  CRUD	  lis8ng  • Book	  Form                                                                                ...
Open%Source,%Reliable%and%Lightweight%       Java%EE%Applica;on%Server%RESIN PRO   Web Profile   Health System   Cloud Sup...
More	  Info         • Caucho	  Technology	  |	  Home	  Page         • Resin	  |	  Applica8on	  Server         • Resin	  |	...
Upcoming SlideShare
Loading in...5
×

Java EE Servlet/JSP Tutorial- Cookbook 2

6,036

Published on

http://wiki4.caucho.com/Java_EE_Tutorial_covering_JSP_2.2,_and_Servlets_3.0

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.

Published in: Technology, Education
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
6,036
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
255
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Transcript of "Java EE Servlet/JSP Tutorial- Cookbook 2"

  1. 1. Java  EE  Servlet/JSP  Tutorial Second cookbook, getting started with Model 2: Servlet and JSP Implementing the Edit/Update, AddCaucho  Home    |    Contact  Us    |    Caucho  Blog    |    Wiki    |  Applica8on  Server
  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  first • Part  1  Slides • Covers  working  with  Servlet  doGet/doPost  methods,  JSTL,   redirec8on  versus  forwardingCaucho  Home    |    Contact  Us    |    Caucho  Blog    |    Wiki    |  Applica8on  Server
  3. 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
  4. 4. Redux:  App  you  are  building Sorting RemoveCaucho  Home    |    Contact  Us    |    Caucho  Blog    |    Wiki    |  Applica8on  Server
  5. 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 TutorialsCaucho  Home    |    Contact  Us    |    Caucho  Blog    |    Wiki    |  Applica8on  Server
  6. 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  lis9ngCaucho  Home    |    Contact  Us    |    Caucho  Blog    |    Wiki    |  Applica8on  Server
  7. 7. Model  2  MVCModel View Controller •BookListingServlet •Book •book-form.jsp •BookEditorServlet •BookRepositoryImpl •book-list.jsp •BookRepositoryCaucho  Home    |    Contact  Us    |    Caucho  Blog    |    Wiki    |  Applica8on  Server
  8. 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  containerCaucho  Home    |    Contact  Us    |    Caucho  Blog    |    Wiki    |  Applica8on  Server
  9. 9. Edit  Link  on  TitleCaucho  Home    |    Contact  Us    |    Caucho  Blog    |    Wiki    |  Applica8on  Server
  10. 10. What  gets  rendered • The  following  links  with  URI  (/bookstore)  of  webapp  get  rendered  when  book-­‐ lis8ng.jsp  loadsCaucho  Home    |    Contact  Us    |    Caucho  Blog    |    Wiki    |  Applica8on  Server
  11. 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
  12. 12. Add  Link  Above  TableCaucho  Home    |    Contact  Us    |    Caucho  Blog    |    Wiki    |  Applica8on  Server
  13. 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"  opera9onCaucho  Home    |    Contact  Us    |    Caucho  Blog    |    Wiki    |  Applica8on  Server
  14. 14. BookEditorServletCaucho  Home    |    Contact  Us    |    Caucho  Blog    |    Wiki    |  Applica8on  Server
  15. 15. BookEditorServlet.doGet  loads  edit/add  form Add Link Edit Link doGet is load form operation book-form.jspCaucho  Home    |    Contact  Us    |    Caucho  Blog    |    Wiki    |  Applica8on  Server
  16. 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.jspCaucho  Home    |    Contact  Us    |    Caucho  Blog    |    Wiki    |  Applica8on  Server
  17. 17. Model  2  and  BookEditorServlet.doGet  (cont) • To  render  the  HTML  form,  the  servlet  delegates  to  book-­‐form.jsp book-form.jspCaucho  Home    |    Contact  Us    |    Caucho  Blog    |    Wiki    |  Applica8on  Server
  18. 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   opera9onCaucho  Home    |    Contact  Us    |    Caucho  Blog    |    Wiki    |  Applica8on  Server
  19. 19. book-­‐form.jsp  (2  of  3) • Uses  Unified  EL  to  render  values  and  then  just  plain  HTML  for  form  fieldsCaucho  Home    |    Contact  Us    |    Caucho  Blog    |    Wiki    |  Applica8on  Server
  20. 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. 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
  22. 22. BookEditorServlet.doPost()Caucho  Home    |    Contact  Us    |    Caucho  Blog    |    Wiki    |  Applica8on  Server
  23. 23. BookEditorServlet.doPost book-form.jsp BookListServletCaucho  Home    |    Contact  Us    |    Caucho  Blog    |    Wiki    |  Applica8on  Server
  24. 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. 25. Open%Source,%Reliable%and%Lightweight% Java%EE%Applica;on%Server%RESIN PRO Web Profile Health System Cloud Support
  26. 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  VideoCaucho  Home    |    Contact  Us    |    Caucho  Blog    |    Wiki    |  Applica8on  Server
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×