Professional Documents
Culture Documents
0A Servlet+JSP Review
0A Servlet+JSP Review
Servlets, JSP, Struts, JSF/MyFaces/Facelets, Ajax, GWT, Spring, Hibernate/JPA, Java 5 & 6. Developed and taught by well-known author and developer. At public venues or onsite at your location.
For live Java training, please see training courses at http //co rses coreser lets com/ Ser http://courses.coreservlets.com/. Servlets, lets JSP JSP, Str Struts, ts JSF, Ajax, GWT, Java 5, Java 6, Spring, Hibernate, JPA, and customized combinations of topics. p
Taught by the author of Core Servlets and JSP, More Servlets and JSP, and this tutorial tutorial. Available at public venues, or customized versions can be held on-site at your Customized Java EE Training: http://courses.coreservlets.com/ Servlets, JSP, Struts, JSF/MyFaces/Facelets, Ajax, GWT, Spring, Hibernate/JPA, Java 5 & 6. organization. Contact hall@coreservlets.com for details. Developed and taught by well-known author and developer. At public venues or onsite at your location.
Agenda
Purpose of Web applications Structure of Web applications Setting up Web applications with Tomcat Sharing data among Web applications
Portability
All compliant servers support Web apps. Can C redeploy d l on new server by b moving i a single i l file. fil
Separation
Each Web app has its own:
ServletContext Class loader Sessions URL prefix Directory structure
Unjarred beans and utility classes: JAR files: web.xml: web xml:
Tag Library Descriptor files: Files in WEB-INF not directly accessible to clients
7
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 8/2008, 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
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.
Shortcut
If you have made Dynamic Web Project recently in workspace you can just do workspace, File New Dynamic Web Project
11
src/somePackage
Java code in somePackage package
WebContent
Web files ( (HTML, JavaScript, p CSS, JSP, images, etc.)
WebContent/some-subdirectory
Web content in subdirectory
Note
Can cut/paste or drag/drop files into appropriate locations
12
Restart Server
R-click Tomcat at bottom Restart
14
Test servlets
http://localhost/test/servlet/HelloServlet http://localhost/test/servlet/coreservlets.HelloServlet2
15
web.xml
WebContent/WEB-INF
Note N t
16
app-blank/WEB-INF/web.xml app blank/WEB INF/web xml (copy from mine) app-blank/WEB-INF/classes (empty) E copy app-blank E.g., bl k and d call ll it myApp A Web content (HTML , JSP, images, etc.) goes in the top-level directory (myApp) or any subdirectory other than WEB-INF WEB INF (e (e.g., g myApp/someDir) Servlets and other classes go in a subdirectory of WEBINF/classes N /c asses that t at matches atc es the t e package pac age name. a e. On Tomcat, entire directory goes in install_dir/webapps Add webAppDir/WEB-INF/classes to it. Not usually needed if you have ".." in the CLASSPATH
Deployment
Copy py the entire Web app pp directory y to the server's 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 y "Copy". py
CLASSPATH
Must include the top-level development directory
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
18
Resultant URL
http://hostname/webappPrefix/MyAddress
20
<!-- Use the URL http://hostname/intro/hi instead of http://hostname/intro/servlet/HelloServlet --> <servlet> <servlet-name>Second Hello Servlet</servlet-name> <servlet-class>coreservlets.HelloServlet2</servlet-class> </servlet> Fully qualified classname. Any arbitrary name. But must be the same both times. <servlet-mapping> <servlet-name>Second Hello Servlet</servlet-name> <url-pattern>/hi2</url-pattern> l tt /hi2 / l tt </servlet-mapping> The part of the URL that comes after the app (project) name. </web-app> Should start with a slash.
21
Eclipse details
Name of f 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 again, Start
22
You can apply filters and security settings later (via web xml) in a more predictable and controllable manner web.xml) Most importantly of all, you can avoid being added to Martys Hall of Shame
The kiss of death for any self-respecting Java EE developer
23
The Hall of Shame (Deployed Sites with Ugly /servlet/ /servlet/ URLs)
24
Tomcat: just drop WAR file in install_dir/webapps webAppName becomes Web application URL prefix
Wh What t if same image i is i used d by b JSP or HTML pages scattered throughout app?
Same problem
Use web.xml to assign URLs that are at the top level of the Web application
Change http://host/webAppPrefix/servlet/SomeName to just http://host/webAppPrefix/SomeName j p pp More useful for servlets than for JSP
Use getContextPath
Call request.getContextPath() and add result to URLs by hand
27
28
Filters apply to
Servlets, JSP pages, and regular Web content Not Velocity or WebMacro pages
Listeners apply to
29
Servlets, JSP pages, and regular Web content Not Velocity or WebMacro pages
Success:
ServletContext myContext y = getServletContext(); String url = "/someWebAppPrefix"; ServletContext otherContext = myContext.getContext(url); Object someData = otherContext.getAttribute("someKey");
30
31
33
34
Not found: session data Not found: servlet context data from normal servlet context Found: servlet context data when explicitly requesting ti servlet l t context from shareTest1 Not found: cookies that had default path Found: cookies with / as path
Summary
Web application benefits Structure
Easy organization and deployment Isolation from other applications Top-level Top level directory or subdirectory other than WEB-INF: WEB INF: WEB-INF
JSP, HTML, other Web content web.xml
WEB-INF/classes/directoryMatchingPackage
M Make k a new D Dynamic i W Web b project. j Eclipse will create deployment structure automatically. M Make k a di directory t with ith proper structure t t (e.g. ( WEB-INF WEB INF and d WEB-INF/classes subdirectories) Copy to tomcat_dir/webapps.
35
Questions?
Customized Java EE Training: http://courses.coreservlets.com/
Servlets, JSP, Struts, JSF/MyFaces/Facelets, Ajax, GWT, Spring, Hibernate/JPA, Java 5 & 6. Developed and taught by well-known author and developer. At public venues or onsite at your location.