Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our User Agreement and Privacy Policy.

Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our Privacy Policy and User Agreement for details.

9.
Manual Web App Development
Strategy with TomcatStrategy with Tomcat
• Development
– Keep the original of your Web app directory in your
development directory. Have all the files in the proper
location within that Web app directory.location within that Web app directory.
• Deployment
– Copy the entire Web app directory to the server'spy pp y
deployment location (e.g., to install_dir/webapps).
• I keep a shortcut to webapps and drag the Web app dir
onto the shortcut with the R mouse and then say "Copy".y py
• CLASSPATH
– Must include the top-level development directory
18
• That now means WEB-INF/classes dir of your Web app
• If your CLASSPATH has "..", you can leave CLASSPATH
unchanged as long as you avoid nested packages
Changing the Web App Prefix
• Eclipse default: project name is Web App
prefixprefix
– So, if project is named foo, when you deploy locally the
URL is http://localhost/foo/whatever
• Tomcat default: folder name is Web App
prefix
So if you deploy the folder bar to tomcat dir/webapps– So, if you deploy the folder bar to tomcat_dir/webapps,
the URL is http://localhost/bar/whatever.
• Custom prefix in Eclipse
– R-click on project, then Properties  Web Project
Settings  Context Root
• Custom prefix in Tomcat
19
Custom prefix in Tomcat
– Edit tomcat_dir/conf/server.xml

11.
Defining Custom URLs: Result
• Eclipse details
f li j i– Name of Eclipse project is "test"
– Servlet is in src/coreservlets/HelloServlet2.java
– Deployed by right-clicking on Tomcat Add and Remove
22
– Deployed by right-clicking on Tomcat, Add and Remove
Projects, Add, choosing test project, Finish, right-clicking
again, Start
Failing to Define Custom URLs
• You should always use custom URLs on
d l d jdeployed projects
– URLs look cleaner and simpler and shorter
URLs have more meaningful names– URLs have more meaningful names
– You don't expose possibly proprietary class file names
– You can use web.xml to assign init params laterg p
• Does not work with …/servlet/myPackage.MyServlet
– You can apply filters and security settings later (via
web xml) in a more predictable and controllable mannerweb.xml) in a more predictable and controllable manner
– Most importantly of all, you can avoid being added to
Marty’s “Hall of Shame”
23
• The kiss of death for any self-respecting Java EE
developer

12.
The Hall of Shame (Deployed Sites
with Ugly /servlet/ URLs)with Ugly …/servlet/… URLs)
24
The Art of WAR (Files)
• WAR files are simply JAR files with a different file
extensionextension
– And JAR files are simply ZIP files
• All servers are required to support Web apps that
are in WAR filesare in WAR files
– Technically, they are not absolutely required to support unbundled
Web apps.
• To create a WAR file change directory to top level• To create a WAR file, change directory to top-level
Web app directory and do:
– jar cvf webAppName.war *
O Wi Zi ( "C t C d F ld " XP)– Or use WinZip (or "Create Compressed Folder" on XP)
– Eclipse can build WAR files automatically
• R-click project, Export  WAR file
• Registering is still server specific
25
• Registering is still server-specific
– Tomcat: just drop WAR file in install_dir/webapps
– webAppName becomes Web application URL prefix

13.
Handling Relative URLs:
ProblemProblem
• Individual JSP or HTML page: easy to load
i f l i l iimage from relative location
– <IMG SRC="EvilEmpire.gif" …>
<IMG SRC="redmond/EvilEmpire jpg" >– <IMG SRC= redmond/EvilEmpire.jpg …>
• What about servlets?
– Same strategy doesn't workgy
– Default servlet URL: http://host/prefix/servlet/Name
– Browser, not server, resolves relative URL
Wh t if i i d b JSP HTML• What if same image is used by JSP or HTML
pages scattered throughout app?
– Same problem
26
Same problem
• Also same problem:
– Style sheets, applets, even regular hypertext links
Handling Relative URLs:
SolutionsSolutions
• Use the Web application name in the URL.
– <IMG SRC="/webAppPrefix/redmond/Borg.gif" ...>
• Use web.xml to assign URLs that are at the
top level of the Web applicationtop level of the Web application
– Change http://host/webAppPrefix/servlet/SomeName to
just http://host/webAppPrefix/SomeNamej p pp
– More useful for servlets than for JSP
• Use getContextPath
– Call request.getContextPath()
and add result to URLs by hand
27