comp9321 web application engineeringcs9321/15s2/lectures/lec02/lec-02.pdf · presentation layer...
TRANSCRIPT
![Page 1: COMP9321 Web Application Engineeringcs9321/15s2/lectures/lec02/Lec-02.pdf · Presentation Layer COMP9321, 15s2, Week 2 3 HTML is a markup language for describing Web documents (Web](https://reader034.vdocuments.us/reader034/viewer/2022043022/5f3e17544290d74c4274317c/html5/thumbnails/1.jpg)
COMP9321 Web Application EngineeringSemester 2, 2015
Dr. Amin BeheshtiService Oriented Computing Group, CSE, UNSW Australia
Week 2
1COMP9321, 15s2, Week 2
http://webapps.cse.unsw.edu.au/webcms2/course/index.php?cid=2411
![Page 2: COMP9321 Web Application Engineeringcs9321/15s2/lectures/lec02/Lec-02.pdf · Presentation Layer COMP9321, 15s2, Week 2 3 HTML is a markup language for describing Web documents (Web](https://reader034.vdocuments.us/reader034/viewer/2022043022/5f3e17544290d74c4274317c/html5/thumbnails/2.jpg)
Different Layers in an Application
2COMP9321, 15s2, Week 2
![Page 3: COMP9321 Web Application Engineeringcs9321/15s2/lectures/lec02/Lec-02.pdf · Presentation Layer COMP9321, 15s2, Week 2 3 HTML is a markup language for describing Web documents (Web](https://reader034.vdocuments.us/reader034/viewer/2022043022/5f3e17544290d74c4274317c/html5/thumbnails/3.jpg)
Presentation Layer
3COMP9321, 15s2, Week 2
HTML is a markup language for describing Web documents (Web pages).
set of markup tags
is-a
describe
different document content
html -> describes an HTML document.head -> provides information about the document.title -> provides a title for the document.body -> describes the visible page content.h1 -> describes a heading.p -> describes a paragraph
![Page 4: COMP9321 Web Application Engineeringcs9321/15s2/lectures/lec02/Lec-02.pdf · Presentation Layer COMP9321, 15s2, Week 2 3 HTML is a markup language for describing Web documents (Web](https://reader034.vdocuments.us/reader034/viewer/2022043022/5f3e17544290d74c4274317c/html5/thumbnails/4.jpg)
Presentation Layer
4COMP9321, 15s2, Week 2
HTML forms are used to collect user input.
If you click "Submit", the form-data will be sent to a page called "main.jsp".
![Page 5: COMP9321 Web Application Engineeringcs9321/15s2/lectures/lec02/Lec-02.pdf · Presentation Layer COMP9321, 15s2, Week 2 3 HTML is a markup language for describing Web documents (Web](https://reader034.vdocuments.us/reader034/viewer/2022043022/5f3e17544290d74c4274317c/html5/thumbnails/5.jpg)
Presentation Layer
5COMP9321, 15s2, Week 2
HTML <form> method attribute
how to send form-data
URL variables (with method="get") , or HTTP post transaction (with method="post").
specifies
as
Method? Get/Post
![Page 6: COMP9321 Web Application Engineeringcs9321/15s2/lectures/lec02/Lec-02.pdf · Presentation Layer COMP9321, 15s2, Week 2 3 HTML is a markup language for describing Web documents (Web](https://reader034.vdocuments.us/reader034/viewer/2022043022/5f3e17544290d74c4274317c/html5/thumbnails/6.jpg)
Presentation Layer
6COMP9321, 15s2, Week 2
HTML <form> method attribute
GET:• Appends form-data into the URL in name/value pairs• The length of a URL is limited (about 2-3k characters)• Never use GET to send sensitive data! (will be visible in the URL)• Useful for form submissions where a user want to bookmark the result,
to enable quick access in future.• GET is better for non-secure data, like query strings in Google.• GET is supposed to be used for getting things - information retrieval
With the GET method, the HTTP request looks like this:
![Page 7: COMP9321 Web Application Engineeringcs9321/15s2/lectures/lec02/Lec-02.pdf · Presentation Layer COMP9321, 15s2, Week 2 3 HTML is a markup language for describing Web documents (Web](https://reader034.vdocuments.us/reader034/viewer/2022043022/5f3e17544290d74c4274317c/html5/thumbnails/7.jpg)
Presentation Layer
7COMP9321, 15s2, Week 2
HTML <form> method attribute
POST:• Appends form-data inside the body of the HTTP request (data is not
shown in URL).• Has no size limitations.• Form submissions with POST cannot be bookmarked.• POST is supposed to be used for sending data to be processed - update or
change something on the serverWhen sent using the POST method, the HTTP request looks like this:
![Page 8: COMP9321 Web Application Engineeringcs9321/15s2/lectures/lec02/Lec-02.pdf · Presentation Layer COMP9321, 15s2, Week 2 3 HTML is a markup language for describing Web documents (Web](https://reader034.vdocuments.us/reader034/viewer/2022043022/5f3e17544290d74c4274317c/html5/thumbnails/8.jpg)
Static vs. Dynamic Web Page
8COMP9321, 15s2, Week 2
A static web page is delivered to the user exactly as stored, in contrast to dynamic web pages which are generated by a web application, and on demand!
Static websites:• Quick to develop• Cheap to develop• Cheap to host
Dynamic websites:• Much more functional website• Much easier to update• New content brings people back to the site and helps in the search engines• Can work as a system to allow staff or users to collaborate• Slower / more expensive to develop• Hosting costs
![Page 9: COMP9321 Web Application Engineeringcs9321/15s2/lectures/lec02/Lec-02.pdf · Presentation Layer COMP9321, 15s2, Week 2 3 HTML is a markup language for describing Web documents (Web](https://reader034.vdocuments.us/reader034/viewer/2022043022/5f3e17544290d74c4274317c/html5/thumbnails/9.jpg)
Static vs. Dynamic Web Page
9COMP9321, 15s2, Week 2
A static web page is delivered to the user exactly as stored, in contrast to dynamic web pages which are generated by a web application, and on demand!
web page whose construction is controlled by an application server processing server-side scripts.
software framework that provides both facilities to create web applications and a server environment to run them.
is-a
is-a
Java application servers
e.g.
It's core set of API and features are defined by Java EE.
The Web modules include Java Servlets and Java Server Pages.
http://docs.oracle.com/javaee/6/tutorial/doc/
![Page 10: COMP9321 Web Application Engineeringcs9321/15s2/lectures/lec02/Lec-02.pdf · Presentation Layer COMP9321, 15s2, Week 2 3 HTML is a markup language for describing Web documents (Web](https://reader034.vdocuments.us/reader034/viewer/2022043022/5f3e17544290d74c4274317c/html5/thumbnails/10.jpg)
Java Servlets
10COMP9321, 15s2, Week 2
http://java.sun.com/products/servlet/index.jsphttp://docs.oracle.com/javaee/6/tutorial/doc/bnafd.html
![Page 11: COMP9321 Web Application Engineeringcs9321/15s2/lectures/lec02/Lec-02.pdf · Presentation Layer COMP9321, 15s2, Week 2 3 HTML is a markup language for describing Web documents (Web](https://reader034.vdocuments.us/reader034/viewer/2022043022/5f3e17544290d74c4274317c/html5/thumbnails/11.jpg)
Apache Tomcat
11COMP9321, 15s2, Week 2
Apache Tomcat™ is an open source software implementation of the Java Servlet, JavaServer Pages, … .
Tomcat's Installed Directory Structure:• bin: for Tomcat's binaries and startup scripts.• conf: global configuration applicable to all the webapps.• lib: Keeps the JAR-file that are available to all webapps.• logs: contains the engine logfile Catalina ("Catalina" is the codename
for Tomcat 5 and above).• webapps: the default appBase - web applications base directory of the
host localhost.• work: contains the translated servlet source files and classes of JSP.• temp: temporary files.
![Page 12: COMP9321 Web Application Engineeringcs9321/15s2/lectures/lec02/Lec-02.pdf · Presentation Layer COMP9321, 15s2, Week 2 3 HTML is a markup language for describing Web documents (Web](https://reader034.vdocuments.us/reader034/viewer/2022043022/5f3e17544290d74c4274317c/html5/thumbnails/12.jpg)
Apache Tomcat
12COMP9321, 15s2, Week 2
![Page 13: COMP9321 Web Application Engineeringcs9321/15s2/lectures/lec02/Lec-02.pdf · Presentation Layer COMP9321, 15s2, Week 2 3 HTML is a markup language for describing Web documents (Web](https://reader034.vdocuments.us/reader034/viewer/2022043022/5f3e17544290d74c4274317c/html5/thumbnails/13.jpg)
Making servlets available in the Web container
13COMP9321, 15s2, Week 2
e.g., Ant tasks for Tomcat, Eclipse Web Tools Platform (WTP)
![Page 14: COMP9321 Web Application Engineeringcs9321/15s2/lectures/lec02/Lec-02.pdf · Presentation Layer COMP9321, 15s2, Week 2 3 HTML is a markup language for describing Web documents (Web](https://reader034.vdocuments.us/reader034/viewer/2022043022/5f3e17544290d74c4274317c/html5/thumbnails/14.jpg)
How container handles the Servlet request
14COMP9321, 15s2, Week 2
1. Client Browse Servlet URL: User clicks a link that has a URL of Servlet.
2. HttpServletRequest and HttpServletResponse: Container (e.g. Apache Tomcat) sees that the request is for servlet , so create two objects : Request and Response
((HeadFirst) p.42)http://www.jitendrazaa.com/blog/java/servlet/
![Page 15: COMP9321 Web Application Engineeringcs9321/15s2/lectures/lec02/Lec-02.pdf · Presentation Layer COMP9321, 15s2, Week 2 3 HTML is a markup language for describing Web documents (Web](https://reader034.vdocuments.us/reader034/viewer/2022043022/5f3e17544290d74c4274317c/html5/thumbnails/15.jpg)
How container handles the Servlet request
15COMP9321, 15s2, Week 2
3. Create Thread for Servlet: Container finds correct servlet using “web.xml” file; and Creates/Allocate thread for that request …
4. Service method of servlet: Container calls the servlets service() method, on the type of request, service calls doGet() or doPost() methods.
((HeadFirst) p.42)http://www.jitendrazaa.com/blog/java/servlet/
![Page 16: COMP9321 Web Application Engineeringcs9321/15s2/lectures/lec02/Lec-02.pdf · Presentation Layer COMP9321, 15s2, Week 2 3 HTML is a markup language for describing Web documents (Web](https://reader034.vdocuments.us/reader034/viewer/2022043022/5f3e17544290d74c4274317c/html5/thumbnails/16.jpg)
How container handles the Servlet request
16COMP9321, 15s2, Week 2
5. Servlet response object: Lets assume that service calls the doPost() method. doPost() method generates dynamic page and add the page in response object.
6. destroy response and request object: Thread completes, container converts the response object into HttpResponse object and destroys the response and request object.
((HeadFirst) p.42)http://www.jitendrazaa.com/blog/java/servlet/
![Page 17: COMP9321 Web Application Engineeringcs9321/15s2/lectures/lec02/Lec-02.pdf · Presentation Layer COMP9321, 15s2, Week 2 3 HTML is a markup language for describing Web documents (Web](https://reader034.vdocuments.us/reader034/viewer/2022043022/5f3e17544290d74c4274317c/html5/thumbnails/17.jpg)
Your Servlet inherits "lifecycle" methods
17COMP9321, 15s2, Week 2
![Page 18: COMP9321 Web Application Engineeringcs9321/15s2/lectures/lec02/Lec-02.pdf · Presentation Layer COMP9321, 15s2, Week 2 3 HTML is a markup language for describing Web documents (Web](https://reader034.vdocuments.us/reader034/viewer/2022043022/5f3e17544290d74c4274317c/html5/thumbnails/18.jpg)
A typical Servlet looks like this
18COMP9321, 15s2, Week 2
Note: No main() - the container calls the servlet methods like doGet() through service()
![Page 19: COMP9321 Web Application Engineeringcs9321/15s2/lectures/lec02/Lec-02.pdf · Presentation Layer COMP9321, 15s2, Week 2 3 HTML is a markup language for describing Web documents (Web](https://reader034.vdocuments.us/reader034/viewer/2022043022/5f3e17544290d74c4274317c/html5/thumbnails/19.jpg)
Servlet Names
19COMP9321, 15s2, Week 2
![Page 20: COMP9321 Web Application Engineeringcs9321/15s2/lectures/lec02/Lec-02.pdf · Presentation Layer COMP9321, 15s2, Week 2 3 HTML is a markup language for describing Web documents (Web](https://reader034.vdocuments.us/reader034/viewer/2022043022/5f3e17544290d74c4274317c/html5/thumbnails/20.jpg)
Servlet Names
20COMP9321, 15s2, Week 2
Why do we need a servlet name?It allows you to have multiple servlet mappings on a single servlet instance without the unnecessary need to create a separate instance per mapping. More?
<servlet><servlet-name>someServlet</servlet-name><servlet-class>com.example.SomeServlet</servlet-class>
</servlet><servlet-mapping>
<servlet-name>someServlet</servlet-name><url-pattern>/enroll</url-pattern>
</servlet-mapping><servlet-mapping>
<servlet-name>someServlet</servlet-name><url-pattern>/pay</url-pattern>
</servlet-mapping><servlet-mapping>
<servlet-name>someServlet</servlet-name><url-pattern>/bill</url-pattern>
</servlet-mapping>
![Page 21: COMP9321 Web Application Engineeringcs9321/15s2/lectures/lec02/Lec-02.pdf · Presentation Layer COMP9321, 15s2, Week 2 3 HTML is a markup language for describing Web documents (Web](https://reader034.vdocuments.us/reader034/viewer/2022043022/5f3e17544290d74c4274317c/html5/thumbnails/21.jpg)
A Lifecycle of a Servlet
21COMP9321, 15s2, Week 2
The Web container controls the lifecycle of a servlet class:
![Page 22: COMP9321 Web Application Engineeringcs9321/15s2/lectures/lec02/Lec-02.pdf · Presentation Layer COMP9321, 15s2, Week 2 3 HTML is a markup language for describing Web documents (Web](https://reader034.vdocuments.us/reader034/viewer/2022043022/5f3e17544290d74c4274317c/html5/thumbnails/22.jpg)
Servlet's Life
22COMP9321, 15s2, Week 2((HeadFirst) p.99)
![Page 23: COMP9321 Web Application Engineeringcs9321/15s2/lectures/lec02/Lec-02.pdf · Presentation Layer COMP9321, 15s2, Week 2 3 HTML is a markup language for describing Web documents (Web](https://reader034.vdocuments.us/reader034/viewer/2022043022/5f3e17544290d74c4274317c/html5/thumbnails/23.jpg)
Why care about this initialisation details?
23COMP9321, 15s2, Week 2
Once the servlet is initialised, the servlet gets access to two important objects: ServletConfig and ServletContext
ServletConfig:• It is implemented by the servlet container to initialize a single servlet using init();• you can pass initialization parameters to the servlet using the web.xml;• For understanding, this is similar to a constructor in a java class;• One ServletCong object per servlet;• Use it to pass deploy-time information to the servlet (any info. that you do not want to
hard-code into the servlet);• Parameters are configured in the deployment descriptor;• Use it to access the ServletContext;
<servlet><servlet-name>ServletConfigTest</servlet-name><servlet-class>com.javapapers.ServletConfigTest</servlet-class><init-param>
<param-name>topic</param-name><param-value>Difference between ServletConfig and ServletContext</param-value>
</init-param></servlet>
![Page 24: COMP9321 Web Application Engineeringcs9321/15s2/lectures/lec02/Lec-02.pdf · Presentation Layer COMP9321, 15s2, Week 2 3 HTML is a markup language for describing Web documents (Web](https://reader034.vdocuments.us/reader034/viewer/2022043022/5f3e17544290d74c4274317c/html5/thumbnails/24.jpg)
Why care about this initialisation details?
24COMP9321, 15s2, Week 2
Once the servlet is initialised, the servlet gets access to two important objects: ServletConfig and ServletContext
ServletContext:• It is implemented by the servlet container for all servlets to communicate with its
servlet container;• For example, to get the MIME type of a file, to get dispatch requests, or to write to a
log file. That is to get detail about its execution environment;• For understanding, this is like a application global variable mechanism for a single
web application deployed in only one JVM;• One ServletContext per Web application (they should have named it AppContext);• Use it to access parameters that are Web application-wide;
• The ServletContext object is contained within the ServletConfig object, and can be accessed using the ServletConfig object within a servlet; You can specify param-value pairs for ServletContext object in <context-param> tags in web.xml file.
<context-param><param-name>globalVariable</param-name><param-value>comp9321</param-value>
</context-param>
![Page 25: COMP9321 Web Application Engineeringcs9321/15s2/lectures/lec02/Lec-02.pdf · Presentation Layer COMP9321, 15s2, Week 2 3 HTML is a markup language for describing Web documents (Web](https://reader034.vdocuments.us/reader034/viewer/2022043022/5f3e17544290d74c4274317c/html5/thumbnails/25.jpg)
Why care about this initialisation details?
25COMP9321, 15s2, Week 2
So to sum up there is only one ServletContext for an entire app and all the parts of the web app share it. But each servlet in the app has its own ServletConfig.
Remember if you change a value you must redeploy the web app in order to get the new value because a servlet is only initialized once at the beginning of its life.
![Page 26: COMP9321 Web Application Engineeringcs9321/15s2/lectures/lec02/Lec-02.pdf · Presentation Layer COMP9321, 15s2, Week 2 3 HTML is a markup language for describing Web documents (Web](https://reader034.vdocuments.us/reader034/viewer/2022043022/5f3e17544290d74c4274317c/html5/thumbnails/26.jpg)
ServletRequest & ServletResponse Interfaces
26COMP9321, 15s2, Week 2
The container implements HttpServletRequest and HttpServletRequest
All you should care about is when servlet is called, the service() passes two objects to your servlet.
![Page 27: COMP9321 Web Application Engineeringcs9321/15s2/lectures/lec02/Lec-02.pdf · Presentation Layer COMP9321, 15s2, Week 2 3 HTML is a markup language for describing Web documents (Web](https://reader034.vdocuments.us/reader034/viewer/2022043022/5f3e17544290d74c4274317c/html5/thumbnails/27.jpg)
HttpServletRequest, HttpServletResponse
27COMP9321, 15s2, Week 2
![Page 28: COMP9321 Web Application Engineeringcs9321/15s2/lectures/lec02/Lec-02.pdf · Presentation Layer COMP9321, 15s2, Week 2 3 HTML is a markup language for describing Web documents (Web](https://reader034.vdocuments.us/reader034/viewer/2022043022/5f3e17544290d74c4274317c/html5/thumbnails/28.jpg)
A simple servlet that generates a text message
28COMP9321, 15s2, Week 2
![Page 29: COMP9321 Web Application Engineeringcs9321/15s2/lectures/lec02/Lec-02.pdf · Presentation Layer COMP9321, 15s2, Week 2 3 HTML is a markup language for describing Web documents (Web](https://reader034.vdocuments.us/reader034/viewer/2022043022/5f3e17544290d74c4274317c/html5/thumbnails/29.jpg)
RequestDispatcher in Servlet
29COMP9321, 15s2, Week 2
• The RequestDispatcher interface provides the facility of dispatching the request to another resource, e.g., servlet, jsp, or html.
• This interface can also be used to include the content of another resource also.• It is one of the way of servlet collaboration.
http://www.javatpoint.com/
The RequestDispatcher interface provides two methods: forward and include• Forward: Forwards a request from a servlet to another resource
(servlet, JSP file, or HTML file) on the server.
public void forward(ServletRequest request,ServletResponse response)
![Page 30: COMP9321 Web Application Engineeringcs9321/15s2/lectures/lec02/Lec-02.pdf · Presentation Layer COMP9321, 15s2, Week 2 3 HTML is a markup language for describing Web documents (Web](https://reader034.vdocuments.us/reader034/viewer/2022043022/5f3e17544290d74c4274317c/html5/thumbnails/30.jpg)
RequestDispatcher in Servlet
30COMP9321, 15s2, Week 2
• The RequestDispatcher interface provides the facility of dispatching the request to another resource, e.g., servlet, jsp, or html.
• This interface can also be used to include the content of another resource also.• It is one of the way of servlet collaboration.
http://www.javatpoint.com/
The RequestDispatcher interface provides two methods: forward and include• Include: Includes the content of a resource (servlet, JSP page, or HTML file)
in the response.
public void include(ServletRequest request,ServletResponse response)
![Page 31: COMP9321 Web Application Engineeringcs9321/15s2/lectures/lec02/Lec-02.pdf · Presentation Layer COMP9321, 15s2, Week 2 3 HTML is a markup language for describing Web documents (Web](https://reader034.vdocuments.us/reader034/viewer/2022043022/5f3e17544290d74c4274317c/html5/thumbnails/31.jpg)
Attributes and Sharing Attributes
31COMP9321, 15s2, Week 2
![Page 32: COMP9321 Web Application Engineeringcs9321/15s2/lectures/lec02/Lec-02.pdf · Presentation Layer COMP9321, 15s2, Week 2 3 HTML is a markup language for describing Web documents (Web](https://reader034.vdocuments.us/reader034/viewer/2022043022/5f3e17544290d74c4274317c/html5/thumbnails/32.jpg)
Who has access to the board and how long does it live?
32COMP9321, 15s2, Week 2
![Page 33: COMP9321 Web Application Engineeringcs9321/15s2/lectures/lec02/Lec-02.pdf · Presentation Layer COMP9321, 15s2, Week 2 3 HTML is a markup language for describing Web documents (Web](https://reader034.vdocuments.us/reader034/viewer/2022043022/5f3e17544290d74c4274317c/html5/thumbnails/33.jpg)
Attributes API
33COMP9321, 15s2, Week 2
HeadFirst, p. 189
![Page 34: COMP9321 Web Application Engineeringcs9321/15s2/lectures/lec02/Lec-02.pdf · Presentation Layer COMP9321, 15s2, Week 2 3 HTML is a markup language for describing Web documents (Web](https://reader034.vdocuments.us/reader034/viewer/2022043022/5f3e17544290d74c4274317c/html5/thumbnails/34.jpg)
Attributes are not parameters
34COMP9321, 15s2, Week 2
Difference between getAttribute() and getParameter() ?• The return type for attributes is an Object, whereas the return type for a parameter is a String.• When calling the getAttribute( ) method, bear in mind that the attributes must be cast.• Notice: there is no servlet specific attributes, and there are no session parameters.• e.g. before you forward the request (using RequestDispatcher) you can set an attribute which will be
available to the next resource.
![Page 35: COMP9321 Web Application Engineeringcs9321/15s2/lectures/lec02/Lec-02.pdf · Presentation Layer COMP9321, 15s2, Week 2 3 HTML is a markup language for describing Web documents (Web](https://reader034.vdocuments.us/reader034/viewer/2022043022/5f3e17544290d74c4274317c/html5/thumbnails/35.jpg)
Request attributes and Request dispatching
35COMP9321, 15s2, Week 2
![Page 36: COMP9321 Web Application Engineeringcs9321/15s2/lectures/lec02/Lec-02.pdf · Presentation Layer COMP9321, 15s2, Week 2 3 HTML is a markup language for describing Web documents (Web](https://reader034.vdocuments.us/reader034/viewer/2022043022/5f3e17544290d74c4274317c/html5/thumbnails/36.jpg)
Managing the User State
36COMP9321, 15s2, Week 2
![Page 37: COMP9321 Web Application Engineeringcs9321/15s2/lectures/lec02/Lec-02.pdf · Presentation Layer COMP9321, 15s2, Week 2 3 HTML is a markup language for describing Web documents (Web](https://reader034.vdocuments.us/reader034/viewer/2022043022/5f3e17544290d74c4274317c/html5/thumbnails/37.jpg)
Managing the User State
37COMP9321, 15s2, Week 2
A problem in HTTP request/response:• HTTP is a stateless protocol.
• A single request/response;• Nothing is remembered 'between requests' from the same user;
• Web applications need to maintain users + their data.
It is a programmer's responsibility:• The term "session" is used to represent the data associated with one user while
she navigates around a Web application.• Session is a conversional state between client and server.• Session can consists of multiple request and response between client and server.• Since HTTP is stateless, the only way to maintain a session is when some unique
information about the session (session id) is passed between server and client in every request and response.
![Page 38: COMP9321 Web Application Engineeringcs9321/15s2/lectures/lec02/Lec-02.pdf · Presentation Layer COMP9321, 15s2, Week 2 3 HTML is a markup language for describing Web documents (Web](https://reader034.vdocuments.us/reader034/viewer/2022043022/5f3e17544290d74c4274317c/html5/thumbnails/38.jpg)
Session Management
38COMP9321, 15s2, Week 2
![Page 39: COMP9321 Web Application Engineeringcs9321/15s2/lectures/lec02/Lec-02.pdf · Presentation Layer COMP9321, 15s2, Week 2 3 HTML is a markup language for describing Web documents (Web](https://reader034.vdocuments.us/reader034/viewer/2022043022/5f3e17544290d74c4274317c/html5/thumbnails/39.jpg)
Session Management
39COMP9321, 15s2, Week 2
![Page 40: COMP9321 Web Application Engineeringcs9321/15s2/lectures/lec02/Lec-02.pdf · Presentation Layer COMP9321, 15s2, Week 2 3 HTML is a markup language for describing Web documents (Web](https://reader034.vdocuments.us/reader034/viewer/2022043022/5f3e17544290d74c4274317c/html5/thumbnails/40.jpg)
Session Management
40COMP9321, 15s2, Week 2
![Page 41: COMP9321 Web Application Engineeringcs9321/15s2/lectures/lec02/Lec-02.pdf · Presentation Layer COMP9321, 15s2, Week 2 3 HTML is a markup language for describing Web documents (Web](https://reader034.vdocuments.us/reader034/viewer/2022043022/5f3e17544290d74c4274317c/html5/thumbnails/41.jpg)
URL Rewriting
41COMP9321, 15s2, Week 2
![Page 42: COMP9321 Web Application Engineeringcs9321/15s2/lectures/lec02/Lec-02.pdf · Presentation Layer COMP9321, 15s2, Week 2 3 HTML is a markup language for describing Web documents (Web](https://reader034.vdocuments.us/reader034/viewer/2022043022/5f3e17544290d74c4274317c/html5/thumbnails/42.jpg)
HTML hidden fields
42COMP9321, 15s2, Week 2
![Page 43: COMP9321 Web Application Engineeringcs9321/15s2/lectures/lec02/Lec-02.pdf · Presentation Layer COMP9321, 15s2, Week 2 3 HTML is a markup language for describing Web documents (Web](https://reader034.vdocuments.us/reader034/viewer/2022043022/5f3e17544290d74c4274317c/html5/thumbnails/43.jpg)
Cookies
43COMP9321, 15s2, Week 2
• Cookies are text files stored on the client computer and they are kept for various information tracking purpose.
• Java Servlets transparently supports HTTP cookies.
• There are three steps involved in identifying returning users:• Server script sends a set of cookies to the browser. e.g. session id• Browser stores this information on local machine for future use.• Next time, browser sends request + those cookies to the server
and server uses that information to identify the user.
![Page 44: COMP9321 Web Application Engineeringcs9321/15s2/lectures/lec02/Lec-02.pdf · Presentation Layer COMP9321, 15s2, Week 2 3 HTML is a markup language for describing Web documents (Web](https://reader034.vdocuments.us/reader034/viewer/2022043022/5f3e17544290d74c4274317c/html5/thumbnails/44.jpg)
Cookies
44COMP9321, 15s2, Week 2
The Anatomy of a Cookie:• Cookies are usually set in an HTTP header.• JavaScript can also set a cookie directly on a browser.• A servlet that sets a cookie might send headers that look something like this:
HTTP/1.1 200 OKDate: Mon, 03 Aug 2015 17:03:38 GMTServer: Apache…Set-Cookie: name=xyz; expires=Monday, 03-Aug-15 22:00:00 GMT;
path=/; domain=comp9321.comConnection: closeContent-Type: text/html
• Set-Cookie header contains a name value pair, a GMT date, a path and a domain.
• The name and value will be URL encoded.
• Expires field is an instruction to the browser to "forget" the cookie after the given time/date.
![Page 45: COMP9321 Web Application Engineeringcs9321/15s2/lectures/lec02/Lec-02.pdf · Presentation Layer COMP9321, 15s2, Week 2 3 HTML is a markup language for describing Web documents (Web](https://reader034.vdocuments.us/reader034/viewer/2022043022/5f3e17544290d74c4274317c/html5/thumbnails/45.jpg)
Servlet Cookies Methods
45COMP9321, 15s2, Week 2
Method Name Method Description
setDomain() sets the domain to which cookie applies, for example comp9321.com.
getDomain() gets the domain to which cookie applies, for example comp9321.com.
setMaxAge() sets how much time (in seconds) should elapse before the cookie expires. If you don't set this, the cookie will last only for the current session.
getMaxAge() returns the maximum age of the cookie, specified in seconds, By default, -1 indicating the cookie will persist until browser shutdown.
getName() returns the name of the cookie. The name cannot be changed after creation.
setValue() sets the value associated with the cookie.
getValue() gets the value associated with the cookie.
setPath() sets the path to which this cookie applies. If you don't specify a path, the cookie is returned for all URLs in the same directory as the current page as well as all subdirectories.
getPath() gets the path to which this cookie applies.
setSecure() sets the boolean value indicating whether the cookie should only be sent over encrypted (i.e. SSL) connections.
setComment() specifies a comment that describes a cookie's purpose. The comment is useful if the browser presents the cookie to the user.
getComment() returns the comment describing the purpose of this cookie, or null if the cookie has no comment.
http://www.tutorialspoint.com/servlets/
![Page 46: COMP9321 Web Application Engineeringcs9321/15s2/lectures/lec02/Lec-02.pdf · Presentation Layer COMP9321, 15s2, Week 2 3 HTML is a markup language for describing Web documents (Web](https://reader034.vdocuments.us/reader034/viewer/2022043022/5f3e17544290d74c4274317c/html5/thumbnails/46.jpg)
Setting Cookies with Servlet
46COMP9321, 15s2, Week 2
Setting cookies with servlet involves three steps:
1. Creating a Cookie object:
Keep in mind, neither the name nor the value should contain white space or any of the following characters: [ ] ( ) = , " / ? @ : ;
2. Setting the maximum age (in seconds):
3. Sending the Cookie into the HTTP response headers
Cookie cookie = new Cookie("key","value");
cookie.setMaxAge(60*60*24);
response.addCookie(cookie);
http://www.tutorialspoint.com/servlets/
![Page 47: COMP9321 Web Application Engineeringcs9321/15s2/lectures/lec02/Lec-02.pdf · Presentation Layer COMP9321, 15s2, Week 2 3 HTML is a markup language for describing Web documents (Web](https://reader034.vdocuments.us/reader034/viewer/2022043022/5f3e17544290d74c4274317c/html5/thumbnails/47.jpg)
HTTP Sessions with JSESSIONID cookie
47COMP9321, 15s2, Week 2
![Page 48: COMP9321 Web Application Engineeringcs9321/15s2/lectures/lec02/Lec-02.pdf · Presentation Layer COMP9321, 15s2, Week 2 3 HTML is a markup language for describing Web documents (Web](https://reader034.vdocuments.us/reader034/viewer/2022043022/5f3e17544290d74c4274317c/html5/thumbnails/48.jpg)
HTTP Sessions Interface
48COMP9321, 15s2, Week 2
![Page 49: COMP9321 Web Application Engineeringcs9321/15s2/lectures/lec02/Lec-02.pdf · Presentation Layer COMP9321, 15s2, Week 2 3 HTML is a markup language for describing Web documents (Web](https://reader034.vdocuments.us/reader034/viewer/2022043022/5f3e17544290d74c4274317c/html5/thumbnails/49.jpg)
An example of using HttpSession object: the scenario
49COMP9321, 15s2, Week 2
![Page 50: COMP9321 Web Application Engineeringcs9321/15s2/lectures/lec02/Lec-02.pdf · Presentation Layer COMP9321, 15s2, Week 2 3 HTML is a markup language for describing Web documents (Web](https://reader034.vdocuments.us/reader034/viewer/2022043022/5f3e17544290d74c4274317c/html5/thumbnails/50.jpg)
The Journey Object
50COMP9321, 15s2, Week 2
![Page 51: COMP9321 Web Application Engineeringcs9321/15s2/lectures/lec02/Lec-02.pdf · Presentation Layer COMP9321, 15s2, Week 2 3 HTML is a markup language for describing Web documents (Web](https://reader034.vdocuments.us/reader034/viewer/2022043022/5f3e17544290d74c4274317c/html5/thumbnails/51.jpg)
Using a Journey Object
51COMP9321, 15s2, Week 2
![Page 52: COMP9321 Web Application Engineeringcs9321/15s2/lectures/lec02/Lec-02.pdf · Presentation Layer COMP9321, 15s2, Week 2 3 HTML is a markup language for describing Web documents (Web](https://reader034.vdocuments.us/reader034/viewer/2022043022/5f3e17544290d74c4274317c/html5/thumbnails/52.jpg)
How session tracking works ...
52COMP9321, 15s2, Week 2
![Page 53: COMP9321 Web Application Engineeringcs9321/15s2/lectures/lec02/Lec-02.pdf · Presentation Layer COMP9321, 15s2, Week 2 3 HTML is a markup language for describing Web documents (Web](https://reader034.vdocuments.us/reader034/viewer/2022043022/5f3e17544290d74c4274317c/html5/thumbnails/53.jpg)
Getting rid of Sessions
53COMP9321, 15s2, Week 2
![Page 54: COMP9321 Web Application Engineeringcs9321/15s2/lectures/lec02/Lec-02.pdf · Presentation Layer COMP9321, 15s2, Week 2 3 HTML is a markup language for describing Web documents (Web](https://reader034.vdocuments.us/reader034/viewer/2022043022/5f3e17544290d74c4274317c/html5/thumbnails/54.jpg)
Servlets ...
54COMP9321, 15s2, Week 2
![Page 55: COMP9321 Web Application Engineeringcs9321/15s2/lectures/lec02/Lec-02.pdf · Presentation Layer COMP9321, 15s2, Week 2 3 HTML is a markup language for describing Web documents (Web](https://reader034.vdocuments.us/reader034/viewer/2022043022/5f3e17544290d74c4274317c/html5/thumbnails/55.jpg)
10 Minutes Break
then
Servlet Examples!
55COMP9321, 15s2, Week 2
![Page 56: COMP9321 Web Application Engineeringcs9321/15s2/lectures/lec02/Lec-02.pdf · Presentation Layer COMP9321, 15s2, Week 2 3 HTML is a markup language for describing Web documents (Web](https://reader034.vdocuments.us/reader034/viewer/2022043022/5f3e17544290d74c4274317c/html5/thumbnails/56.jpg)
Installing Apache Tomcat…
56COMP9321, 15s2, Week 2
download
unzip
![Page 57: COMP9321 Web Application Engineeringcs9321/15s2/lectures/lec02/Lec-02.pdf · Presentation Layer COMP9321, 15s2, Week 2 3 HTML is a markup language for describing Web documents (Web](https://reader034.vdocuments.us/reader034/viewer/2022043022/5f3e17544290d74c4274317c/html5/thumbnails/57.jpg)
Installing Apache Tomcat…
57COMP9321, 15s2, Week 2
Next: Verifying Tomcat's Java Dependencies
Tomcat 8.0 requires Java 7 or later …
System variables: “Java_Home”
How to Set JAVA_HOME environment variable onMacWindows…
Didn’t set? thenGoogle
Check
Notice: we use JDK (not JRE) as we like to experiment and make changes to the example servlets, so we need to compile Java code in the servlets and produce .class files …
![Page 58: COMP9321 Web Application Engineeringcs9321/15s2/lectures/lec02/Lec-02.pdf · Presentation Layer COMP9321, 15s2, Week 2 3 HTML is a markup language for describing Web documents (Web](https://reader034.vdocuments.us/reader034/viewer/2022043022/5f3e17544290d74c4274317c/html5/thumbnails/58.jpg)
Installing Apache Tomcat…
58COMP9321, 15s2, Week 2
e.g. in Windows …
click
![Page 59: COMP9321 Web Application Engineeringcs9321/15s2/lectures/lec02/Lec-02.pdf · Presentation Layer COMP9321, 15s2, Week 2 3 HTML is a markup language for describing Web documents (Web](https://reader034.vdocuments.us/reader034/viewer/2022043022/5f3e17544290d74c4274317c/html5/thumbnails/59.jpg)
Start and Stop Tomcat
59COMP9321, 15s2, Week 2
![Page 60: COMP9321 Web Application Engineeringcs9321/15s2/lectures/lec02/Lec-02.pdf · Presentation Layer COMP9321, 15s2, Week 2 3 HTML is a markup language for describing Web documents (Web](https://reader034.vdocuments.us/reader034/viewer/2022043022/5f3e17544290d74c4274317c/html5/thumbnails/60.jpg)
Start and Stop Tomcat
60COMP9321, 15s2, Week 2
![Page 61: COMP9321 Web Application Engineeringcs9321/15s2/lectures/lec02/Lec-02.pdf · Presentation Layer COMP9321, 15s2, Week 2 3 HTML is a markup language for describing Web documents (Web](https://reader034.vdocuments.us/reader034/viewer/2022043022/5f3e17544290d74c4274317c/html5/thumbnails/61.jpg)
Servlet Examples
61COMP9321, 15s2, Week 2
• Download the Servlet-Example zip file
• Unzip and copy the folder under “webapps” folder
![Page 62: COMP9321 Web Application Engineeringcs9321/15s2/lectures/lec02/Lec-02.pdf · Presentation Layer COMP9321, 15s2, Week 2 3 HTML is a markup language for describing Web documents (Web](https://reader034.vdocuments.us/reader034/viewer/2022043022/5f3e17544290d74c4274317c/html5/thumbnails/62.jpg)
Servlet Examples
62COMP9321, 15s2, Week 2
![Page 63: COMP9321 Web Application Engineeringcs9321/15s2/lectures/lec02/Lec-02.pdf · Presentation Layer COMP9321, 15s2, Week 2 3 HTML is a markup language for describing Web documents (Web](https://reader034.vdocuments.us/reader034/viewer/2022043022/5f3e17544290d74c4274317c/html5/thumbnails/63.jpg)
Servlet Examples: Hello World
63COMP9321, 15s2, Week 2
![Page 64: COMP9321 Web Application Engineeringcs9321/15s2/lectures/lec02/Lec-02.pdf · Presentation Layer COMP9321, 15s2, Week 2 3 HTML is a markup language for describing Web documents (Web](https://reader034.vdocuments.us/reader034/viewer/2022043022/5f3e17544290d74c4274317c/html5/thumbnails/64.jpg)
Servlet Examples: Request Info
64COMP9321, 15s2, Week 2
![Page 65: COMP9321 Web Application Engineeringcs9321/15s2/lectures/lec02/Lec-02.pdf · Presentation Layer COMP9321, 15s2, Week 2 3 HTML is a markup language for describing Web documents (Web](https://reader034.vdocuments.us/reader034/viewer/2022043022/5f3e17544290d74c4274317c/html5/thumbnails/65.jpg)
Servlet Examples: Request Headers
65COMP9321, 15s2, Week 2
![Page 66: COMP9321 Web Application Engineeringcs9321/15s2/lectures/lec02/Lec-02.pdf · Presentation Layer COMP9321, 15s2, Week 2 3 HTML is a markup language for describing Web documents (Web](https://reader034.vdocuments.us/reader034/viewer/2022043022/5f3e17544290d74c4274317c/html5/thumbnails/66.jpg)
Other Examples, LAB
66COMP9321, 15s2, Week 2
![Page 67: COMP9321 Web Application Engineeringcs9321/15s2/lectures/lec02/Lec-02.pdf · Presentation Layer COMP9321, 15s2, Week 2 3 HTML is a markup language for describing Web documents (Web](https://reader034.vdocuments.us/reader034/viewer/2022043022/5f3e17544290d74c4274317c/html5/thumbnails/67.jpg)
67COMP9321, 15s2, Week 2