SlideShare a Scribd company logo
© 2010 Marty Hall
An Overview of ServletAn Overview of Servlet
& JSP Technology
Originals of Slides and Source Code for Examples:
http://courses.coreservlets.com/Course-Materials/csajsp2.html
Customized Java EE Training: http://courses.coreservlets.com/
Servlets, JSP, JSF 2.0, Struts, Ajax, GWT 2.0, Spring, Hibernate, SOAP & RESTful Web Services, Java 6.
Developed and taught by well-known author and developer. At public venues or onsite at your location.3
© 2010 Marty Hall
For live Java EE training, please see training courses
at http://courses.coreservlets.com/.at http://courses.coreservlets.com/.
Servlets, JSP, Struts, JSF 1.x, JSF 2.0, Ajax (with jQuery, Dojo,
Prototype, Ext-JS, Google Closure, etc.), GWT 2.0 (with GXT),
Java 5, Java 6, SOAP-based and RESTful Web Services, Spring,g
Hibernate/JPA, and customized combinations of topics.
Taught by the author of Core Servlets and JSP, More
Servlets and JSP and this tutorial Available at public
Customized Java EE Training: http://courses.coreservlets.com/
Servlets, JSP, JSF 2.0, Struts, Ajax, GWT 2.0, Spring, Hibernate, SOAP & RESTful Web Services, Java 6.
Developed and taught by well-known author and developer. At public venues or onsite at your location.
Servlets and JSP, and this tutorial. Available at public
venues, or customized versions can be held on-site at your
organization. Contact hall@coreservlets.com for details.
Agenda
• What servlets and JSP are all about
– Understanding the role of servlets
– Building Web pages dynamically
– Evaluating servlets vs. other technologies
– Understanding the role of JSP
T i T i h E li• Testing Tomcat with Eclipse
– Installing Tomcat
I lli d i E li– Installing and starting Eclipse
– Telling Eclipse about Tomcat
Deploying and running Web apps from Eclipse– Deploying and running Web apps from Eclipse
– Making new Web apps in Eclipse
5
© 2010 Marty Hall
What Servlets and JSPWhat Servlets and JSP
are All About
Customized Java EE Training: http://courses.coreservlets.com/
Servlets, JSP, JSF 2.0, Struts, Ajax, GWT 2.0, Spring, Hibernate, SOAP & RESTful Web Services, Java 6.
Developed and taught by well-known author and developer. At public venues or onsite at your location.6
A Servlet’s Job
• Read explicit data sent by client (form data)
• Read implicit data sent by client
(request headers)
G t th lt• Generate the results
• Send the explicit data back to client (HTML)
S d th i li it d t t li t• Send the implicit data to client
(status codes and response headers)
7
Why Build Web Pages
Dynamically?Dynamically?
• The Web page is based on data submitted
b hby the user
– E.g., results page from search engines and order-
confirmation pages at on-line storesconfirmation pages at on line stores
• The Web page is derived from data that
changes frequentlyg q y
– E.g., a weather report or news headlines page
• The Web page uses information from
d t b th iddatabases or other server-side sources
– E.g., an e-commerce site could use a servlet to build a
Web page that lists the current price and availability ofWeb page that lists the current price and availability of
each item that is for sale.
8
The Advantages of Servlets
Over “Traditional” CGIOver Traditional CGI
• Efficient
Th d i t d f OS l t– Threads instead of OS processes, one servlet copy
• Convenient
– Lots of high-level utilities
• Powerful
– Sharing data, pooling, persistence
• PortablePortable
– Run on virtually all operating systems and servers
• Inexpensive
There are plenty of free and low cost servers– There are plenty of free and low-cost servers
• Secure
– No shell escapes,
b ff flno buffer overflows
• Mainstream
– See next page9 From Randall Munroe and xkcd.com
Mainstream
• Popular:
The single most common use of Java technology– The single most common use of Java technology
– The leading technology for medium/large Web applications
• Google reports over 650 million Web pages using JSP
• Supported by:Supported by:
– Apache, Oracle, IBM, Sybase, BEA, Jetty, Caucho, Sun, New
Atlanta, ATG, Fujitsu, Lutris, Silverstream, the World Wide Web
Consortium (W3C), and many others( ), y
– Plugins for IIS and Zeus
• Runs on:
– Windows, Unix/Linux, MacOS,, , ,
VMS, and IBM mainframe OSs
• Used for:
– Airline companies, hotels,p , ,
e-commerce sites, search engines,
banks, financial sites, etc., etc., etc.
10
Server-side Java is driving the Web
Ten Most Popular Web Sites
(Alexa com Summer 2010)(Alexa.com, Summer 2010)
1. Google 6. Baidu
– Java (Web),
C++ (indexing)
2. Facebook
– Unknown
7. Wikipedia
PHP2. Facebook
– PHP
3. YouTube
– PHP
8. Blogger
– Java
– Flash, Python, Java
4. Yahoo
PHP and Java
9. MSN
– .NET
10 T itt– PHP and Java
5. Microsoft Live.com
– .NET
10.Twitter
– Ruby on Rails, Scala, Java
11
Keywords in Job Postings
12
Extending the Power of Servlets:
JavaServer Pages (JSP)JavaServer Pages (JSP)
• Idea:
Use regular HTML for most of page– Use regular HTML for most of page
– Mark dynamic content with special tags
– Details in second half of course
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD><TITLE>Welcome to Our Store</TITLE></HEAD>
<BODY><BODY>
<H1>Welcome to Our Store</H1>
<SMALL>Welcome,
<!-- User name is "New User" for first-time visitors -->
<%= coreservlets.Utils.getUserNameFromCookie(request) %>
To access your account settings, click
<A HREF="Account-Settings.html">here.</A></SMALL>
<P>
Regular HTML for rest of on-line store’s Web page
</BODY></HTML>
13
Accessing the Online
DocumentationDocumentation
• Servlets and JSP
htt //d l t / l t 3 0 i/– http://docs.coreservlets.com/servlet-3.0-api/
• Servlets 3.0 and JSP 2.2 (Tomcat 7)
– http://java.sun.com/products/servlet/2.5/docs/servlet-2_5-mr2/
S l t 2 5 (T t 6)• Servlets 2.5 (Tomcat 6)
– http://java.sun.com/products/jsp/2.1/docs/jsp-2_1-pfd2/
• JSP 2.1 (Tomcat 6)
J 6• Java 6
– http://java.sun.com/javase/6/docs/api/
• Class uses Java 6 and Tomcat 7C ass uses Ja a 6 a d o cat
• Advice
– If you have a fast and reliable internet connection,
bookmark these addressesbookmark these addresses
– If not, download a copy of the APIs onto your local
machine and use it14
Setting Up Tomcat on Your PC
• Tomcat 7 with Eclipse
– http://www.coreservlets.com/Apache-Tomcat-Tutorial/
tomcat-7-with-eclipse.html
• Or just follow link at top left of www coreservlets comOr, just follow link at top left of www.coreservlets.com
• More details in next section of this tutorial
• Tomcat 6 with Eclipse
– http://www.coreservlets.com/Apache-Tomcat-Tutorial/
• For manual execution
h // l /A h T T i l/– http://www.coreservlets.com/Apache-Tomcat-Tutorial/
• More details in last section.
• Eclipse or another IDE strongly recommended
• Bottom line
– Unzip Tomcat, then point Eclipse at the install folder15
© 2010 Marty Hall
I t lli T tInstalling Tomcat
For even more detailed step-by-step instructions, see tutorials on using Eclipse with
Tomcat 6 or Tomcat 7 at http://www.coreservlets.com/Apache-Tomcat-Tutorial/
Customized Java EE Training: http://courses.coreservlets.com/
Servlets, JSP, JSF 2.0, Struts, Ajax, GWT 2.0, Spring, Hibernate, SOAP & RESTful Web Services, Java 6.
Developed and taught by well-known author and developer. At public venues or onsite at your location.16
Download and Unzip
• Start at http://tomcat.apache.org
– Choose download link on left, then ZIP version
• Tomcat 7 (recommended)
• Tomcat 6 (if you need compatibility with older servers)Tomcat 6 (if you need compatibility with older servers)
• Or, go to http://www.coreservlets.com/
– Choose Tomcat tutorial from top left
– This is preconfigured version
• Set for development, not deployment mode
Port changed to 80 servlet reloading enabled directory listings– Port changed to 80, servlet reloading enabled, directory listings
turned on, etc.
• Otherwise unchanged
• Either way just unzip the file• Either way, just unzip the file
– E.g., resulting in C:apache-tomcat-7.0.0
17
© 2010 Marty Hall
I t lli E liInstalling Eclipse
For even more detailed step-by-step instructions, see tutorials on using Eclipse with
Tomcat 6 or Tomcat 7 at http://www.coreservlets.com/Apache-Tomcat-Tutorial/
Customized Java EE Training: http://courses.coreservlets.com/
Servlets, JSP, JSF 2.0, Struts, Ajax, GWT 2.0, Spring, Hibernate, SOAP & RESTful Web Services, Java 6.
Developed and taught by well-known author and developer. At public venues or onsite at your location.18
Installing Eclipse
• Overview
– Eclipse is a a free open source IDE– Eclipse is a a free open source IDE
for Java. Support for Java, HTML,
CSS, JavaScript, C++, PHP, and
more.
http://eclipse org/downloads/• http://eclipse.org/downloads/
• Choose “Eclipse IDE for Java EE
Developers”
– Need version 3.6 (Helios) for Tomcat 7
F t• Features
– Checks your syntax as you type
– Automatically compiles every
time you save filetime you save file
– Many tools: refactoring,
debugging, server integration,
templates for common tasks, etc.p
• Low learning curve: beginners can
use Eclipse without knowing these
tools
Reminder: step-by-step guide at http://www.coreservlets.com/ (click “Apache Tomcat 7” in top left).
Running Eclipse
• Unzip the downloaded file
– Call the folder you unzip into “installDir”
• Double click eclipse.exe
F i llDi /bi– From installDir/bin
• Click on
“Workbench” iconWorkbench icon
– Next time you bring
up Eclipse, it will
come up in workbench automatically
• Shortcut
M d l t E li li k th i d kt– Many developers put Eclipse link on their desktop
• R-click eclipse.exe, Copy, then go to desktop, R-click, and
Paste Shortcut (not just Paste!)
Configuring Eclipse
• Tell Eclipse about Java version
Wi d  P f  J – Window  Preferences  Java 
Installed JREs  Press “Add”, choose
“Standard VM”, navigate to JDK folder
(not “bin” subdirectory)(not bin subdirectory)
• E.g., C:Program FilesJavajdk1.6.0_21
• Tell Eclipse about Tomcat
Cli k S b b– Click on Servers tab at bottom.
R-click in window.
– New, Server, Apache, Tomcat v7.0,
N t i t t f ld Fi i hNext, navigate to folder, Finish.
• Suppress serializable warnings
– Window  Preferences  Java 
Compiler  Errors/Warnings
• Change “Serializable class
without ...” to “Ignore”21
Tomcat v7.0 is choice only in Eclipse 3.6 (Helios). If you
prefer Tomcat 6, choose Tomcat v6.0 above instead. If
you lose the “Servers” tab at the bottom of Eclipse, use
Window, Show View, and hunt for “Servers”.
© 2010 Marty Hall
Deploying AppsDeploying Apps
from Eclipse
Customized Java EE Training: http://courses.coreservlets.com/
Servlets, JSP, JSF 2.0, Struts, Ajax, GWT 2.0, Spring, Hibernate, SOAP & RESTful Web Services, Java 6.
Developed and taught by well-known author and developer. At public venues or onsite at your location.22
Download and Import Sample
ProjectProject
• Get test-app.zip from coreservlets.com
– Start at servlet and JSP tutorials
• http://courses.coreservlets.com/
Course-Materials/csajsp2.htmlj p
– Go to first section (Overview and Setup)
– Or, start at Apache Tomcat tutorial
• http://www coreservlets com/Apache Tomcat Tutorial/• http://www.coreservlets.com/Apache-Tomcat-Tutorial/
– Choose Tomcat 7 (recommended) or Tomcat 6 version
• Then, download test-app.zip
– Then, import into Eclipse.
• File, Import, General, Existing
Projects, Select archive file.Projects, Select archive file.
Then click Browse and navigate
to test-app.zip.
23
Deploying App in Eclipse
• Deploy project
– Select “Servers” tab at bottom
– R-click on Tomcat
– Choose “Add and Remove”Choose Add and Remove
– Choose project
– Press “Add”
– Click “Finish”
• Start Server
R click Tomcat at bottom– R-click Tomcat at bottom
– Start (use “Restart” if
Tomcat already running)
• Test URL
– http://localhost/test-app/ in any Web browser
24
Testing Deployed App in Eclipse
• Start a browser
– Eclipse also has builtin browser,
but I prefer to use Firefox or
Internet Explorer
• Test base URL
– http://localhost/test-app/
T t W b t t• Test Web content
– http://localhost/test-app/hello.html
– http://localhost/test-app/hello.jsphttp://localhost/test app/hello.jsp
• Test servlets
– http://localhost/test-app/hello
– http://localhost/test-app/test1
– http://localhost/test-app/test2
25
© 2010 Marty Hall
Making New AppsMaking New Apps
from Eclipse
Customized Java EE Training: http://courses.coreservlets.com/
Servlets, JSP, JSF 2.0, Struts, Ajax, GWT 2.0, Spring, Hibernate, SOAP & RESTful Web Services, Java 6.
Developed and taught by well-known author and developer. At public venues or onsite at your location.26
Making Web Apps in Eclipse
• Make empty project
– File  New  Project 
Web  Dynamic Web Project
– For “Target runtime” choose– For Target runtime , choose
“Apache Tomcat v7.0”
– Give it a name (e.g., “test”)
– Accept all other defaults
• Shortcut
If h d D i– If you have made Dynamic
Web Project recently in
workspace, you can just dop y j
File  New 
Dynamic Web Project
27
Adding Code to Eclipse Projects
• Locations
Java Resources: src– Java Resources: src
• R-click and New  Package
• Never use default package
– src/testPackagesrc/testPackage
• Java code in testPackage package
– WebContent
• Web files (HTML, JavaScript,
CSS JSP i t )CSS, JSP, images, etc.)
– WebContent/some-subdirectory
• Web content in subdirectory
• R-click on WebContent New  Folder• R-click on WebContent, New  Folder
– WebContent/WEB-INF
• web.xml
– Optional with servlets 3.0. Required in 2.5 & earlier.p q
– Will be discussed later
• Note
– Can cut/paste or drag/drop files into appropriate locations28
Testing New App
• Follow same procedure as “deploying app”
f i ifrom previous section
– Deploy project
• Select “Servers” tab at bottomSelect Servers tab at bottom
• R-click on Tomcat
• Choose “Add and Remove”
• Choose project• Choose project
• Press “Add”
• Click “Finish”
Start Server– Start Server
• R-click Tomcat at bottom
• Restart (use “Start” if Tomcat not already running)
– Test URL
• http://localhost/appName/ in any Web browser
29
Summary
• General
– Servlets are efficient, portable, powerful, and widely
accepted in industry
Regardless of deployment server run a free server on– Regardless of deployment server, run a free server on
your desktop for development
– Using Eclipse greatly simplifies development and
deployment
• Getting started
S i h d T S l f l– Start with test-app and TestServlet from coreservlets.com
– Click on “Intermediate Servlets and JSP” tutorial in top-
left corner and you can get pre-made Eclipse projectsleft corner and you can get pre made Eclipse projects
34
© 2010 Marty Hall
Questions?
Customized Java EE Training: http://courses.coreservlets.com/
Servlets, JSP, JSF 2.0, Struts, Ajax, GWT 2.0, Spring, Hibernate, SOAP & RESTful Web Services, Java 6.
Developed and taught by well-known author and developer. At public venues or onsite at your location.35

More Related Content

What's hot

Peter lubbers-html5-offline-web-apps
Peter lubbers-html5-offline-web-appsPeter lubbers-html5-offline-web-apps
Peter lubbers-html5-offline-web-apps
Skills Matter
 
HTML5 Offline Web Application
HTML5 Offline Web ApplicationHTML5 Offline Web Application
HTML5 Offline Web Application
Allan Huang
 
WordPress Development Tools and Best Practices
WordPress Development Tools and Best PracticesWordPress Development Tools and Best Practices
WordPress Development Tools and Best Practices
Danilo Ercoli
 
Java EE 7 et ensuite pourquoi pas JavaScript sur le serveur!
Java EE 7 et ensuite pourquoi pas JavaScript sur le serveur! Java EE 7 et ensuite pourquoi pas JavaScript sur le serveur!
Java EE 7 et ensuite pourquoi pas JavaScript sur le serveur!
David Delabassee
 
Content Management Systems (CMS) & Wordpress theme development
Content Management Systems (CMS) & Wordpress theme developmentContent Management Systems (CMS) & Wordpress theme development
Content Management Systems (CMS) & Wordpress theme development
Dave Wallace
 
Front-end optimisation & jQuery Internals
Front-end optimisation & jQuery InternalsFront-end optimisation & jQuery Internals
Front-end optimisation & jQuery Internals
Artur Cistov
 

What's hot (20)

Peter lubbers-html5-offline-web-apps
Peter lubbers-html5-offline-web-appsPeter lubbers-html5-offline-web-apps
Peter lubbers-html5-offline-web-apps
 
EVOLVE'13 | Enhance | Permission Sensitive Caching | Paul McMahon & Jason Rap...
EVOLVE'13 | Enhance | Permission Sensitive Caching | Paul McMahon & Jason Rap...EVOLVE'13 | Enhance | Permission Sensitive Caching | Paul McMahon & Jason Rap...
EVOLVE'13 | Enhance | Permission Sensitive Caching | Paul McMahon & Jason Rap...
 
HTML5 Offline Web Application
HTML5 Offline Web ApplicationHTML5 Offline Web Application
HTML5 Offline Web Application
 
AEM (CQ) Dispatcher Security and CDN+Browser Caching
AEM (CQ) Dispatcher Security and CDN+Browser CachingAEM (CQ) Dispatcher Security and CDN+Browser Caching
AEM (CQ) Dispatcher Security and CDN+Browser Caching
 
Adobe AEM Maintenance - Customer Care Office Hours
Adobe AEM Maintenance - Customer Care Office HoursAdobe AEM Maintenance - Customer Care Office Hours
Adobe AEM Maintenance - Customer Care Office Hours
 
Flash And Dom
Flash And DomFlash And Dom
Flash And Dom
 
ClubAJAX Basics - Server Communication
ClubAJAX Basics - Server CommunicationClubAJAX Basics - Server Communication
ClubAJAX Basics - Server Communication
 
Wt unit 1 ppts web development process
Wt unit 1 ppts web development processWt unit 1 ppts web development process
Wt unit 1 ppts web development process
 
Wt unit 3 server side technology
Wt unit 3 server side technologyWt unit 3 server side technology
Wt unit 3 server side technology
 
Hibernate java and_oracle
Hibernate java and_oracleHibernate java and_oracle
Hibernate java and_oracle
 
Apache Camel: The Swiss Army Knife of Open Source Integration
Apache Camel: The Swiss Army Knife of Open Source IntegrationApache Camel: The Swiss Army Knife of Open Source Integration
Apache Camel: The Swiss Army Knife of Open Source Integration
 
Improve WordPress performance with caching and deferred execution of code
Improve WordPress performance with caching and deferred execution of codeImprove WordPress performance with caching and deferred execution of code
Improve WordPress performance with caching and deferred execution of code
 
Getting Started with Rails on GlassFish (Hands-on Lab) - Spark IT 2010
Getting Started with Rails on GlassFish (Hands-on Lab) - Spark IT 2010Getting Started with Rails on GlassFish (Hands-on Lab) - Spark IT 2010
Getting Started with Rails on GlassFish (Hands-on Lab) - Spark IT 2010
 
Mobile Hybrid Development with WordPress
Mobile Hybrid Development with WordPressMobile Hybrid Development with WordPress
Mobile Hybrid Development with WordPress
 
WordPress Development Tools and Best Practices
WordPress Development Tools and Best PracticesWordPress Development Tools and Best Practices
WordPress Development Tools and Best Practices
 
Anatomy of a Wordpress theme
Anatomy of a Wordpress themeAnatomy of a Wordpress theme
Anatomy of a Wordpress theme
 
Java EE 7 et ensuite pourquoi pas JavaScript sur le serveur!
Java EE 7 et ensuite pourquoi pas JavaScript sur le serveur! Java EE 7 et ensuite pourquoi pas JavaScript sur le serveur!
Java EE 7 et ensuite pourquoi pas JavaScript sur le serveur!
 
Java EE 8: What Servlet 4.0 and HTTP/2 mean to you
Java EE 8: What Servlet 4.0 and HTTP/2 mean to youJava EE 8: What Servlet 4.0 and HTTP/2 mean to you
Java EE 8: What Servlet 4.0 and HTTP/2 mean to you
 
Content Management Systems (CMS) & Wordpress theme development
Content Management Systems (CMS) & Wordpress theme developmentContent Management Systems (CMS) & Wordpress theme development
Content Management Systems (CMS) & Wordpress theme development
 
Front-end optimisation & jQuery Internals
Front-end optimisation & jQuery InternalsFront-end optimisation & jQuery Internals
Front-end optimisation & jQuery Internals
 

Viewers also liked (18)

10 jdbc
10 jdbc10 jdbc
10 jdbc
 
02 servlet-basics
02 servlet-basics02 servlet-basics
02 servlet-basics
 
The Change! Tool
The Change! ToolThe Change! Tool
The Change! Tool
 
Linea Viso-Corpo "Naturale" by Delta BkB
Linea Viso-Corpo "Naturale" by Delta BkBLinea Viso-Corpo "Naturale" by Delta BkB
Linea Viso-Corpo "Naturale" by Delta BkB
 
05 status-codes
05 status-codes05 status-codes
05 status-codes
 
08 session-tracking
08 session-tracking08 session-tracking
08 session-tracking
 
07 cookies
07 cookies07 cookies
07 cookies
 
ICT observatories for better governance
ICT observatories for better governanceICT observatories for better governance
ICT observatories for better governance
 
04 request-headers
04 request-headers04 request-headers
04 request-headers
 
15 expression-language
15 expression-language15 expression-language
15 expression-language
 
14 mvc
14 mvc14 mvc
14 mvc
 
03 form-data
03 form-data03 form-data
03 form-data
 
13 java beans
13 java beans13 java beans
13 java beans
 
10 jdbc
10 jdbc10 jdbc
10 jdbc
 
Digital Engagement for CSPs
Digital Engagement for CSPsDigital Engagement for CSPs
Digital Engagement for CSPs
 
11 page-directive
11 page-directive11 page-directive
11 page-directive
 
06 response-headers
06 response-headers06 response-headers
06 response-headers
 
08 session-tracking
08 session-tracking08 session-tracking
08 session-tracking
 

Similar to 01 overview-and-setup

Struts 2-overview2
Struts 2-overview2Struts 2-overview2
Struts 2-overview2
divzi1913
 
Ajax Tags Advanced
Ajax Tags AdvancedAjax Tags Advanced
Ajax Tags Advanced
AkramWaseem
 
Struts 2-overview2
Struts 2-overview2Struts 2-overview2
Struts 2-overview2
Long Nguyen
 
Raisa anthony web programming 1st week
Raisa anthony   web programming 1st weekRaisa anthony   web programming 1st week
Raisa anthony web programming 1st week
Raisa Anjani
 
eXo Platform SEA - Play Framework Introduction
eXo Platform SEA - Play Framework IntroductioneXo Platform SEA - Play Framework Introduction
eXo Platform SEA - Play Framework Introduction
vstorm83
 

Similar to 01 overview-and-setup (20)

Servlets
ServletsServlets
Servlets
 
Servlets Java Slides & Presentation
Servlets Java Slides & Presentation Servlets Java Slides & Presentation
Servlets Java Slides & Presentation
 
19servlets
19servlets19servlets
19servlets
 
01 web-apps
01 web-apps01 web-apps
01 web-apps
 
JavaScript-Core
JavaScript-CoreJavaScript-Core
JavaScript-Core
 
JavaScript-Core
JavaScript-CoreJavaScript-Core
JavaScript-Core
 
Ajax basics
Ajax basicsAjax basics
Ajax basics
 
AJppt.pptx
AJppt.pptxAJppt.pptx
AJppt.pptx
 
Struts 2-overview2
Struts 2-overview2Struts 2-overview2
Struts 2-overview2
 
Ajax Tags Advanced
Ajax Tags AdvancedAjax Tags Advanced
Ajax Tags Advanced
 
Struts 2-overview2
Struts 2-overview2Struts 2-overview2
Struts 2-overview2
 
Raisa anthony web programming 1st week
Raisa anthony   web programming 1st weekRaisa anthony   web programming 1st week
Raisa anthony web programming 1st week
 
Jsp Comparison
 Jsp Comparison Jsp Comparison
Jsp Comparison
 
Dave Orchard - Offline Web Apps with HTML5
Dave Orchard - Offline Web Apps with HTML5Dave Orchard - Offline Web Apps with HTML5
Dave Orchard - Offline Web Apps with HTML5
 
Setting up a free open source java e-commerce website
Setting up a free open source java e-commerce websiteSetting up a free open source java e-commerce website
Setting up a free open source java e-commerce website
 
Developing Java Web Applications
Developing Java Web ApplicationsDeveloping Java Web Applications
Developing Java Web Applications
 
Advance Java Topics (J2EE)
Advance Java Topics (J2EE)Advance Java Topics (J2EE)
Advance Java Topics (J2EE)
 
Week 05 Web, App and Javascript_Brandon, S.H. Wu
Week 05 Web, App and Javascript_Brandon, S.H. WuWeek 05 Web, App and Javascript_Brandon, S.H. Wu
Week 05 Web, App and Javascript_Brandon, S.H. Wu
 
eXo Platform SEA - Play Framework Introduction
eXo Platform SEA - Play Framework IntroductioneXo Platform SEA - Play Framework Introduction
eXo Platform SEA - Play Framework Introduction
 
JavaEE6 my way
JavaEE6 my wayJavaEE6 my way
JavaEE6 my way
 

01 overview-and-setup

  • 1. © 2010 Marty Hall An Overview of ServletAn Overview of Servlet & JSP Technology Originals of Slides and Source Code for Examples: http://courses.coreservlets.com/Course-Materials/csajsp2.html Customized Java EE Training: http://courses.coreservlets.com/ Servlets, JSP, JSF 2.0, Struts, Ajax, GWT 2.0, Spring, Hibernate, SOAP & RESTful Web Services, Java 6. Developed and taught by well-known author and developer. At public venues or onsite at your location.3 © 2010 Marty Hall For live Java EE training, please see training courses at http://courses.coreservlets.com/.at http://courses.coreservlets.com/. Servlets, JSP, Struts, JSF 1.x, JSF 2.0, Ajax (with jQuery, Dojo, Prototype, Ext-JS, Google Closure, etc.), GWT 2.0 (with GXT), Java 5, Java 6, SOAP-based and RESTful Web Services, Spring,g Hibernate/JPA, and customized combinations of topics. Taught by the author of Core Servlets and JSP, More Servlets and JSP and this tutorial Available at public Customized Java EE Training: http://courses.coreservlets.com/ Servlets, JSP, JSF 2.0, Struts, Ajax, GWT 2.0, Spring, Hibernate, SOAP & RESTful Web Services, Java 6. Developed and taught by well-known author and developer. At public venues or onsite at your location. Servlets and JSP, and this tutorial. Available at public venues, or customized versions can be held on-site at your organization. Contact hall@coreservlets.com for details.
  • 2. Agenda • What servlets and JSP are all about – Understanding the role of servlets – Building Web pages dynamically – Evaluating servlets vs. other technologies – Understanding the role of JSP T i T i h E li• Testing Tomcat with Eclipse – Installing Tomcat I lli d i E li– Installing and starting Eclipse – Telling Eclipse about Tomcat Deploying and running Web apps from Eclipse– Deploying and running Web apps from Eclipse – Making new Web apps in Eclipse 5 © 2010 Marty Hall What Servlets and JSPWhat Servlets and JSP are All About Customized Java EE Training: http://courses.coreservlets.com/ Servlets, JSP, JSF 2.0, Struts, Ajax, GWT 2.0, Spring, Hibernate, SOAP & RESTful Web Services, Java 6. Developed and taught by well-known author and developer. At public venues or onsite at your location.6
  • 3. A Servlet’s Job • Read explicit data sent by client (form data) • Read implicit data sent by client (request headers) G t th lt• Generate the results • Send the explicit data back to client (HTML) S d th i li it d t t li t• Send the implicit data to client (status codes and response headers) 7 Why Build Web Pages Dynamically?Dynamically? • The Web page is based on data submitted b hby the user – E.g., results page from search engines and order- confirmation pages at on-line storesconfirmation pages at on line stores • The Web page is derived from data that changes frequentlyg q y – E.g., a weather report or news headlines page • The Web page uses information from d t b th iddatabases or other server-side sources – E.g., an e-commerce site could use a servlet to build a Web page that lists the current price and availability ofWeb page that lists the current price and availability of each item that is for sale. 8
  • 4. The Advantages of Servlets Over “Traditional” CGIOver Traditional CGI • Efficient Th d i t d f OS l t– Threads instead of OS processes, one servlet copy • Convenient – Lots of high-level utilities • Powerful – Sharing data, pooling, persistence • PortablePortable – Run on virtually all operating systems and servers • Inexpensive There are plenty of free and low cost servers– There are plenty of free and low-cost servers • Secure – No shell escapes, b ff flno buffer overflows • Mainstream – See next page9 From Randall Munroe and xkcd.com Mainstream • Popular: The single most common use of Java technology– The single most common use of Java technology – The leading technology for medium/large Web applications • Google reports over 650 million Web pages using JSP • Supported by:Supported by: – Apache, Oracle, IBM, Sybase, BEA, Jetty, Caucho, Sun, New Atlanta, ATG, Fujitsu, Lutris, Silverstream, the World Wide Web Consortium (W3C), and many others( ), y – Plugins for IIS and Zeus • Runs on: – Windows, Unix/Linux, MacOS,, , , VMS, and IBM mainframe OSs • Used for: – Airline companies, hotels,p , , e-commerce sites, search engines, banks, financial sites, etc., etc., etc. 10 Server-side Java is driving the Web
  • 5. Ten Most Popular Web Sites (Alexa com Summer 2010)(Alexa.com, Summer 2010) 1. Google 6. Baidu – Java (Web), C++ (indexing) 2. Facebook – Unknown 7. Wikipedia PHP2. Facebook – PHP 3. YouTube – PHP 8. Blogger – Java – Flash, Python, Java 4. Yahoo PHP and Java 9. MSN – .NET 10 T itt– PHP and Java 5. Microsoft Live.com – .NET 10.Twitter – Ruby on Rails, Scala, Java 11 Keywords in Job Postings 12
  • 6. Extending the Power of Servlets: JavaServer Pages (JSP)JavaServer Pages (JSP) • Idea: Use regular HTML for most of page– Use regular HTML for most of page – Mark dynamic content with special tags – Details in second half of course <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD><TITLE>Welcome to Our Store</TITLE></HEAD> <BODY><BODY> <H1>Welcome to Our Store</H1> <SMALL>Welcome, <!-- User name is "New User" for first-time visitors --> <%= coreservlets.Utils.getUserNameFromCookie(request) %> To access your account settings, click <A HREF="Account-Settings.html">here.</A></SMALL> <P> Regular HTML for rest of on-line store’s Web page </BODY></HTML> 13 Accessing the Online DocumentationDocumentation • Servlets and JSP htt //d l t / l t 3 0 i/– http://docs.coreservlets.com/servlet-3.0-api/ • Servlets 3.0 and JSP 2.2 (Tomcat 7) – http://java.sun.com/products/servlet/2.5/docs/servlet-2_5-mr2/ S l t 2 5 (T t 6)• Servlets 2.5 (Tomcat 6) – http://java.sun.com/products/jsp/2.1/docs/jsp-2_1-pfd2/ • JSP 2.1 (Tomcat 6) J 6• Java 6 – http://java.sun.com/javase/6/docs/api/ • Class uses Java 6 and Tomcat 7C ass uses Ja a 6 a d o cat • Advice – If you have a fast and reliable internet connection, bookmark these addressesbookmark these addresses – If not, download a copy of the APIs onto your local machine and use it14
  • 7. Setting Up Tomcat on Your PC • Tomcat 7 with Eclipse – http://www.coreservlets.com/Apache-Tomcat-Tutorial/ tomcat-7-with-eclipse.html • Or just follow link at top left of www coreservlets comOr, just follow link at top left of www.coreservlets.com • More details in next section of this tutorial • Tomcat 6 with Eclipse – http://www.coreservlets.com/Apache-Tomcat-Tutorial/ • For manual execution h // l /A h T T i l/– http://www.coreservlets.com/Apache-Tomcat-Tutorial/ • More details in last section. • Eclipse or another IDE strongly recommended • Bottom line – Unzip Tomcat, then point Eclipse at the install folder15 © 2010 Marty Hall I t lli T tInstalling Tomcat For even more detailed step-by-step instructions, see tutorials on using Eclipse with Tomcat 6 or Tomcat 7 at http://www.coreservlets.com/Apache-Tomcat-Tutorial/ Customized Java EE Training: http://courses.coreservlets.com/ Servlets, JSP, JSF 2.0, Struts, Ajax, GWT 2.0, Spring, Hibernate, SOAP & RESTful Web Services, Java 6. Developed and taught by well-known author and developer. At public venues or onsite at your location.16
  • 8. Download and Unzip • Start at http://tomcat.apache.org – Choose download link on left, then ZIP version • Tomcat 7 (recommended) • Tomcat 6 (if you need compatibility with older servers)Tomcat 6 (if you need compatibility with older servers) • Or, go to http://www.coreservlets.com/ – Choose Tomcat tutorial from top left – This is preconfigured version • Set for development, not deployment mode Port changed to 80 servlet reloading enabled directory listings– Port changed to 80, servlet reloading enabled, directory listings turned on, etc. • Otherwise unchanged • Either way just unzip the file• Either way, just unzip the file – E.g., resulting in C:apache-tomcat-7.0.0 17 © 2010 Marty Hall I t lli E liInstalling Eclipse For even more detailed step-by-step instructions, see tutorials on using Eclipse with Tomcat 6 or Tomcat 7 at http://www.coreservlets.com/Apache-Tomcat-Tutorial/ Customized Java EE Training: http://courses.coreservlets.com/ Servlets, JSP, JSF 2.0, Struts, Ajax, GWT 2.0, Spring, Hibernate, SOAP & RESTful Web Services, Java 6. Developed and taught by well-known author and developer. At public venues or onsite at your location.18
  • 9. Installing Eclipse • Overview – Eclipse is a a free open source IDE– Eclipse is a a free open source IDE for Java. Support for Java, HTML, CSS, JavaScript, C++, PHP, and more. http://eclipse org/downloads/• http://eclipse.org/downloads/ • Choose “Eclipse IDE for Java EE Developers” – Need version 3.6 (Helios) for Tomcat 7 F t• Features – Checks your syntax as you type – Automatically compiles every time you save filetime you save file – Many tools: refactoring, debugging, server integration, templates for common tasks, etc.p • Low learning curve: beginners can use Eclipse without knowing these tools Reminder: step-by-step guide at http://www.coreservlets.com/ (click “Apache Tomcat 7” in top left). Running Eclipse • Unzip the downloaded file – Call the folder you unzip into “installDir” • Double click eclipse.exe F i llDi /bi– From installDir/bin • Click on “Workbench” iconWorkbench icon – Next time you bring up Eclipse, it will come up in workbench automatically • Shortcut M d l t E li li k th i d kt– Many developers put Eclipse link on their desktop • R-click eclipse.exe, Copy, then go to desktop, R-click, and Paste Shortcut (not just Paste!)
  • 10. Configuring Eclipse • Tell Eclipse about Java version Wi d  P f  J – Window  Preferences  Java  Installed JREs  Press “Add”, choose “Standard VM”, navigate to JDK folder (not “bin” subdirectory)(not bin subdirectory) • E.g., C:Program FilesJavajdk1.6.0_21 • Tell Eclipse about Tomcat Cli k S b b– Click on Servers tab at bottom. R-click in window. – New, Server, Apache, Tomcat v7.0, N t i t t f ld Fi i hNext, navigate to folder, Finish. • Suppress serializable warnings – Window  Preferences  Java  Compiler  Errors/Warnings • Change “Serializable class without ...” to “Ignore”21 Tomcat v7.0 is choice only in Eclipse 3.6 (Helios). If you prefer Tomcat 6, choose Tomcat v6.0 above instead. If you lose the “Servers” tab at the bottom of Eclipse, use Window, Show View, and hunt for “Servers”. © 2010 Marty Hall Deploying AppsDeploying Apps from Eclipse Customized Java EE Training: http://courses.coreservlets.com/ Servlets, JSP, JSF 2.0, Struts, Ajax, GWT 2.0, Spring, Hibernate, SOAP & RESTful Web Services, Java 6. Developed and taught by well-known author and developer. At public venues or onsite at your location.22
  • 11. Download and Import Sample ProjectProject • Get test-app.zip from coreservlets.com – Start at servlet and JSP tutorials • http://courses.coreservlets.com/ Course-Materials/csajsp2.htmlj p – Go to first section (Overview and Setup) – Or, start at Apache Tomcat tutorial • http://www coreservlets com/Apache Tomcat Tutorial/• http://www.coreservlets.com/Apache-Tomcat-Tutorial/ – Choose Tomcat 7 (recommended) or Tomcat 6 version • Then, download test-app.zip – Then, import into Eclipse. • File, Import, General, Existing Projects, Select archive file.Projects, Select archive file. Then click Browse and navigate to test-app.zip. 23 Deploying App in Eclipse • Deploy project – Select “Servers” tab at bottom – R-click on Tomcat – Choose “Add and Remove”Choose Add and Remove – Choose project – Press “Add” – Click “Finish” • Start Server R click Tomcat at bottom– R-click Tomcat at bottom – Start (use “Restart” if Tomcat already running) • Test URL – http://localhost/test-app/ in any Web browser 24
  • 12. Testing Deployed App in Eclipse • Start a browser – Eclipse also has builtin browser, but I prefer to use Firefox or Internet Explorer • Test base URL – http://localhost/test-app/ T t W b t t• Test Web content – http://localhost/test-app/hello.html – http://localhost/test-app/hello.jsphttp://localhost/test app/hello.jsp • Test servlets – http://localhost/test-app/hello – http://localhost/test-app/test1 – http://localhost/test-app/test2 25 © 2010 Marty Hall Making New AppsMaking New Apps from Eclipse Customized Java EE Training: http://courses.coreservlets.com/ Servlets, JSP, JSF 2.0, Struts, Ajax, GWT 2.0, Spring, Hibernate, SOAP & RESTful Web Services, Java 6. Developed and taught by well-known author and developer. At public venues or onsite at your location.26
  • 13. Making Web Apps in Eclipse • Make empty project – File  New  Project  Web  Dynamic Web Project – For “Target runtime” choose– For Target runtime , choose “Apache Tomcat v7.0” – Give it a name (e.g., “test”) – Accept all other defaults • Shortcut If h d D i– If you have made Dynamic Web Project recently in workspace, you can just dop y j File  New  Dynamic Web Project 27 Adding Code to Eclipse Projects • Locations Java Resources: src– Java Resources: src • R-click and New  Package • Never use default package – src/testPackagesrc/testPackage • Java code in testPackage package – WebContent • Web files (HTML, JavaScript, CSS JSP i t )CSS, JSP, images, etc.) – WebContent/some-subdirectory • Web content in subdirectory • R-click on WebContent New  Folder• R-click on WebContent, New  Folder – WebContent/WEB-INF • web.xml – Optional with servlets 3.0. Required in 2.5 & earlier.p q – Will be discussed later • Note – Can cut/paste or drag/drop files into appropriate locations28
  • 14. Testing New App • Follow same procedure as “deploying app” f i ifrom previous section – Deploy project • Select “Servers” tab at bottomSelect Servers tab at bottom • R-click on Tomcat • Choose “Add and Remove” • Choose project• Choose project • Press “Add” • Click “Finish” Start Server– Start Server • R-click Tomcat at bottom • Restart (use “Start” if Tomcat not already running) – Test URL • http://localhost/appName/ in any Web browser 29 Summary • General – Servlets are efficient, portable, powerful, and widely accepted in industry Regardless of deployment server run a free server on– Regardless of deployment server, run a free server on your desktop for development – Using Eclipse greatly simplifies development and deployment • Getting started S i h d T S l f l– Start with test-app and TestServlet from coreservlets.com – Click on “Intermediate Servlets and JSP” tutorial in top- left corner and you can get pre-made Eclipse projectsleft corner and you can get pre made Eclipse projects 34
  • 15. © 2010 Marty Hall Questions? Customized Java EE Training: http://courses.coreservlets.com/ Servlets, JSP, JSF 2.0, Struts, Ajax, GWT 2.0, Spring, Hibernate, SOAP & RESTful Web Services, Java 6. Developed and taught by well-known author and developer. At public venues or onsite at your location.35