Jsp & Ajax

67
HTTP, JSP, and AJAX Ang Chen CUI, University of Geneva PINFO 05-06 May 8, 2006

description

old slides about jsp, ajax, and web 2.0

Transcript of Jsp & Ajax

Page 1: Jsp & Ajax

HTTP, JSP, and AJAX

Ang Chen

CUI, University of Geneva

PINFO 05-06 May 8, 2006

Page 2: Jsp & Ajax

Outline

IntroductionOverviewWEB 1.0 vs WEB 2.0

HTTP, CGI, Web Application ModelHTTPCommon Gateway Interface (CGI)Web Application ModelSummary

Servlet & JSPServletJava Server Pages

AJAXUsing AJAX

Summary

Resources

Page 3: Jsp & Ajax

Outline

IntroductionOverviewWEB 1.0 vs WEB 2.0

HTTP, CGI, Web Application ModelHTTPCommon Gateway Interface (CGI)Web Application ModelSummary

Servlet & JSPServletJava Server Pages

AJAXUsing AJAX

Summary

Resources

Page 4: Jsp & Ajax

Overview

This presentation provides a brief introduction of:

I HTTP, CGI, Web Application Model

I Servlets & JSP

I AJAX

What it does not include (many!) but can be found in references:

I Complete technical details, APIs: e.g Taglib, JavaScript,HTML, DOM

I How to configure Tomcat and deploy applications

I How to manage sessions with cookies and URL rewriting.

I How to use JavaBeans in JSP

I Development Frameworks with JSP and AJAX, e.g. Struts,various AJAX frameworks.

Page 5: Jsp & Ajax

WEB 1.0 vs WEB 2.0

Technically, they are the same. ”Old wine in a new bottle.”

But, the Use Cases are different.

WEB 1.0:

I User browses the content on the server(the Web), theserver(the Web) return the desired content.

I The Internet Content Provider(ICP) provides the contents toclients.

WEB 2.0:

I User reads and write content from/to the Web.

I Most contents are provided by Internet users. The Web asPlatform.

Page 6: Jsp & Ajax

WEB 2.0

Example of WEB 2.0:

I WebBlog, Social Networks, Contents Sharing, Wiki, RSS(Really Simple Syndication), PodCast

I Google Maps, Gmail, AdSense, Writely, Flickr, del.icio.us,eBay, Amazon, BitTorrent

Keywords of WEB 2.0:

I Sharing, Tagging, Syndication, Blogging,

I Services, Simplicity, Re-usability

Note that sometimes the term WEB 2.0 is more marketing than itsreal sense.

Page 7: Jsp & Ajax

Outline

IntroductionOverviewWEB 1.0 vs WEB 2.0

HTTP, CGI, Web Application ModelHTTPCommon Gateway Interface (CGI)Web Application ModelSummary

Servlet & JSPServletJava Server Pages

AJAXUsing AJAX

Summary

Resources

Page 8: Jsp & Ajax

HTTP: Hypertext Transfer Protocol I

I A generic protocol for data transfer across the Internet, RFC2616.

I HTTP Extensions and HTTP 1.1 are stable specifications andW3C has closed the HTTP Activity.

I Client/Server architecture: the client sends a request, theserver responses. The protocol consists of several parts:communication, access authentication, message format,encoding, caching, etc.

Page 9: Jsp & Ajax

HTTP: Hypertext Transfer Protocol I

I A generic protocol for data transfer across the Internet, RFC2616.

I HTTP Extensions and HTTP 1.1 are stable specifications andW3C has closed the HTTP Activity.

I Client/Server architecture: the client sends a request, theserver responses. The protocol consists of several parts:communication, access authentication, message format,encoding, caching, etc.

Page 10: Jsp & Ajax

HTTP: Hypertext Transfer Protocol I

I A generic protocol for data transfer across the Internet, RFC2616.

I HTTP Extensions and HTTP 1.1 are stable specifications andW3C has closed the HTTP Activity.

I Client/Server architecture: the client sends a request, theserver responses. The protocol consists of several parts:communication, access authentication, message format,encoding, caching, etc.

Page 11: Jsp & Ajax

HTTP: Hypertext Transfer Protocol II

To write Web applications, we are interested in the most usedcommunication primitives between the client(browser) and theWeb server:

I GET: retrieve the information identified by the Request-URI.

I POST: providing a block of data to server, e.g. posting amessage, filling a form, etc.

I PUT: request to store the enclosed entity under theRequest-URI, e.g. uploading a file.

I DELETE: request to delete the resource identified by theRequest-URI, e.g. deleting an uploaded file.

Page 12: Jsp & Ajax

HTTP: Hypertext Transfer Protocol II

To write Web applications, we are interested in the most usedcommunication primitives between the client(browser) and theWeb server:

I GET: retrieve the information identified by the Request-URI.

I POST: providing a block of data to server, e.g. posting amessage, filling a form, etc.

I PUT: request to store the enclosed entity under theRequest-URI, e.g. uploading a file.

I DELETE: request to delete the resource identified by theRequest-URI, e.g. deleting an uploaded file.

Page 13: Jsp & Ajax

HTTP: Hypertext Transfer Protocol II

To write Web applications, we are interested in the most usedcommunication primitives between the client(browser) and theWeb server:

I GET: retrieve the information identified by the Request-URI.

I POST: providing a block of data to server, e.g. posting amessage, filling a form, etc.

I PUT: request to store the enclosed entity under theRequest-URI, e.g. uploading a file.

I DELETE: request to delete the resource identified by theRequest-URI, e.g. deleting an uploaded file.

Page 14: Jsp & Ajax

HTTP: Hypertext Transfer Protocol II

To write Web applications, we are interested in the most usedcommunication primitives between the client(browser) and theWeb server:

I GET: retrieve the information identified by the Request-URI.

I POST: providing a block of data to server, e.g. posting amessage, filling a form, etc.

I PUT: request to store the enclosed entity under theRequest-URI, e.g. uploading a file.

I DELETE: request to delete the resource identified by theRequest-URI, e.g. deleting an uploaded file.

Page 15: Jsp & Ajax

Common Gateway Interface (CGI) I

CGI is a standard for interfacing external applications withinformation servers, such as HTTP or Web servers.

Very important evolution: Static pages -> dynamic pages

CGI programs can be written by using any programming languageswith input/output, e.g. C, C++, Perl, Shell, Fortran, TCL ...

Page 16: Jsp & Ajax

Common Gateway Interface (CGI) II

But CGI has the following problems:

I Performance: each CGI program instance is a process ofoperation system, i.e. for each request from client, the OScreate a process.

I Security: a Web application, as it is a normal application, canblock or crash the server.

I Manageability: difficult to manage the set of CGI programsand connect them together.

Page 17: Jsp & Ajax

Common Gateway Interface (CGI) II

But CGI has the following problems:

I Performance: each CGI program instance is a process ofoperation system, i.e. for each request from client, the OScreate a process.

I Security: a Web application, as it is a normal application, canblock or crash the server.

I Manageability: difficult to manage the set of CGI programsand connect them together.

Page 18: Jsp & Ajax

Common Gateway Interface (CGI) II

But CGI has the following problems:

I Performance: each CGI program instance is a process ofoperation system, i.e. for each request from client, the OScreate a process.

I Security: a Web application, as it is a normal application, canblock or crash the server.

I Manageability: difficult to manage the set of CGI programsand connect them together.

Page 19: Jsp & Ajax

Web Application Model

Web Application Models are proposed for the separation ofresponsibilities between the Web server and Web application, andhow to collaborate to get better performance.

I Easy to be managed, Web applications are homogeneous andsecurer.

I More efficient, e.g using thread pool to manage theapplication instances.

I Use the Inverse of Control principle.

I Examples: Apache modules (for php, perl etc.), Webapplication container (e.g. Tomcat).

I With the same approach: Application Server (AS), e.g.Enterprise JavaBean container.

Page 20: Jsp & Ajax

Summary

I HTTP for : client / server communication

I CGI for : Web server / Application Integration

I JSP / Servlet : a model used for creating Java Webapplications. Using the container model.

I We proceed with JSP/Servlet.

Page 21: Jsp & Ajax

Summary

I HTTP for : client / server communication

I CGI for : Web server / Application Integration

I JSP / Servlet : a model used for creating Java Webapplications. Using the container model.

I We proceed with JSP/Servlet.

Page 22: Jsp & Ajax

Summary

I HTTP for : client / server communication

I CGI for : Web server / Application Integration

I JSP / Servlet : a model used for creating Java Webapplications. Using the container model.

I We proceed with JSP/Servlet.

Page 23: Jsp & Ajax

Summary

I HTTP for : client / server communication

I CGI for : Web server / Application Integration

I JSP / Servlet : a model used for creating Java Webapplications. Using the container model.

I We proceed with JSP/Servlet.

Page 24: Jsp & Ajax

Outline

IntroductionOverviewWEB 1.0 vs WEB 2.0

HTTP, CGI, Web Application ModelHTTPCommon Gateway Interface (CGI)Web Application ModelSummary

Servlet & JSPServletJava Server Pages

AJAXUsing AJAX

Summary

Resources

Page 25: Jsp & Ajax

Java Web Application Model

Web Server

DatabaseServer Side Apps

Web Application Container

Web Application/app1

servlet

servlet

servlet

Web Application/app2

servlet

servlet

servlet

Server side Service Invocation

request handling

client

request/response

Figure: Java Web Application Model

Page 26: Jsp & Ajax

Java Web Application Model

There are several levels of scope, where each one has itsparameters:

I Container: system-wide configuration

I Application: application configuration and parameters

I Servlet: servlet information and parameters

I Page: information related with a JSP

I Session: session information can cross pages and servlets,session can be used to store objects

Each level is modeled by a Java class or interface. (Implicit objectsin JSP)

Page 27: Jsp & Ajax

Interface javax.servlet.Servlet

This interface defines methods to initialize a servlet, to servicerequests, and to remove a servlet from the server:

I The servlet is constructed, then initialized with the initmethod.

I Any calls from clients to the service method are handled.

I The servlet is taken out of service, then destroyed with thedestroy method, then garbage collected and finalized.

In addition to the life-cycle methods, this interface provides thegetServletConfig method, which the servlet can use to get anystartup information, and the getServletInfo method, which allowsthe servlet to return basic information about itself, such as author,version, and copyright.

Page 28: Jsp & Ajax

Abstract Class javax.servlet.GenericServlet

The abstract class GenericServlet defines a generic,protocol-independent servlet.

I It implements the Servlet and ServletConfig interface.

I It provides simple versions of the lifecycle methods init anddestroy and of the methods in the ServletConfig interface.GenericServlet also implements the log method, declared inthe ServletContext interface.

To write a generic servlet, you need only override the abstractservice method. HttpServlet is a subclass of GenericServlet usingHTTP protocol.

Page 29: Jsp & Ajax

Abstract class javax.servlet.http.HttpServlet

The abstract class HttpServlet is designed to be subclassed tocreate an HTTP servlet suitable for a Web site. A subclass ofHttpServlet must override at least one method, usually one ofthese:

I doGet, if the servlet supports HTTP GET requests

I doPost, for HTTP POST requests

I doPut, for HTTP PUT requests

I doDelete, for HTTP DELETE requests

I init and destroy, to manage resources that are held for the lifeof the servlet

I getServletInfo, which the servlet uses to provide informationabout itself

Page 30: Jsp & Ajax

Abstract class javax.servlet.http.HttpServlet

The abstract class HttpServlet is designed to be subclassed tocreate an HTTP servlet suitable for a Web site. A subclass ofHttpServlet must override at least one method, usually one ofthese:

I doGet, if the servlet supports HTTP GET requests

I doPost, for HTTP POST requests

I doPut, for HTTP PUT requests

I doDelete, for HTTP DELETE requests

I init and destroy, to manage resources that are held for the lifeof the servlet

I getServletInfo, which the servlet uses to provide informationabout itself

Page 31: Jsp & Ajax

Abstract class javax.servlet.http.HttpServlet

The abstract class HttpServlet is designed to be subclassed tocreate an HTTP servlet suitable for a Web site. A subclass ofHttpServlet must override at least one method, usually one ofthese:

I doGet, if the servlet supports HTTP GET requests

I doPost, for HTTP POST requests

I doPut, for HTTP PUT requests

I doDelete, for HTTP DELETE requests

I init and destroy, to manage resources that are held for the lifeof the servlet

I getServletInfo, which the servlet uses to provide informationabout itself

Page 32: Jsp & Ajax

Abstract class javax.servlet.http.HttpServlet

The abstract class HttpServlet is designed to be subclassed tocreate an HTTP servlet suitable for a Web site. A subclass ofHttpServlet must override at least one method, usually one ofthese:

I doGet, if the servlet supports HTTP GET requests

I doPost, for HTTP POST requests

I doPut, for HTTP PUT requests

I doDelete, for HTTP DELETE requests

I init and destroy, to manage resources that are held for the lifeof the servlet

I getServletInfo, which the servlet uses to provide informationabout itself

Page 33: Jsp & Ajax

Abstract class javax.servlet.http.HttpServlet

The abstract class HttpServlet is designed to be subclassed tocreate an HTTP servlet suitable for a Web site. A subclass ofHttpServlet must override at least one method, usually one ofthese:

I doGet, if the servlet supports HTTP GET requests

I doPost, for HTTP POST requests

I doPut, for HTTP PUT requests

I doDelete, for HTTP DELETE requests

I init and destroy, to manage resources that are held for the lifeof the servlet

I getServletInfo, which the servlet uses to provide informationabout itself

Page 34: Jsp & Ajax

Abstract class javax.servlet.http.HttpServlet

The abstract class HttpServlet is designed to be subclassed tocreate an HTTP servlet suitable for a Web site. A subclass ofHttpServlet must override at least one method, usually one ofthese:

I doGet, if the servlet supports HTTP GET requests

I doPost, for HTTP POST requests

I doPut, for HTTP PUT requests

I doDelete, for HTTP DELETE requests

I init and destroy, to manage resources that are held for the lifeof the servlet

I getServletInfo, which the servlet uses to provide informationabout itself

Page 35: Jsp & Ajax

Writing a HttpServlet

A Servlet which returns ”Hello World” to the browser

import java.io.*;import javax.servlet.*;import javax.servlet.http.*;

public class HelloWorld extends HttpServlet {public void doGet(HttpServletRequest request,

HttpServletResponse response)throws ServletException, IOException {

PrintWriter out = response.getWriter();out.println("Hello World");

}public String getServletInfo() {return "Hello world example for PINFO";}

}

Page 36: Jsp & Ajax

Writing a HttpServlet II

Return HTML content

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

public class HelloWorld extends HttpServlet {

public void doGet(HttpServletRequest request,

HttpServletResponse response)

throws ServletException, IOException {

response.setContentType("text/html"); // set HTTP response content type

PrintWriter out = response.getWriter();

out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +

"Transitional//EN\">\n" +

"<HTML>\n" +

"<HEAD><TITLE>Hello WWW</TITLE></HEAD>\n" +

"<BODY>\n" +

"<H1>Hello WWW</H1>\n" +

"</BODY></HTML>");

}

public String getServletInfo() {

return "Hello world example for PINFO";

}

}

Page 37: Jsp & Ajax

Get the value of the request parameter

HttpServletRequest classRequest URL: http://localhost:8080/list.jsp?category=pinfo05

public void doGet(HttpServletRequest request,HttpServletResponse response) throws ... {

...String cat = request.getParameter("category");...}

cat will have value ”pinfo05”. It works for both GET and POST requests.

Two important classes: HttpServletRequest contains informationabout the request; HttpServletResponse is used to generate HTTPresponse.

Page 38: Jsp & Ajax

Java Server Pages

Java Server Pages

A way of creating dynamic pages with Java and HTML.Principle: Java for application logic, HTML for presentation.

JSP uses HTML for page rendering, and provides several ways touse Java components:

I Scriptlet: Java codes enclosed by <% and %>

I Taglib: customized JSP tags

I Using JavaBeans

JSP are translated into Java servlets before they are compiled bythe Web application container. HttpServletRequest andHttpServletResponse are implicit objects in JSP.

Page 39: Jsp & Ajax

Implicit Objects in Java Server Page

The following variables can be directly used in JSP:

I page: jsp.HttpJspPage : Page’s servlet instance

I config: ServletConfig : Servlet configuration information

I pageContext: jsp.pageContext : Provides access to all thenamespaces associated with a JSP page and access to severalpage attributes

I request: http.HttpServletRequest : Data included with theHTTP Request

I response: http.HttpServletResponse : HTTP Response data,e.g. cookies

I out: jsp.JspWriter : Output stream for page context

I session: http.HttpSession : User specific session data

I application: ServletContext : Data shared by all applicationpages

Page 40: Jsp & Ajax

Implicit Objects in Java Server Page

The following variables can be directly used in JSP:

I page: jsp.HttpJspPage : Page’s servlet instance

I config: ServletConfig : Servlet configuration information

I pageContext: jsp.pageContext : Provides access to all thenamespaces associated with a JSP page and access to severalpage attributes

I request: http.HttpServletRequest : Data included with theHTTP Request

I response: http.HttpServletResponse : HTTP Response data,e.g. cookies

I out: jsp.JspWriter : Output stream for page context

I session: http.HttpSession : User specific session data

I application: ServletContext : Data shared by all applicationpages

Page 41: Jsp & Ajax

Implicit Objects in Java Server Page

The following variables can be directly used in JSP:

I page: jsp.HttpJspPage : Page’s servlet instance

I config: ServletConfig : Servlet configuration information

I pageContext: jsp.pageContext : Provides access to all thenamespaces associated with a JSP page and access to severalpage attributes

I request: http.HttpServletRequest : Data included with theHTTP Request

I response: http.HttpServletResponse : HTTP Response data,e.g. cookies

I out: jsp.JspWriter : Output stream for page context

I session: http.HttpSession : User specific session data

I application: ServletContext : Data shared by all applicationpages

Page 42: Jsp & Ajax

Implicit Objects in Java Server Page

The following variables can be directly used in JSP:

I page: jsp.HttpJspPage : Page’s servlet instance

I config: ServletConfig : Servlet configuration information

I pageContext: jsp.pageContext : Provides access to all thenamespaces associated with a JSP page and access to severalpage attributes

I request: http.HttpServletRequest : Data included with theHTTP Request

I response: http.HttpServletResponse : HTTP Response data,e.g. cookies

I out: jsp.JspWriter : Output stream for page context

I session: http.HttpSession : User specific session data

I application: ServletContext : Data shared by all applicationpages

Page 43: Jsp & Ajax

Implicit Objects in Java Server Page

The following variables can be directly used in JSP:

I page: jsp.HttpJspPage : Page’s servlet instance

I config: ServletConfig : Servlet configuration information

I pageContext: jsp.pageContext : Provides access to all thenamespaces associated with a JSP page and access to severalpage attributes

I request: http.HttpServletRequest : Data included with theHTTP Request

I response: http.HttpServletResponse : HTTP Response data,e.g. cookies

I out: jsp.JspWriter : Output stream for page context

I session: http.HttpSession : User specific session data

I application: ServletContext : Data shared by all applicationpages

Page 44: Jsp & Ajax

Implicit Objects in Java Server Page

The following variables can be directly used in JSP:

I page: jsp.HttpJspPage : Page’s servlet instance

I config: ServletConfig : Servlet configuration information

I pageContext: jsp.pageContext : Provides access to all thenamespaces associated with a JSP page and access to severalpage attributes

I request: http.HttpServletRequest : Data included with theHTTP Request

I response: http.HttpServletResponse : HTTP Response data,e.g. cookies

I out: jsp.JspWriter : Output stream for page context

I session: http.HttpSession : User specific session data

I application: ServletContext : Data shared by all applicationpages

Page 45: Jsp & Ajax

Implicit Objects in Java Server Page

The following variables can be directly used in JSP:

I page: jsp.HttpJspPage : Page’s servlet instance

I config: ServletConfig : Servlet configuration information

I pageContext: jsp.pageContext : Provides access to all thenamespaces associated with a JSP page and access to severalpage attributes

I request: http.HttpServletRequest : Data included with theHTTP Request

I response: http.HttpServletResponse : HTTP Response data,e.g. cookies

I out: jsp.JspWriter : Output stream for page context

I session: http.HttpSession : User specific session data

I application: ServletContext : Data shared by all applicationpages

Page 46: Jsp & Ajax

Implicit Objects in Java Server Page

The following variables can be directly used in JSP:

I page: jsp.HttpJspPage : Page’s servlet instance

I config: ServletConfig : Servlet configuration information

I pageContext: jsp.pageContext : Provides access to all thenamespaces associated with a JSP page and access to severalpage attributes

I request: http.HttpServletRequest : Data included with theHTTP Request

I response: http.HttpServletResponse : HTTP Response data,e.g. cookies

I out: jsp.JspWriter : Output stream for page context

I session: http.HttpSession : User specific session data

I application: ServletContext : Data shared by all applicationpages

Page 47: Jsp & Ajax

Outline

IntroductionOverviewWEB 1.0 vs WEB 2.0

HTTP, CGI, Web Application ModelHTTPCommon Gateway Interface (CGI)Web Application ModelSummary

Servlet & JSPServletJava Server Pages

AJAXUsing AJAX

Summary

Resources

Page 48: Jsp & Ajax

Asynchronous JavaScript And XML (AJAX)

AJAX incorporates several technologies:

I standards-based presentation using XHTML and CSS;

I dynamic display and interaction using the Document ObjectModel;

I data interchange and manipulation using XML and XSLT;

I asynchronous data retrieval using XMLHttpRequest;

I and JavaScript binding everything together.

www.w3schools.com provides tutorials for XHTML, CSS, DOM,XML, AJAX, JavaScript etc.

Page 49: Jsp & Ajax

Asynchronous JavaScript And XML (AJAX)

AJAX incorporates several technologies:

I standards-based presentation using XHTML and CSS;

I dynamic display and interaction using the Document ObjectModel;

I data interchange and manipulation using XML and XSLT;

I asynchronous data retrieval using XMLHttpRequest;

I and JavaScript binding everything together.

www.w3schools.com provides tutorials for XHTML, CSS, DOM,XML, AJAX, JavaScript etc.

Page 50: Jsp & Ajax

Asynchronous JavaScript And XML (AJAX)

AJAX incorporates several technologies:

I standards-based presentation using XHTML and CSS;

I dynamic display and interaction using the Document ObjectModel;

I data interchange and manipulation using XML and XSLT;

I asynchronous data retrieval using XMLHttpRequest;

I and JavaScript binding everything together.

www.w3schools.com provides tutorials for XHTML, CSS, DOM,XML, AJAX, JavaScript etc.

Page 51: Jsp & Ajax

Asynchronous JavaScript And XML (AJAX)

AJAX incorporates several technologies:

I standards-based presentation using XHTML and CSS;

I dynamic display and interaction using the Document ObjectModel;

I data interchange and manipulation using XML and XSLT;

I asynchronous data retrieval using XMLHttpRequest;

I and JavaScript binding everything together.

www.w3schools.com provides tutorials for XHTML, CSS, DOM,XML, AJAX, JavaScript etc.

Page 52: Jsp & Ajax

Asynchronous JavaScript And XML (AJAX)

AJAX incorporates several technologies:

I standards-based presentation using XHTML and CSS;

I dynamic display and interaction using the Document ObjectModel;

I data interchange and manipulation using XML and XSLT;

I asynchronous data retrieval using XMLHttpRequest;

I and JavaScript binding everything together.

www.w3schools.com provides tutorials for XHTML, CSS, DOM,XML, AJAX, JavaScript etc.

Page 53: Jsp & Ajax

AJAX: Connect HTML with JavaScript

Capture Events on HTML pages

I Mouse event (with elements): mouse down, mouse up, mousemove, mouse over ...

I Keyboard event: key pressed ...

I Form: submitted ...

I Timer etc.

Example: Call the getListByCategory function when the mouse ismoved over the hyper linked text, and call the function cleanTablewhen the moused is moved out the link.

<a href="/" onmouseover="getListByCategory()"onmouseout="clearTable()">Show Member List</a>

Page 54: Jsp & Ajax

AJAX: Create XMLHttpRequest Object

This function will create an XMLHttpRequest object and send a GETrequest to the server page http://pinfo.unige.ch:8080/Member/list.jspwith parameter category with value pinfo05.

Send a HTTP request via XMLHttpRequest Object:

function getListByCategory() {

var req = false;

var self = this;

var url = "http://pinfo.unige.ch:8080/Member/list.jsp?category=pinfo05";

if (window.XMLHttpRequest) {

self.req = new XMLHttpRequest(); // for Firefox and other browsers

} else if (window.ActiveXObject) {

self.req = new ActiveXObject("Microsoft.XMLHTTP"); // for IE browser

}

// when the request is finished, call this function

self.req.onreadystatechange = processRequest;

self.req.open("GET", url, true); // it is a GET request

self.req.send(null); // send the request

}

Note that the implementation of XMLHttpRequest is different from one

browser to another.

Page 55: Jsp & Ajax

AJAX: Handling Asynchronous Response

A node is tagged with id ”result” in the HTML document

<div id=result></div>

The function processRequest is called when the XMLHttpRequestis finished (asynchronous response).

function processRequest() {

// check the request state is "complete"

if (req.readyState == 4 || req.readyState == "complete") {

if (req.status == 200) {

updatepage(self.req.responseText);

} else {

alert("Not able to retrieve member list");

}

}

}

// Just set the content of note with id "result" with the input parameter

// Other DOM and XML operations are possible, here it is simplified.

function updatepage(str) {

document.getElementById("result").innerHTML = str;

}

Page 56: Jsp & Ajax

AJAX: Web Application Model

Figure: AJAX Web Application Model

Page 57: Jsp & Ajax

AJAX: Interactions

Figure: AJAX Interactions

Page 58: Jsp & Ajax

Outline

IntroductionOverviewWEB 1.0 vs WEB 2.0

HTTP, CGI, Web Application ModelHTTPCommon Gateway Interface (CGI)Web Application ModelSummary

Servlet & JSPServletJava Server Pages

AJAXUsing AJAX

Summary

Resources

Page 59: Jsp & Ajax

Summary

I Web Application Model: relations between client, Web server,server side application, and web application. The ApplicationContainer model is mature.

I JSP and Servlets provide server side programming facilities:reusability, manageability, security etc.

I AJAX provides a standard solution for client/serverinteraction: XML based, Asynchronous, no page refreshneeded. It is the most used technology in WEB 2.0.

I AJAX is the complement for the presentation layer of Webapplications with JSP: JSP provides services, AJAX use theseservices, messages are in XML, communication via HTTP.

I WEB 2.0 is more than AJAX.

Page 60: Jsp & Ajax

Summary

I Web Application Model: relations between client, Web server,server side application, and web application. The ApplicationContainer model is mature.

I JSP and Servlets provide server side programming facilities:reusability, manageability, security etc.

I AJAX provides a standard solution for client/serverinteraction: XML based, Asynchronous, no page refreshneeded. It is the most used technology in WEB 2.0.

I AJAX is the complement for the presentation layer of Webapplications with JSP: JSP provides services, AJAX use theseservices, messages are in XML, communication via HTTP.

I WEB 2.0 is more than AJAX.

Page 61: Jsp & Ajax

Summary

I Web Application Model: relations between client, Web server,server side application, and web application. The ApplicationContainer model is mature.

I JSP and Servlets provide server side programming facilities:reusability, manageability, security etc.

I AJAX provides a standard solution for client/serverinteraction: XML based, Asynchronous, no page refreshneeded. It is the most used technology in WEB 2.0.

I AJAX is the complement for the presentation layer of Webapplications with JSP: JSP provides services, AJAX use theseservices, messages are in XML, communication via HTTP.

I WEB 2.0 is more than AJAX.

Page 62: Jsp & Ajax

Summary

I Web Application Model: relations between client, Web server,server side application, and web application. The ApplicationContainer model is mature.

I JSP and Servlets provide server side programming facilities:reusability, manageability, security etc.

I AJAX provides a standard solution for client/serverinteraction: XML based, Asynchronous, no page refreshneeded. It is the most used technology in WEB 2.0.

I AJAX is the complement for the presentation layer of Webapplications with JSP: JSP provides services, AJAX use theseservices, messages are in XML, communication via HTTP.

I WEB 2.0 is more than AJAX.

Page 63: Jsp & Ajax

Summary

I Web Application Model: relations between client, Web server,server side application, and web application. The ApplicationContainer model is mature.

I JSP and Servlets provide server side programming facilities:reusability, manageability, security etc.

I AJAX provides a standard solution for client/serverinteraction: XML based, Asynchronous, no page refreshneeded. It is the most used technology in WEB 2.0.

I AJAX is the complement for the presentation layer of Webapplications with JSP: JSP provides services, AJAX use theseservices, messages are in XML, communication via HTTP.

I WEB 2.0 is more than AJAX.

Page 64: Jsp & Ajax

Summary

I Web Application Model: relations between client, Web server,server side application, and web application. The ApplicationContainer model is mature.

I JSP and Servlets provide server side programming facilities:reusability, manageability, security etc.

I AJAX provides a standard solution for client/serverinteraction: XML based, Asynchronous, no page refreshneeded. It is the most used technology in WEB 2.0.

I AJAX is the complement for the presentation layer of Webapplications with JSP: JSP provides services, AJAX use theseservices, messages are in XML, communication via HTTP.

I WEB 2.0 is more than AJAX.

Page 65: Jsp & Ajax

Outline

IntroductionOverviewWEB 1.0 vs WEB 2.0

HTTP, CGI, Web Application ModelHTTPCommon Gateway Interface (CGI)Web Application ModelSummary

Servlet & JSPServletJava Server Pages

AJAXUsing AJAX

Summary

Resources

Page 66: Jsp & Ajax

Servlet, JSP

Specifications:

I Java Servlet Specifications

I JSP Technology

Tutorial:

I Basic Servlets:http://www.apl.jhu.edu/ hall/java/Servlet-Tutorial/

I MoreServlets Book & Tutorials: Servlet, JSP, Taglib, Struts,AJAX

Page 67: Jsp & Ajax

AJAX, WEB 2.0

AJAX:

I Ajax: A New Approach to Web Applications

I AJAX: Getting Started, mozilla developer center

I Top 10 Ajax Applications

I AJAX Tutorial

I AJAX Login System Demo

I Guide to Using AJAX and XMLHttpRequest

I Ajaxian

WEB 2.0

I O’Reilly: What Is Web 2.0

I The Best Web 2.0 Software of 2005

I Writly

I Google Earth