The NowPlayingServlet
The HTTP Post processing logic of your setup lies in NowPlayingServlet.java, which you can download in the Web Application Archive (WAR) associated with this article here. I suggest that you take some time to analyze the code before moving on. Don't worry, though—I'll dissect the code piece by piece. For simplicity, I process only the title and the artist of the current song; the procedure for processing other current song information and previously played song information will be obvious once you've seen this example.
To store information about the songs in the servlet, I use local variables that are created at the first invocation of the servlet:
String currentSong = ""; String artist = "";
The Now Playing Plug-in invokes the doPost method when it performs an HTTP Post. The doPost method of the servlet, shown below, extracts the title and artist of the current song playing, as informed by the Now Playing Plug-in through HTTP Post parameters, which you get using a getParameter call from your HttpServletRequest object.
public void doPost( HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { currentSongTitle = req.getParameter("Title1"); currentSongArtist = req.getParameter("Artist1"); System.out.println( "New Track Discovered: " + currentSongTitle + " by " + currentSongArtist); }
Notice that I pulled the request parameters Title1 and Artist1. If you wanted to access the history of songs played, you could change the number of the request parameter. For example, Title2 would get you the title of the song that was last played, and Artist3 would get you the artist name of the song that was played two songs ago. I went ahead and used a System.out call for troubleshooting purposes so you can analyze your server logs to see if the Now Playing Plug-In is truly posting to the NowPlayingServlet.
The remaining part of the NowPlayingServlet is the doGet method:
public void doGet( HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { res.setContentType("text/html"); PrintWriter out = res.getWriter(); out.println("<html>"); out.println( "<META HTTP-EQUIV='Refresh' CONTENT='3;[ccc] URL=http://localhost:9080/WinampProject/NowPlayingServlet'>"); out.println("<body>"); out.println("Now Playing: "); out.println(currentSongTitle); out.println(" by: " + currentSongArtist); out.println("</body></html>"); out.close(); }
Whereas the Winamp NowPlayingServlet posts to the NowPlayingServlet that ends up calling the doPost method, visitors of your website will invoke the NowPlayingServlet's doGet method, which will report to them information about the current song playing via an HTTP response. The code above creates an HTML response that is sent back to the client, reporting the current song title as well as current song's artist information.
Notice the META HTTP-EQUIV='REFRESH' call to the URL http://localhost:9080/WinampProject/NowPlayingServlet. This URL must be modified in your code to point to the eventual endpoint of your servlet. Of course, for the outside world to see the servlet, the localhost would have to be replaced with your domain name/IP address. This META tag is used so that a client visiting your website can have his browser automatically refresh after a period of 3 seconds. Of course, you can change this value to meet your preference.
In addition to the NowPlayingServlet, my Now Playing setup requires a couple auxiliary HTML files. One of them is the HTML file that clients will access via their browser. I have included this in the project zip file as index.html. The code of the HTML file is shown below:
<FRAMESET BORDER=0 ROWS="50,*"> <FRAME SRC="http://localhost:9080/WinampProject/NowPlayingServlet" [ccc] NAME="nowPlayingFrame" BORDER=0 SCROLLING=0> <FRAME SRC="http://localhost:9080/WinampProject/content.html" [ccc] NAME="contentFrame" BORDER=0 SCROLLING=0> </FRAMESET>
Notice that the HTML file contains a couple of frames. One of the frames points to the NowPlayingServlet. As before, you will need to change the URL of the servlet to correspond to the external URL of your servlet as the outside world will know it.
The second frame points to an HTML file named content.html, which, in short, is there only to show you where you can place your web content. The code of content.html is shown below:
<HTML> <HEAD> <TITLE>content.html</TITLE> </HEAD> <BODY> <HR SIZE=1> <P>My page content would go here.</P> </BODY> </HTML>
The beauty of our framed setup allows for the frame named nowPlayingFrame to be updated every 3 seconds, as mandated by the META refresh tag we planted in the servlet's HTML output.
When you have the NowPlayingServlet deployed and two HTML files deployed to your servlet container and visible to the outside world, you are ready to test things.
After deploying to the web server, you should see communication between the plug-in and the NowPlayingServlet. When a song stops playing in Winamp and the next song is loaded from a playlist, you should see a small pop-up like the one in Figure 5.
Figure 5 The Now Playing Plug-in posting information.
If you set things up properly, you should see a message in your web server's console stating that a new track was discovered after each song in your playlist ends (see Figure 6).
Figure 6 Analyzing your console for new track discovery by the NowPlaying servlet.
At this point, you are ready to let your clients view your work. Your web visitors simply need to visit the index.html page on your web application server. In Figure 7, the page http://localhost:9080/WinampProject/index.html is being visited. Of course, this URL will need to be updated with external URL of the index.html page. The client browser will call the NowPlaying servlet every 3 seconds to keep up-to-date with what the Winamp player is playing.
Figure 7 Client browser updated with track information.
Now you can use the Now Playing Plug-In to produce HTTP posts that can be consumed by a servlet container. The information contained in the payload of the HTTP Post contains information such as the title and artist of the current track playing, as well as previous tracks that have played. You can access a plethora of other information from the HTTP Post payloads of the Now Playing Plug-in. To learn more, see http://www.cc.jyu.fi/~ltnevala/nowplaying/instruction.html#post.
You can use the knowledge you gained in this article in tandem with my Winamp streaming article to let your website visitors keep up with what song you are currently playing on your Winamp-based Internet broadcast.