Title: Servlets and JavaServer Pages
1Servlets and JavaServer Pages
- Milt Epstein
- CITES
- mepstein_at_uiuc.edu
2Outline
- Servlets
- Servlet containers
- JavaServer Pages (JSPs)
- Servlet advantages
- Q/A
3So, whats a servlet?
- Javas answer to CGI
- Run on a web server
- Dynamic content
- Middle layer between browser and
databases/applications
4Lets look at some example servlets
5How 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
6Integrating 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
7Tomcat
- Servlet reference implementation
- Can be run standalone
- Can be integrated with web server
- Apache
- IIS, IES (nee NES), others as well
8Integrating 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
9A 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())
10Lets look at some more example servlets
11OK, so whats a JSP?
- JavaServer Page
- Javas answer to ASP
- A text/html page with embedded Java code
(scriptlet)
12Lets look at some example JSPs
13No, 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!
14JSP implicit objects
- Objects available automatically within a JSP
request out pageContext response config sessio
n page application exception
15JSP 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
16JSP tags, XML style
- Comment No equivalent
- Declaration ltjspdeclarationgt
- Expression ltjspexpressiongt
- Code ltjspscriptletgt
- Include
ltjspdirective.include filerelativeURL /gt - Page
ltjspdirective.page pageDirectiveAttrList /gt
17If 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.
18MVC 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
19JSPs 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)
20Contexts/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
21Attributes
- 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
22A 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/
23So, why servlets?
- Efficient
- Persistent
- Convenient
- Powerful/Robust
- Portable
- Secure
- Inexpensive
24The Servlet Life Cycle
- init()
- service()
- doGet()
- doPost()
- doXXX()
- destroy()
25More advanced topics
- Web applications (WARs)
- Database connections (JDBC)
- JSP custom tags
- Filters
- Multi-threading/concurrency
- J2EE/EJB/JMS
26Java Users Group?
27Any questions?