95-702 Organizational Communications And Distributed Object Technologies Homework 1 Due Midnight on Tuesday, September 18, 2007 (1) Using Eclipse and Tomcat, write a JSP page and a servlet that uses cookies to allow a user to vote for one of three candidates but also determines if an individual has voted for president more than once. The initial page will ask the user to vote for Edwards, Clinton, or Obama. After the user selects a candidate execute a servlet that writes the user's choice back to the browser and to a cookie. If the user votes a second time, from the same browser with cookies turned on, reject the new vote and tell the user who they already voted for. This is not actually secure but it allows us to explore the use of cookies. (2) Add a shared object (singleton) to the system above and use it to collect a vote tally. A second report page should be available to a voting administrator with user name "admin" and password "sesame". Anyone with this username and password will be able to collect the vote tally for each candidate at any point in time. Perform your username and password check within your servlet. (Tomcat can also be configured to do this but here we will do it ourselves.) In going from question 1 to question 2, you will need to modify the web.xml file. You may have to restart Tomcat to have the new web.xml file accepted. The modification to the web.xml file needs to be done correctly. Be sure to study some existing web.xml files before you do this. Plenty of examples exist on the internet. Question 3 is designed so that you get a chance to review some of what we have done in the previous questions. Please resist the temptation to simply make modifications to the servlet we worked on above. Take some time and build a new project from scratch. 3) Write a new web application (a servlet called ReadRSS.java with a new index.jsp file) that allows the user to enter the name of an RSS 1.0 feed (URL) in a browser. Your servlet will then read the RSS 1.0 feed and send it back to the browser as text/xml. There is no need to parse the XML with a parser. We will do that in the next question. You may assume that your user is friendly and that the URL is correct. Paste at least three screen shots showing the browser displaying different user selected RSS documents. In order to read the RSS feed, you may use the following Java code from within the servlet: URL u = new URL(RSSName); URLConnection uc = u.openConnection(); InputStream raw = uc.getInputStream(); InputStream buffer = new BufferedInputStream(raw); Reader r = new InputStreamReader(buffer); int c; while((c = r.read()) != -1) { fromRSS = fromRSS + ((char)c); } The RSSName string will come from the browser’s http request. The browser must prompt the user for an RSS 1.0 URL rather than a name of a person (as we did in the previous questions). The browser will simply display this as text and it will not look very good. Below are a few RSS 1.0 feeds that you might try: http://www.w3.org/2000/08/w3c-synd/home.rss http://weblogs.macromedia.com/dev_center/index.rdf http://dublincore.org/news.rss http://www.networkworld.com/rss/soa.xml 4) In question 3 we are reading the RSS 1.0 feed and displaying it to the browser as raw XML. Many web sites are beginning to describe their content using RSS but raw XML is not typically what *browsers* like to display. For this question you will write a web application that allows a user to register her interest in a set of several RSS feeds. After the registration step, the user will be able to visit the web application and receive a list of news items and links (pointing to those items) on her browser. Many users (with different email addresses) may use the same application concurrently. All the data sent to the browser for display will be in XHTML. If the user enters a URL that does not actually address an RSS feed then the program will report that fact to the user without crashing. Required files: Register.jsp When Register.jsp is accessed it displays an HTML form on the browser. The form allows the user to enter a complete email address and a URL of an RSS 1.0 news feed. When the submit button is hit, Register.jsp sends an HTTP request to a servlet called SetRSS.java. This servlet collects the email address and the URL and places the pair in a shared object called UserLists.java. UserLists.java is a modified version of the VisitTracker.java program we studied in class. UserLists holds a hash table of linked lists. Each hash table cell holds a list of URL’s that a particular user is interested in. The hash table is indexed with an email address. SetRSS.java SetRSS is a servlet that is called by Register.jsp. It reads the URL and email address and passes both of these data items to UserLists.java. It then makes a request on UserLists for a list of all URL’s that the email address is associated with. SetRSS collects this list of RSS URL’s and sends them back to the browser. UserLists.java UserLists.java is a singleton. There is only one object of this class and can be used for servlet collaboration. It makes use of the java.util package. This package contains various collection classes. The UserLists object needs to associate a list of RSS 1.0 URL’s with each registered email address. The number of email addresses may grow over time and so may the number of URL’s associated with each email address. Access.jsp When Access.jsp is accessed it displays an HTML form on the browser. The form allows the user to enter an email address. When the submit button is hit, Access.jsp sends an HTTP request to a servlet called ReadRSS.java. ReadRSS.java The ReadRSS servlet collects the email address from the HTTP request and passes it to the UserLists object. The UserLists object returns a list of RSS URL’s associated with the email address. ReadRSS iterates over each URL and creates an InputSource object for each. The InputSource objects are passed, one at a time, to the RSSHandler class. ReadRSS reads the results made available by RSSHandler. ReadRSS returns an HTML document to the browser. This document contains a set of anchor elements. Each anchor element has an attribute containing a URL. Each anchor element has the title of the news item as its content. So, the user sees a list of news items with links. The source of the RSS feed and the current date and time will be displayed around each list of news feed items. RSSHandler.java The RSSHandler class is a SAX handler and it extends the DefaultHandler class. Its constructor takes an InputSource object as a parameter. The class handles SAX events from an RSS news feed. As a simplification, the RSSHandler class looks for any element containing a and a <link> element. It assumes that a <title> and a <link> element will always be found within an <item> element. It does not assume that <link> and <title> are in any particular order. Nor does it assume that there are no additional children of the <item> element. It ignores all elements that are not found within an <item> element. To model the behavior of the SAX handler you might consider using a finite state machine as discussed in class. For each RSS feed, the RSSHandler class makes available a list of title and link pairs. RSSTitleAndLink.java This is a very simple class that holds one title and link pair as two Java String objects. If you write your solution without this file that is fine.