4020 Week 4
4020 Week 4
4020 Week 4
HTML
Web Designer External Applications
Authoring Non-HTTP objects
& Publisher
Tools/Editors
• JAVA Servlet
• CGI (Perl)
• ASP & ASP.NET
• Java Server Pages
• Java Applet
• JavaScript
Web Programmer
Web
Browser
Internet
Global Reach
Broad Range Web
Server
Client
End User Web Master
Why Build Pages Dynamically?
◆ The Web page is based on data submitted by the user
➢ E.g., results page from search engines and order-
confirmation pages at on-line stores
◆ The Web page is derived from data that changes
frequently
➢ E.g., a weather report or news headlines page
◆ The Web page uses information from databases 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 of
each item that is for sale
Alternatives for Generating
Dynamic Pages
In addition to CGI, can we dynamically generate
page in other ways?
◆ Java Servlets
Application+data tier
expresses data into a
HTML/XML document for
the user
Application
HTML
on a Web
interface
server
Database access
(Optional)
Chrome
http://csp.ipm.edu.mo
Suppose this web page has an
embedded flash movie and a
background music. We want a
responsive browser user interface
while the flash movie and
background music are playing.
Flash movie playing …
We need three threads within the
browser process.
Background music playing
…
Two Examples for Multithreading
http://jakarta.apache.org/tomcat/
Tomcat
http://sit.yorku.ca:8080/jhuang
The Directory Structure of a
Web Application
◆ Web applications are located in the webapps
directory
◆ Each web application has its own subdirectory.
◆ The web application subdirectory is built in a
standard fashion
◆ Note: After creating the subdirectory you must
restart Tomcat, for tomcat to recognize it!
The Directory Structure of a
Web Application
◆ ~/jhuang: The root directory of the “jhuang” web
application. Store here HTML, Servlet and JSP files
• used to
- perform all CGI related tasks
- dynamically create HTML pages
- access databases through JDBC
- email service using JavaMail
- access distributed objects using RMI, CORBA or EJB
- many others
<FORM ACTION="http://localhost:8080/examples/servlet/servFormIntro”
METHOD="POST">
• Servlet classes and interfaces are not part of core Java standard.
They are defined in two packages:
- javax.servlet (generic servlet package)
- javax.servlet.http
* extends generic servlet classes/interfaces for Web server to
capture HTTP communication and servlet management
HTTPServlet class
• we need to implement doGet or doPost
• default implementation returns error
doGet() and doPost() methods
• Both the methods in HTTPServlet class define only default behaviour. The
default behaviour is to send an error message as a response.
• The class implementing servlets must extend HTTPServlet class to set non-
default response to client requests
• both the methods can generate IOException and ServletException which are
thrown to the servlet engine.
First Servlet Example
import java.io.*; Import these packages
import javax.servlet.*;
import javax.servlet.http.*;
service() method in
Response Response
Servlet
Get POST
Request Get Request POST
Response Response
◆ Output HTML
➢ Be sure to include the DOCTYPE
A Servlet that Generates HTML
public class HelloWWW extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String docType =
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
"Transitional//EN\">\n";
out.println(docType +
"<HTML>\n" +
"<HEAD><TITLE>Hello WWW</TITLE></HEAD>\n"+
"<BODY>\n" +
"<H1>Hello WWW</H1>\n" +
"</BODY></HTML>");
}
}
Handling the Client Request: Form Data
◆ Example URL at online travel agent
➢ http://host/path?user=Marty+Hall&origin=iad&dest=nrt
➢ Names (user) come from HTML author;
values (Marty+Hall) usually come from end user