站内搜索: 请输入搜索关键词
当前页面: 图书首页 > JavaServer Pages, Second Edition

JavaServer Pages, Second Edition

[ directory ] Previous Section Next Section

7.5 Advertising

Money does not really make the world go around; gravity and angular momentum take care of that quite nicely. However, money can keep a Web site running, which at times may seem almost as important. One of the most time-tested ways for a Web site to make money is to sell space on each page to advertisers.

This is not fundamentally at odds with a usercentric site, such as Java News Today. No one enjoys the endless repetition of ads for unwanted items or constant plugs to buy shoddy or uninteresting goods. However, the Web can make shopping very easy and convenient, and an advertisement for an item a user would like but did not know about is a win for the user, the vendor, and the Web site.

The secret here is to show users only items that might appeal to them and to filter out all the advertising "noise" that most people find so irritating. In other words, the key is personalization, just as it is with content. By customizing the ads to the user, users will not be bothered with irrelevant advertising, and advertisers are generally willing to pay much more to ensure that their ads are seen only by people who might buy their products. Again, everybody wins.

Because personalization will be the driving force behind JNT's ads, it should not be surprising that ads will also be stored in the database. Once again, this means that the first step will be to design the tables by considering what information needs to be stored.

The first and most obvious element is the text of each ad. In order to match ads with users, the ads will need to be weighted according to relevant keywords, so an auxiliary table mapping ad IDs to keyword IDs will be needed. This will work in much the same way that keywords were associated with articles. Finally, most ads are sold based on a number of impressions; in other words, an advertiser may pay a certain amount to ensure that the ad is seen a certain number of times. The database will thus need to store the number of impressions sold, and the bean will need to decrement this count each time the ad is viewed and remove it from the system when the count reaches 0. The new tables are shown in Listing 7.9.

Listing 7.9 The advertising tables
create table ad (
        ad_id           int,
        impressions     int,
        text            varchar(4096)
);

create table ad_keywords (
        ad_id           int,
        keyword_id      int
);

Because ads are marked with keywords, just as articles are, it is possible to use the scoring mechanism that was developed for articles to compute a score for each ad. Rather then show this score directly to the user, it can be used by a new AdManagerBean. This bean will compute a score for every ad in the system and randomly return an ad from among the ten with the highest score. This ad will be placed in the header, which is shown in Listing 7.10.

Listing 7.10 The header, with an ad
<%@ taglib prefix="c"
    uri="http://java.sun.com/jstl/core" %>

<jsp:useBean id="user7"
 class="com.awl.jspbook.ch07.UserInfoBean"
 scope="session"/>

<jsp:useBean
  id="adManager7"
  class="com.awl.jspbook.ch07.AdManagerBean"
  scope="session"/>

<center>
  <h2>
    Java News Today: <c:out value="${param.title}"/>
  </h2>
</center>

<c:if test="${user7.isLoggedIn}">
  <div class="left">
    Hello <c:out value="${user7.name}"/>!
  </div>
</c:if>

Note that the AdManagerBean is stored in the session. The process of computing the score may be somewhat time-consuming, and because the scores will not change much while a user is on the site, the score does not need to be recomputed on every page.

The implication of this is that when the user logs in, the AdManagerBean must be told who the user is in order to compute the scores, just as the EditionBean needed this information to select the correct sections. This is done with another little addition to the login handler page:

<c:set name="ads.userId" value="${user.userId}"/>

With such an ad in the header, the new index page will look like the one in Figure 7.4.

Figure 7.4. The new index page.

graphics/07fig04.gif

A slight variation to this scheme is worth mentioning. Instead of asking the user to specify manually which keywords are of interest, this information could be collected automatically. Every time a user reads an article, it would be possible to track that article's keywords and so over time build up a record of the user's behavior on the site. This profile could then be used to select advertisements using essentially the same AdManagerBean. Although there may be ethical concerns about the collection of information without a user's knowledge or participation, there is no technical barrier to doing so.

    [ directory ] Previous Section Next Section