An Example Servlet - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

An Example Servlet

Description:

It returns, in the usual fashion, to the BeerSelect class, which will then ... This is the top-level directory for your web applications ... – PowerPoint PPT presentation

Number of Views:74
Avg rating:3.0/5.0
Slides: 19
Provided by: DavidMa6
Category:
Tags: example | servlet

less

Transcript and Presenter's Notes

Title: An Example Servlet


1
An Example Servlet
  • Putting it all together

2
Credits
  • This is the first example in Head First Servlets
    JSP by Brian Basham, Kathy Sierra, and Bert
    Bates
  • This is an excellent book, and goes into
    considerably more detail than we will in this
    course

3
It starts with an HTML form...
4
The HTML page, 1
lthtmlgt ltheadgt lttitlegtBeer
Selectionlt/titlegt lt/headgt ltbodygt lth1
align"center"gtBeer Selection Pagelt/h1gt
...the form (on the next slide)...
lt/bodygt lt/htmlgt
5
The HTML page, 2
ltform method"POST" action"SelectBeer.do"gt
Select beer characteristicsltpgt Color
ltselect name"color" size"1"gt
ltoptiongtlightlt/optiongt ltoptiongtamberlt/opti
ongt ltoptiongtbrownlt/optiongt
ltoptiongtdarklt/optiongt lt/selectgt ltbrgt
ltbrgt ltcentergt ltinput
type"SUBMIT"gt lt/centergt lt/formgt
6
The deployment descriptor
  • The request goes to the server, with the action
    ltform method"POST" action"SelectBeer.do"gt
  • The name "SelectBeer.do" is not the name of an
    actual file anywhere it is a name given to the
    user
  • Partly, this is for security you dont want the
    user to have access to the actual file without
    going through your form
  • The extension .do is just a convention used by
    this particular book no extension is necessary
  • It is up to the deployment descriptor to find the
    correct servlet to answer this request
  • The deployment descriptor must be named web.xml

7
web.xml 1 -- boilerplate
lt?xml version"1.0" encoding"ISO-8859-1"?gt ltweb-
app xmlns"http//java.sun.com/xml/ns/j2ee"
xmlnsxsi"http//www.w3.org/2001/XMLSchema-ins
tance" xsischemaLocation"http//java.su
n.com/xml/ns/j2ee/web-app_2_4.xsd"
version"2.4"gt ...important stuff goes
here... lt/web-appgt
8
web.xml 2 -- actual work
ltservletgt ltservlet-namegtCh3
Beerlt/servlet-namegt ltservlet-classgt
com.example.web.BeerSelect lt/servlet-classgt lt/
servletgt ltservlet-mappinggt
ltservlet-namegtCh3 Beerlt/servlet-namegt
lturl-patterngt/SelectBeer.dolt/url-patterngt lt/servle
t-mappinggt
9
BeerSelect.java 1
package com.example.web import
javax.servlet. import javax.servlet.http. impo
rt java.io. import java.util. import
com.example.model.BeerExpert // notice
this public class BeerSelect extends HttpServlet
... doPost method goes here. ..
10
BeerSelect.java 2
public void doPost(HttpServletRequest
request,
HttpServletResponse response) throws
IOException, ServletException
String c request.getParameter("color")
BeerExpert be new BeerExpert()
List result be.getBrands(c)
request.setAttribute("styles", result)
RequestDispatcher view
request.getRequestDispatcher("result.jsp")
view.forward(request, response)
11
MVC
  • BeerSelect.java acts as the controller
  • It delegates the actual work to a model,
    BeerExpert.java
  • It delegates (forwards) the information to a JSP
    page that will provide the view
  • RequestDispatcher view
    request.getRequestDispatcher("result.jsp")view.f
    orward(request, response)

12
The model class
  • BeerExpert is the model class it computes
    results and adds them to the HttpServletRequest
    object
  • Not the HttpServletResponse object thats the
    HTML output
  • It returns, in the usual fashion, to the
    BeerSelect class, which will then forward it to
    the JSP

13
BeerExpert.java
package com.example.model import
java.util. public class BeerExpert
public List getBrands(String color)
List brands new ArrayList() if
(color.equals("amber"))
brands.add("Jack Amber")
brands.add("Red Moose") else
brands.add("Jail Pale Ale")
brands.add("Gout Stout")
return brands
14
The JSP file
  • The JSP file must have the extension .jsp
  • It is basically HTML, plus a few JSP directives
  • It receives the HttpServletRequest and the
    HttpServletResponse objects
  • The HttpServletResponse object may have been
    partially written by the servlet (but its a bad
    idea)
  • The resultant HTML page goes back to the user

15
result.jsp
lt_at_ page import"java.util." gt lthtmlgt ltbodygt lth
1 align"center"gtBeer Recommendations
JSPlt/h1gt ltpgt lt List styles
(List)request.getAttribute("styles") Iterator
it styles.iterator() while (it.hasNext())
out.print("ltbrgtTRY " it.next())
gt lt/bodygt lt/htmlgt
16
Directory structure
jakarta-tomcat-5.0.12/ webapps/ ? this is
http//m174pc4.cis.upenn.edu8080/
beerV1/ form.html
result.jsp WEB-INF/
web.xml classes/
com/
example/
model/
BeerExpert.class
web/
BeerSelect.class lib/
yourLastName ? when you ftp, this is where you
are
17
Accessing the class server
  • Tomcat should be running 24/7 on
    m174pc4.cis.upenn.edu
  • To try it, point your browser tohttp//m174pc4.c
    is.upenn.edu8080/beerV1/form.html
  • When you ftp to m174pc4, pwd will tell you that
    you are in a directory /, but you are really in
    a directoryC\Tomcat\webapps\yourLastName
  • This is the top-level directory for your web
    applications
  • You should be able to put an HTML file here, say,
    index.html, and access it withhttp//m174pc4.cis.
    upenn.edu8080/yourLastName/index.html

18
The End
Write a Comment
User Comments (0)
About PowerShow.com