Web Jukebox 1.0


In this section you ll begin using your knowledge of SRF files and request handlers to put together a basic online jukebox. To create your first online jukebox, simply create a Web page that displays all the songs available in the jukebox. When a user clicks a song, he or she will be prompted to download or open the associated file. (Depending on the user s browser settings, the file may actually play automatically.)

Rather than hard-coding the Web page with your list of songs, you ll use a simple Access database (.mdb file) to store your list of songs. For each song you ll need to maintain the name of the song; the musician, band , or composer of the song; and a brief description of the song. You ll also need to store the location of the song itself.

For purposes of simplicity, you ll keep the user interface for this application very simple. You ll use an HTML table with columns for each of the fields in which you re interested and a hyperlink to the file containing the song.

Getting Started

To create the skeleton for the online jukebox, launch the ATL Server Project Wizard, and name your project WebJukebox. Select File Cache, Datasource Cache, Predefined Performance Counters, and Session State (memory backed, not database backed ) on the Server Options tab. Otherwise accept the defaults and create the application. Although you won t use all these functions in this example, you are encouraged to add the code required to use them as you continue through the book.

Start by opening the WebJukebox.srf file. You can work in either Design view or HTML view. (In the Visual Studio .NET 2003 release it is not possible to use Design View with SRF files. This functionality was removed to quick-fix some parser problems discovered very late in the ship cycle.) To begin, remove the comment tag at the beginning of the file. You should also remove This is a test: {{Hello}} from the document. If you re using Design view, you can use the Toolbox to drag and drop a table onto your page.

Create an HTML table with columns for each of your categories (name, musician/band/composer, and description). Use the Name field as a hyperlink to the file containing the song. You should end up with something that looks like this:

 <table>    <tr>      <td><b>Name</b></td>      <td><b>Musician / Band / Composer</b></td>      <td><b>Description</b></td>    </tr>  </table> 

To generate your table from your database, you can use the power of the ATL Server while statement. This power is clear when you look at the following code:

 <table>    <tr>      <td><b>Name</b></td>      <td><b>Musician / Band / Composer</b></td>      <td><b>Description</b></td>    </tr>    {{while MoreSongs}}    <tr>      <td><a href="{{Location}}">{{Name}}</a></td>      <td>{{Musician}}</td>      <td>{{Description}}</td>    </tr>    {{endwhile}}  </table> 

Can you see what you ve done here? You ve inserted a while block around a second table row. What this means is that as long as the MoreSongs method returns HTTP_SUCCESS , you ll continue creating additional blocks of HTML that match those between the while and endwhile statements. That is, you ll keep creating additional rows in the table.

What data will go into these rows? Well, there are three entries (to match your three headings). The first entry is the name of the song. You use the Name method to return the name of each song. You use the power of HTML to make this value a hyperlink, and you use the Location method to determine the location of the hyperlink (i.e., the location of the song). Similarly, the Musician and Description methods will need to return the name of the musician and a description of the song, respectively.

If you now look at the WebJukebox.h file, you ll see that it contains a lot of code comments related to the implementation of the session state and the file cache. Simply remove these comments for the time being. You can also remove the OnHello method and replace it with skeletons for each of the methods we discussed previously. This should leave you with the following request handler:

 [ request_handler("Default") ]  class CWebJukeboxHandler  {  public:       // Put public members here       HTTP_CODE ValidateAndExchange()       {            // Set the content type            m_HttpResponse.SetContentType("text/html");            return HTTP_SUCCESS;       }  protected:       [ tag_name(name="MoreSongs") ]       HTTP_CODE OnMoreSongs(void)       {            return HTTP_SUCCESS;       }        [ tag_name(name="Name") ]       HTTP_CODE OnName(void)       {            return HTTP_SUCCESS;       }        [ tag_name(name="Location") ]       HTTP_CODE OnLocation(void)       {            return HTTP_SUCCESS;       }        [ tag_name(name="Position") ]       HTTP_CODE OnPosition(void)       {            return HTTP_SUCCESS;       }        [ tag_name(name="Description") ]       HTTP_CODE OnDescription(void)       {            return HTTP_SUCCESS;       }  }; // class CWebJukeboxHandler 

Now you ll go ahead and implement each of these functions. First you need to connect to your database. To do this, you can use the OLE DB Consumer Wizard to connect to the WebJukebox.mdb file. Right-click the WebJukebox project in Solution Explorer and select Add Add Class to choose the ATL OLE DB Consumer Wizard. Open the wizard and use the Jet Provider to point to the WebJukebox.mdb file (which you can download from the Apress Web site, or create your own database using Access). Choose the Song List table and click OK twice on the dialog boxes that pop up.

The generated code will appear before you automatically. Remove the #error and the associated security warning. It s important to ensure that your database and database connections are secure. If you re using a modern database such as SQL, the best way to make it secure is to use a system such as Windows Authentication so you don t need to expose your credentials by sending them in the connection string. In this case you re storing data in a simple .mdb file, therefore you ll have all the information pass in the connection string. If we were using SQL Server or another fully featured database for this example, we would use proper Windows Authentication to protect our database, even for test code. Databases usually run as local system, and not protecting access to them is the equivalent of making normal users administrators on your machine.

If you re interested, you can examine the attributes used to make the database connection. Once you ve finished, return to WebJukebox.h and #include the generated SongList.h file.

Now you can create a public member variable in your class of type CSongList to access the data from your database. Here s the code for implementing the rest of your functions:

 [ request_handler("Default") ]  class CWebJukeboxHandler  {  public:    // Put public members here    CSongList m_songList;    HRESULT hr;    HTTP_CODE ValidateAndExchange()    {      // Set the content-type      m_HttpResponse.SetContentType("text/html");      hr = m_songList.OpenAll();      if (hr != S_OK) return HTTP_S_FALSE;      return HTTP_SUCCESS;    }  protected:    [ tag_name(name="MoreSongs") ]    HTTP_CODE OnMoreSongs(void)    {      hr = m_songList.MoveNext();      if (hr != S_OK) return HTTP_S_FALSE;      return HTTP_SUCCESS;    }     [ tag_name(name="Name") ]    HTTP_CODE OnName(void)    {      m_HttpResponse << m_songList.m_Name;      return HTTP_SUCCESS;    }     [ tag_name(name="Location") ]    HTTP_CODE OnLocation(void)    {      m_HttpResponse << m_songList.m_Location;      return HTTP_SUCCESS;    }     [ tag_name(name="Musician") ]    HTTP_CODE OnMusician(void)    {      m_HttpResponse << m_songList.m_Musician;      return HTTP_SUCCESS;    }     [ tag_name(name="Description") ]    HTTP_CODE OnDescription(void)    {      m_HttpResponse << m_songList.m_Description;      return HTTP_SUCCESS;    }  }; // class CWebJukeboxHandler 

The code is fairly self-explanatory. You initialize your database connection in the ValidateAndExchange method with the OpenAll call. Your OnMoreSongs method moves through the database entries and returns HTTP_SUCCESS as long as there s an entry to output. The outputting for each value is done by the other methods, which simply put information from the database into the HTML stream.

There you have it, your first ATL Server application!




ATL Server. High Performance C++ on. NET
Observing the User Experience: A Practitioners Guide to User Research
ISBN: B006Z372QQ
EAN: 2147483647
Year: 2002
Pages: 181

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net