Professional Documents
Culture Documents
An Overview of Servlet
& JSP Technology
Originals of Slides and Source Code for Examples:
http://courses.coreservlets.com/Course-Materials/csajsp2.html
Mainstream
• Popular:
– The single most common use of Java technology
– The leading technology for medium/large Web applications
• Google reports over 500 million Web pages using JSP
• Supported by:
– Apache, Oracle, IBM, Sybase, BEA, Macromedia, Caucho,
Sun/iPlanet, New Atlanta, ATG, Fujitsu, Lutris, Silverstream, the
World Wide Web Consortium ((W3C), ), and manyy others
– Plugins for IIS and Zeus
• Runs on:
– Windows,, Unix/Linux,, MacOS,,
VMS, and IBM mainframe OSs
• Used for:
– Airline companies,
p , hotels,,
e-commerce sites, search engines,
banks, financial sites, etc., etc., etc. Server-side Java is driving the Web
9
Ten Most Popular Web Sites
(Alexa com Fall 2008)
(Alexa.com,
1. Google 6. Windows Live Search
– Custom technology, – .NET
some Java 7. MSN (Microsoft
2. Yahoo Network)
– PHP and Java – .NET
3. MySpace 8. Wikipedia
– ColdFusion – PHP
(Java “under the hood”)
9. Ebay
4 YouTube
4. – Java
J
– Flash, Python, Java
10.AOL
5. Facebook – Java
– PHP
10
13
© 2010 Marty Hall
Testing with
Eclipse
Installing Eclipse
• Overview
–E
Eclipse
li is
i a free
f open-source development
d l environment
i
with support for Java and many other languages
• Downloading
g
– http://www.eclipse.org/downloads/
• Choose “Eclipse IDE for Java EE Developers”
• As of 4/2009, version 3.4, called Eclipse Ganymede
• Installing
– Unzip into directory of your choice
– Put
P shortcut
h to eclipse.exe
li on your desktop
d k
• Integrating Tomcat in Eclipse
– http://www
http://www.coreservlets.com/
coreservlets com/
Apache-Tomcat-Tutorial/eclipse.html
15
Configuring Eclipse
• Make sure Eclipse
k
knows about
b T
Tomcat
– Click on Servers tab at bottom.
R-click in window.
window
– New, Server, Apache, Tomcat v6.0,
Next, navigate to folder, Finish.
• Suppress unnecessary
compiler warnings
– Wi d Preferences
Window P f
Java Compiler
Errors/Warnings
g
• Change “Serializable class
without ...” to “Ignore”
16
17
Adding Code to Eclipse Projects
• Locations
– src
• Unpackaged Java code
• Packages strongly recommended
– src/somePackage
• Java code in somePackage package
– WebContent
• Web files ((HTML, JavaScript,
p
CSS, JSP, images, etc.)
– WebContent/some-subdirectory
• Web content in subdirectory
– WebContent/WEB-INF
W bC t t/WEB INF
• web.xml (will be discussed later)
• Can also click on
“Deployment
p y Descriptor”
p
• Note
– Can cut/paste or drag/drop files into appropriate locations
18
• Eclipse details
– Name off Eclipse
li project
j is i “test”
– Servlet is in src/coreservlets/HelloServlet2.java
– Deployed by right-clicking on Tomcat,
Tomcat Add and Remove
Projects, Add, choosing test project, Finish, right-clicking
24
again, Start
32
33
Test Your Setup (Continued)
• Compile and deploy a packaged servlet that
uses a helper
h l class
l
– Download HelloServlet3.java and ServletUtilities.java
– Place in coreservlets subdirectory of development dir
– Compile (if errors, check CLASSPATH)
– Move both class files to
install_dir/webapps/ROOT/WEB-INF/classes/coreservlets
– Access with
http://localhost/servlet/coreservlets HelloServlet3
http://localhost/servlet/coreservlets.HelloServlet3
34
Establish a Simplified
Deployment Method
• Let your IDE take care of deployment
– See Eclipse directions on earlier slides
• Copy to a shortcut or symbolic link
–MMakek shortcut
h to
install_dir/webapps/ROOT/WEB-INF/classes
– For ppackageless
g servlets,, copy
py .class file to this shortcut
– For packaged servlets, copy entire directory to shortcut
• This is simplest for beginners who don't have an IDE
• Use
U ththe -d
d option
ti off javac
j
– Lets you have source files in one location but
automatically place .class
class files in another location
• Use ant or a similar tool
35 – Ant is especially popular when using custom Web apps
© 2010 Marty Hall
D l i to
Deploying t JHU
Setup
• Install an sftp client
– Google “free sftp client”
• Eclipse also supports sftp via “Remote System Explorer”
perspective. See http://www.eclipse.org/dsdp/tm/
– I use filezilla,, but there are many
y reliable free clients that support
pp
drag-and-drop from your PC to remote server
• In FileZilla, File Site Manager lets you save locations
• If you are prompted, sftp port is 22
• The
Th Art
A t off WAR:
WAR Learn
L to
t create
t WAR fil
files
– R-click project, Export, WAR file (or Export, Web, WAR file)
– You can deploy this WAR file to any Java-capable server
• Or, find the location Eclipse uses for Web apps
– Deploy a project, go to eclipse-workspace/.metadata/ and search for
a wtpwebapps in that project
• On my system it is …\.metadata\.plugins\...\tmp1\wtpwebapps
• Can deploy project folder from here or deploy WAR file
37
Sending Apps to Tomcat on
web4 apl jhu edu
web4.apl.jhu.edu
• Make project starting with your name or ID
– E.g.,
E name your Eclipse
E li project
j t “hall-intro”
“h ll i t ”
– Use same naming scheme all semester
• Deploy from Eclipse and test at home
• http://localhost/hall-intro/Hello.jsp
• http://localhost/hall-intro/servlet/coreservlets.HelloServlet2
• Send app to web4
– Find project
• Find deployed project folder (e.g., “wtpwebapps/hall-intro”)
• Or, build WAR file (e.g., “hall-intro.war”)
– Connect to web4.apl.jhu.edu
– Copy project folder or WAR file to /usr/local/tomcat/webapps
• This is the exact pathname. Do not replace /usr with your id
– Test (only hostname changes!)
• http://web4.apl.jhu.edu/hall-intro/Hello.jsp
38
• http://web4.apl.jhu.edu/hall-intro/servlet/coreservlets.HelloServlet2
40
Questions?