Advanced Web Architecture Class Notes

Lecture 4
Assignment 1
Servlets Part I
Lecture 5
Servlets Part II
Sessions

TA – Youer Pu –

Lecture 4.

Assignment 1

  • High-level design:
    • Check if the data for passed url is available in Cache and is not expired
    • If so, send back data by constructing the response
      • Add ‘Age’ header
    • If data not available
      • Contact the server get the data
        • Need to send the Host: field need to parse out the Host.

Requirements

  • Reading command line data
  • Parsing stuff out of Strings
  • Reading and writing files
  • Network programming
    • Client Sockets
    • Server Sockets
  • We will be given the server code, for testing

What about server side? What does a server take care of ?

  • Correctly handle GET, HEAD, POST, and PUT methods
  • Should correctly create the requireed response header for each request
  • Should manage and maintain resource
  • Support concurrect clients

Examples

java TestServerSocket
java TestClientSocket localhost /

Servlets

A servlet is a Java-based Web component managed by a cntainer, that generates dynamic content
* Web component
* Understand HTTP
* Container
* That which manages Servlets
* Dynamic content
* Content that depeds on input data

Client -> Web Server -> Servlet Container -> Servlet

Servlet Container

  • Servlet container is a part of a Web server or application server. Built into a host web server or installed as an add-on component to a Web Server via that server’s native extension API
  • A servler container can also be
    • built into or possibly installed into Web-enabled application servers
    • must implement HTTP/1.0 and HTTP/1.1
    • may perform caching subject to RFC 2616
      • may respond without delivering them to servlet
      • may modify requests from the clients before delivering them to the servlet
      • may modify responses from the servlets before sending them to the clients
        • Adding ‘age’ header
      • We will be using Tomcat
    • Servlet Life-cycle
      • Loading and initilization
        • Servlet container is responsible for loading and instantiating servlets
          • Either when the container is started or when the servlet is needed
        • Servlet container calls servlet’s ‘init’ method
        • Servlet container calls servlet’s ‘service’ method

Creating your first Hello World web project with IntelliJ Idea Professional

  • File -> New Project
    • On the left side select Java Enterprise
    • New Project with IntelliJ Idea
      • Either select the application server or select New and find the directory to Tomcat
      • Step 2. Setting the Project name
      • Set Project Name to “Hello World” and Choose a project location
    • Between the <body> </body> tags insert a “Hello World”
    • Setting the Body tags.
    • It’ll take about a minute to initiate the project and once ready the Run button in the Top Right corner will become enabled and green up.
    • Run Tomcat pressing the Run button Run button
    • You should see scrolling text in the console and after 10–20 seconds the browser window should show up and the page will display.
    • HelloWorld

      Troubleshooting

      A bug I ran into was “Unsupported Major.Minor version. ”
      Java version problem

I fixed it by re-installing the Java 8u20 JDK, I tried installing the different versions of Java EE first, and a bunch of random stuff from MacPort and etc, but none of that worked 🙂 So just download and install the Java 8u20 JDK that can be found here.
http://www.oracle.com/technetwork/java/javase/downloads/index.html

Lecture 5

To change the context root, you have to remove the children of the server

Servlets continued

  • Class loading architecture
    • Hierarchy is reversed, first we search locally then we check containers
  • Load on startup
    • Servlet container starts up
      • We add ‘’ tags to web.xml with a value to specify the order of initiazation, if equal it takes the relative order in the document.
      • If we do not use load-on-startup, it loads on hit by default.
    • When the first request hits the Servlet
      • Adds a initialization delay to the request
  • Servlet initiation parameters
    • Parameters that are passed to a Servlet at the Servlet initialization time
      • Context init params
        • These apply to all Servletss defined in a deployment descipton
  • Servlets request parameters
    • Query Parameters
      • Passed as a query string with ?
  • HTTPRequest object
  • Sessions

Servlet message passing example

<servlet>
</servlet>  
<servlet-mapping>   
</servlet-mapping>  
<init-param>   
        <param-name> password</param-name>   
        <param-value> ohWowww!!!</param-value>  
</init-param>  
<context-param>  
    <param-name>database</param-name>  
    <param-value>http://helloworld123.com</param-value>  
</context-param>

Writers and Readers VS Streams
Streams binary data -> images
Readers and Writers are usually used for characters an input stream abstraction

Sessions

  • How to identify that requests belong to the same “session”
  • What is a session?
    • Application-defined set of logical actions that have some application-specific semantic meaning
  • Server needs something unique to identify a client
    • Could use ClientIP + Browser combination
      • Problem due to things such as NAT
    • What about requests coming on a particular “persistent connection”
      • Connection socket
    • Persistency is the property of the transport mechanism
      • A persistent connection is “long-lived” Socket
    • A Session is the property of HTTP requests
    • Requests belonging to a Session may or may not use the same persistent connection
    • You may want to close a connection and still maintain the session
  • Use a unique string (session_token) to identify a client
    • Client can generate and send it to the Server…..
      • Client doesn’t care….
      • Server’s responsibility
    • Server generates a unique string and client then sends that same String in each request
    • So basically… that’s how it works…
  • Sending the session_token to the client..
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s