MSU CS351 Fall 2007

 
 
 
  • About

    Course blog for CS351
 
Thursday, Dec 6 December 6th, 2007

There will be a quiz today, followed by a short discussion about Regular Expressions.  Probably the sanest discussion on this topic I’ve come across can be found here.

November 29th, 2007

I committed build 351, do I get a cookie?

Jason

Ajax, Thursday, Nov 29 November 27th, 2007

Ajax has become the fastest growing area of web development in the last couple of years. Before we go into what it is and how it works, take a look at Google Suggest. Notice that when you type a term to search for, the results list changes on-the-fly. This type of behavior is what Ajax is all about.

Asynchronous Javascript And XML. That’s what Ajax is supposed to stand for, but it actually encompasses more technologies than those two: XML, Javascript, HTML and CSS in all.

Ajax isn’t a language. It isn’t even a standard. Think of Ajax as a way of doing things - it’s a methodology - a way of hooking things together to improve the user experience on the web. Ajax is all about the user experience, period.

Browsers and Javascript

All modern web browsers construct a document tree when they receive HTML from a server. This tree is known as the DOM (document object model) - just a fancy acronym for your basic boring computer science tree data structure. Another thing all modern web browsers have in common is that they implement Javascript in a fairly standard way (fairly, but annoyingly not so standard in some ways). One of the things Javascript can do is to access the browser’s DOM - this means they can find a specific node and change something about that node. For instance, within a page of HTML, let’s say there is the following <div> tag:

<div id=”junglelove”>Tarzan loves Jane</div>

Javascript is able to search and find a <div> with the attribute id=”junglelove”, and then replace the text to something like: “Jane loves Tarzan”. This seems pretty straight forward, right? The key idea here is that this Javascript can be invoked from all kinds of events - not just on a page load. You could switch the text every time the user moves the mouse over the <div>. You could replace the text with something that came in from a web server … and do so without refreshing the web page - hence… the asynchronous aspect of Ajax.

Javascript has pretty much full control over a browser’s HTML document. Saying exactly the same thing - JS has pretty much full control over the DOM. You can add tags/nodes - change attributes - replace tags/nodes - etc. When you were looking at Google Suggest (you did go and check this out, right?!?), every key press event (yep, that’s one of the events Javascript can respond to) sends a message to the Google server requesting a new set of results - when those results come back - it happens very quickly, less than a second - the Javascript running on the browser uses that response to modify the current HTML - adding the new results that came back from Google. This all happens “under the hood”, and it all happens asynchronously. Pretty cool, eh?

This is Ajax in a nutshell. Exactly how this happens is just some Javascript code and some corresponding web server code. It really isn’t that hard nor complicated.

Ajax in Practice

First, you create a request object in Javascript - remember this is happening on the browser side of things:

XMLHttpRequest();

This returns a request object that you add some information to. The main thing you’re going to add is a function. This function doesn’t get called, yet. The purpose of this function is to respond to something that the web server is going to send back. So, after some other housekeeping stuff, you tell Javascript to send off this request to the web server. The browser doesn’t wait for the server to respond, it just goes along doing it’s browser thing - displaying HTML and sitting idle waiting for you to do something.

Meanwhile, on the web server side of life, it receives the request that the Javascript fired off. It does it’s server thing, and after processing the request, it sends the response back to browser.

Back on the browser - there it was minding it’s own business, waiting for you to click something, and in comes the response from the web server. The browser checks with Javascript to see if it cares about this response, and because you registered a function to process this particular response, Javascript raises its hand and accepts the response. It then passes the response to your function, and that’s where you do your magic in the DOM, or do whatever you had in mind.

This roundtrip communication behind the scene is the power behind Ajax. In class, we’ll draw some pretty (or not) diagrams of how this all works, but if you can understand the theory here, you’ll be well along the way.

Wrapping it up

It is very likely that a number of you will be working with Ajax as part of a job, perhaps sooner than later. Gaining a solid grasp on Ajax is important, so if you need to, read this post two or three times. Ajax really isn’t that complicated once you get it. There are a ton of resources on the web around Ajax. There’s even a Head First Ajax book! It’s a great introduction to Ajax. The best book I’ve found so far is “Ajax Design Patterns”, by Michael Mahemoff. It is jammed full of real-world examples and information about how the “big guys” do Ajax.

XPATH notes, Tuesday, Nov 27 November 27th, 2007

XPath is a W3C standard and is one of the most important core technologies in the XML world. It allows you to navigate through XML documents selecting nodes or node sets using a simple format.

XPath uses path expressions to select nodes or node-sets in an XML document. These path expressions look very much like the expressions you see when you work with a traditional computer file system.

Selecting nodes:

Expression Description
nodename Selects all child nodes of the named node
/ Selects from the root node
// Selects nodes in the document from the current node that match the selection no matter where they are
. Selects the current node
.. Selects the parent of the current node
@ Selects attribute

Here’s a sample xml document:

<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
<book>
  <title lang="en">Harry Potter</title>
  <author>J K. Rowling</author>
  <year>2005</year>
  <price>29.99</price>
</book>
</bookstore>

XPath expressions:

Path Expression Result
bookstore Selects all the child nodes of the bookstore element
/bookstore Selects the root element bookstoreNote: If the path starts with a slash ( / ) it always represents an absolute path to an element!
bookstore/book Selects all book elements that are children of bookstore
//book Selects all book elements no matter where they are in the document
bookstore//book Selects all book elements that are descendant of the bookstore element, no matter where they are under the bookstore element
//@lang Selects all attributes that are named lang

Predicates

Predicates are used to find a specific node or a node that contains a specific value.

Predicates are always embedded in square brackets.

Path Expression Result
/bookstore/book[0] Selects the first book element that is the child of the bookstore element.Note: IE5 and later has implemented that [0] should be the first node, but according to the W3C standard it should have been [1]!!
/bookstore/book[last()] Selects the last book element that is the child of the bookstore element
/bookstore/book[last()-1] Selects the last but one book element that is the child of the bookstore element
/bookstore/book[position()<3] Selects the first two book elements that are children of the bookstore element
//title[@lang] Selects all the title elements that have an attribute named lang
//title[@lang=’eng’] Selects all the title elements that have an attribute named lang with a value of ‘eng’
/bookstore/book[price>35.00] Selects all the book elements of the bookstore element that have a price element with a value greater than 35.00
/bookstore/book[price>35.00]/title Selects all the title elements of the book elements of the bookstore element that have a price element with a value greater than 35.00

Wildcards

XPath wildcards can be used to select unknown XML elements.

Wildcard Description
* Matches any element node
@* Matches any attribute node
node() Matches any node of any kind

Examples

In the table below we have listed some path expressions and the result of the expressions:

Path Expression Result
/bookstore/* Selects all the child nodes of the bookstore element
//* Selects all elements in the document
//title[@*] Selects all title elements which have any attribute

This is only the basics of XPath. There are over 100 functions that you can use in specifying XPath expressions. You can combine multiple XPath expressions using boolean operations. The list goes on. See the W3C documentation on XPath for a complete spec.

Thurs 11/15 and Tues 11/20 November 15th, 2007

Thursday’s Lecture was on PHP. Here is an excellent site for learning PHP:

http://www.keithjbrown.co.uk/vworks/php/default.php

There will be a quiz on Threads, Client-Server and PHP on Tuesday.

Tuesday’s lecture will be on MySQL and how PHP interfaces to MySQL. The above link also has a tutorial on MySQL. Read both tutorials by Tuesday.

Builds, Blame, and Shame November 14th, 2007

The Gods are Pleased In what may be a first, the cs351 project builds cleanly at revision 314. I cleaned up some cruft, wrote an Ant build script, and sacrificed a goat. All is now well as far as the java compiler gods are concerned.

That’s the good news. The bad news is that there was cruft. Nasty cruft. Which leads me to believe that some people don’t compile their code. Ever. I’m not talking about missing libraries. No, I’m talking about syntax so horrible it could never, ever have compiled.

Can you feel my pain?

You know who you are. I know who you are. It’s called svn blame /some/brokenfile.java. I can see your name beside that line of horribly broken code.

But it’s ok. I get it. Compiling is hard. It takes too much time. We all have busy schedules. Well fear not! I can make the server compile your code for you. When you check it in. And then publicly shame you when the build breaks. Embrace the shame. Enter continuous integration!

Yes, through the miracle of a working build script (ant build, ant jar, ant run, …) the server now watches for changes, checks out the new code, and builds it automatically. And yes, if it doesn’t compile properly it emails everyone in the class.

Still can’t believe it!? Go to http://acg01.msu.montana.edu:3333/ to see the build history and the compile errors if the build failed. Which in combination with “svn blame” lets me know who to beat with a stick.

–Ryan

Who broke the build!?

Tuesday Nov 13 Topics November 8th, 2007

Tuesday will be project code review day.   If you can’t make class, email me, else I’ll expect everyone there.  It’s going to be a session you cannot afford to miss.

Tuesday’s Topics:

Project Code Review

Project Goals

Makefiles  — read the following link

http://mrbook.org/tutorials/make/

Thursday 11/8 Topic November 7th, 2007

On Tuesday, we talked about threads and mutexes.

On Thursday, we’re going to start diving into client server technology and how the Web works from the Apache Web Server to the HTTP protocol all the way down to TCP/IP packets. This will be the first of two talks about Web Technologies and Client Server Design. We’ll also see how threads help implement client server design.

Java 3D October 25th, 2007

To run any of the Render Team’s code, you will need to install the Java3D packages on your computer.

Java 3D API Download

Mid-Term Topics October 22nd, 2007

You should be familiar with the following subjects for the Mid-Term:

  • Software Requirements
  • Functional Design / Technical Design / UML Diagrams
  • Software Scheduling
  • Subversion / Work Flow with Source Code Revision software
  • XML
  • Software Testing / JUnit
  • Four Design Patterns:
  •    Observer
  •    Decorator
  •    Factory
  •    Adapter
  • Development Teams