Servlets and JavaServer Pages - PowerPoint PPT Presentation

1 / 27
About This Presentation
Title:

Servlets and JavaServer Pages

Description:

Servlets and JavaServer Pages. Milt Epstein. CITES. mepstein_at_uiuc.edu. Outline. Servlets ... Many servlet containers can be run in standalone mode ... – PowerPoint PPT presentation

Number of Views:49
Avg rating:3.0/5.0
Slides: 28
Provided by: joycebe
Category:

less

Transcript and Presenter's Notes

Title: Servlets and JavaServer Pages


1
Servlets and JavaServer Pages
  • Milt Epstein
  • CITES
  • mepstein_at_uiuc.edu

2
Outline
  • Servlets
  • Servlet containers
  • JavaServer Pages (JSPs)
  • Servlet advantages
  • Q/A

3
So, whats a servlet?
  • Javas answer to CGI
  • Run on a web server
  • Dynamic content
  • Middle layer between browser and
    databases/applications

4
Lets look at some example servlets
  • examples

5
How do I run servlets
  • Need a servlet container runs a JVM as a
    separate process to run the servlets
  • Tomcat
  • part of Apache Jakarta project
  • servlet reference implementation
  • Many others
  • JRun, ServletExec, ...
  • Some available free

6
Integrating with web server
  • Many servlet containers can be run in standalone
    mode
  • Typically they are integrated (like a plug-in)
    into the web server
  • Some web servers (or more fancy application
    servers) have built-in servlet containers
  • Typically this means there are (at least) two
    processes running, the web server itself and the
    servlet container

7
Tomcat
  • Servlet reference implementation
  • Can be run standalone
  • Can be integrated with web server
  • Apache
  • IIS, IES (nee NES), others as well

8
Integrating Tomcat with Apache
  • Need mod_webapp
  • Need to modify httpd.conf

LoadModule webapp_module /usr/local/libexec/mod_we
bapp.so AddModule mod_webapp.c ltIfModule
mod_webapp.cgt WebAppConnection warpConnection
warp localhost8008 WebAppDeploy examples
warpConnection /examples/ WebAppDeploy ROOT
warpConnection /tomcat/ lt/IfModule gt
9
A closer look at a servlet
  • A Java class that extends HttpServlet
  • Compiled and placed in the appropriate directory
    (more on that later)
  • When a request for a servlet arrives, the servlet
    container (a JVM)
  • checks if an instance of that servlet exists
  • if not, it creates/loads an instance
  • calls the servlets service() method (which in
    turn may call doGet()/doPost()/doXXX())

10
Lets look at some more example servlets
  • examples

11
OK, so whats a JSP?
  • JavaServer Page
  • Javas answer to ASP
  • A text/html page with embedded Java code
    (scriptlet)

12
Lets look at some example JSPs
  • examples

13
No, really, whats a JSP?
  • OK, you caught me
  • A JSP is really a servlet!
  • The first time a JSP is requested, its
    translated to a servlet, and then compiled
  • You want proof? It can get pretty ugly!

14
JSP implicit objects
  • Objects available automatically within a JSP

request out pageContext response config sessio
n page application exception
15
JSP tags
  • Comment lt-- comment --gt
  • Declaration lt! declaration gt
    (may contain multiple declarations)
  • Expression lt expression gt
  • Code lt code gt
  • Include lt_at_ include filerelativeURL gt
  • Page lt_at_ page pageDirectiveAttrList gt

16
JSP tags, XML style
  • Comment No equivalent
  • Declaration ltjspdeclarationgt
  • Expression ltjspexpressiongt
  • Code ltjspscriptletgt
  • Include
    ltjspdirective.include filerelativeURL /gt
  • Page
    ltjspdirective.page pageDirectiveAttrList /gt

17
If a JSP is really a servlet, why use JSPs at
all?Because they can lead to a more cleanly
designed, and more easily maintained system.
18
MVC Model, View, Controller
  • Model 1
  • Model 2
  • Blatantly stolen from
    http//www.javaworld.com/javaworld/jw-12-1999/jw-1
    2-ssj-jspmvc_p.html

19
JSPs where to put the code
  • Call code directly
  • Call code indirectly
  • Use beans
  • Use custom tags
  • Use the MVC architecture

(listed in increasing order of complexity of
application)
20
Contexts/web applications
  • The servlet spec allows for the concept of a web
    application
  • Stored in a war (web application archive), a la
    jar and tar
  • Includes all resources that might be needed for
    the web application servlets, JSPs, HTML files,
    text files, images, etc.
  • Normally, you can have intra-context
    communication, but not inter-context
  • Default/ROOT context

21
Attributes
  • Objects that can be attached to request,
    session, or context/application (correspond to
    scope in JSPs)
  • Allow sharing of resources (such as DB connection
    pools), passing data around

22
A closer look at Tomcat
  • Directory structure
  • Server configuration files
  • Context/application configuration files
  • Other servlet containers will work differently
    (but perhaps similarly)

bin/ classes/ common/ conf/ lib/ logs/
server/ webapps/ work/
23
So, why servlets?
  • Efficient
  • Persistent
  • Convenient
  • Powerful/Robust
  • Portable
  • Secure
  • Inexpensive

24
The Servlet Life Cycle
  • init()
  • service()
  • doGet()
  • doPost()
  • doXXX()
  • destroy()

25
More advanced topics
  • Web applications (WARs)
  • Database connections (JDBC)
  • JSP custom tags
  • Filters
  • Multi-threading/concurrency
  • J2EE/EJB/JMS

26
Java Users Group?
  • CUJUG

27
Any questions?
Write a Comment
User Comments (0)
About PowerShow.com