Java EE Servlet/JSP Tutorial- Cookbook 2
Upcoming SlideShare
Loading in...5
×
 

Java EE Servlet/JSP Tutorial- Cookbook 2

on

  • 5,901 views

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

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.

Statistics

Views

Total Views
5,901
Views on SlideShare
5,899
Embed Views
2

Actions

Likes
1
Downloads
228
Comments
0

2 Embeds 2

https://twimg0-a.akamaihd.net 1
https://si0.twimg.com 1

Accessibility

Upload Details

Uploaded via as Adobe PDF

Usage Rights

CC Attribution-ShareAlike LicenseCC Attribution-ShareAlike License

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Java EE Servlet/JSP Tutorial- Cookbook 2 Java EE Servlet/JSP Tutorial- Cookbook 2 Presentation Transcript

  • 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
  • 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
  • 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
  • Redux:  App  you  are  building Sorting RemoveCaucho  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 TutorialsCaucho  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  lis9ngCaucho  Home    |    Contact  Us    |    Caucho  Blog    |    Wiki    |  Applica8on  Server
  • Model  2  MVCModel View Controller •BookListingServlet •Book •book-form.jsp •BookEditorServlet •BookRepositoryImpl •book-list.jsp •BookRepositoryCaucho  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  containerCaucho  Home    |    Contact  Us    |    Caucho  Blog    |    Wiki    |  Applica8on  Server
  • Edit  Link  on  TitleCaucho  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  loadsCaucho  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  func8onsCaucho  Home    |    Contact  Us    |    Caucho  Blog    |    Wiki    |  Applica8on  Server
  • Add  Link  Above  TableCaucho  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"  opera9onCaucho  Home    |    Contact  Us    |    Caucho  Blog    |    Wiki    |  Applica8on  Server
  • BookEditorServletCaucho  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.jspCaucho  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.jspCaucho  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.jspCaucho  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   opera9onCaucho  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  fieldsCaucho  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  bookmarkingCaucho  Home    |    Contact  Us    |    Caucho  Blog    |    Wiki    |  Applica8on  Server
  • BookEditorServlet.doPost()Caucho  Home    |    Contact  Us    |    Caucho  Blog    |    Wiki    |  Applica8on  Server
  • BookEditorServlet.doPost book-form.jsp BookListServletCaucho  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  VideoCaucho  Home    |    Contact  Us    |    Caucho  Blog    |    Wiki    |  Applica8on  Server