Tutorials : Struts meets Swing (1) :

Testing the Swing jsp-pages

Apart from an error page the DVD application only uses the swinglist.jsp and swingdetail.jsp pages, so let's test these two jsp-pages with a small test program called DemoRequest.

It contains a method called request that can send a request to a servlet, and then write out the response to System.out. Note how request sets the user-agent field to "SWING". If we call request like this:

DemoRequest dr = new DemoRequest("http://localhost:8080/dvdapp2/list.do");
    dr.request();

we get this output:

<collection>
      <dvd id="C">
        <title>Amadeus</title>
        <length>158</length>
        <actor>F. Murray Abraham</actor>
        <actor>Tom Hulce</actor>
        <actor>Elizabeth Berridge</actor>
      </dvd>
      <dvd id="A">
        <title>Lord of the Rings: The Fellowship of the Ring</title>
        <length>178</length>
        <actor>Ian Holm</actor>
        <actor>Elijah Wood</actor>
        <actor>Ian McKellen</actor>
      </dvd>
      <dvd id="B">
        <title>The Matrix</title>
        <length>136</length>
        <actor>Keanu Reeves</actor>
        <actor>Laurence Fishburne</actor>
      </dvd>
    </collection>

- Figure 10: The list of DVDs in XML format -

If you compare it to Figure 3 you'll see that they match nicely. Let's follow our luck and make a succeeding request for the details of the first DVD (index=0):

DemoRequest dr = new DemoRequest("http://localhost:8080/dvdapp2/list.do");
    dr.request();  
    dr = new DemoRequest("http://localhost:8080/dvdapp2/detail.do");
    dr.setData("index=0");  
    dr.request(); 

The result is not what we expected. First we correctly get the list of DVDS, but then this output follows:

<html>
    <head>
    <title>System Error</title>
    </head>
    <body>
    <h2>System Error</h2>
    
    No DVD information available
    
    </body>
    </html>

This is the output from the error.jsp page (which is in HTML-format, but it's easily changed to XML), and it tells us that the Struts Action class for the Detail Page didn't find the list of DVDs in session scope (you may look in the DetailDVDAction class in the zip file to see the code for this).

Maintaining the Session

This is the kind of error you may spend hours to locate. Or you may see the solution right away. Here is the cause of the problem.

What we often forget--since it's transparent to a browser user--is that when we run a servlet application on a web server, then the session "state" for each user is handled through a cookie, which uniquely identifies each user or session. The first time we contact the application (as we do by requesting list.do) a cookie is sent back to us. When we make the next request, it's our turn to send this cookie back to the server, so it can identify the session. We haven't done this yet in the test program, so now we must modify the code so it stores the cookie that is returned, and sends it back on subsequent requests. A new and updated class, DemoRequestCookie, can be found here.

The new requests go like this:

DemoRequestCookie dr = 
       new DemoRequestCookie("http://localhost:8080/dvdapp2/list.do");
    dr.request();  
    String cookie = dr.getCookie();
    dr = new DemoRequestCookie("http://localhost:8080/dvdapp2/detail.do");
    dr.setData("index=0");  
    dr.setCookie(cookie);
    dr.request();  

This time the output is as expected:

. . . (list of all DVDs) . . .
    <dvd id="C">
        <title>Amadeus</title>
        <length>158</length>
        <actor>F. Murray Abraham</actor>
        <actor>Tom Hulce</actor>
        <actor>Elizabeth Berridge</actor>
    </dvd>

How to Add Java Applets to Your Site

New on the Java Boutique:

New Review:

Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling API boasts simplicity, ease-of-integration, a well-rounded feature set, and it's free!

New Applet:

Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA sequences into three useful formats.

Elsewhere on internet.com:

WebDeveloper Java
Lots of Java information on webdeveloper.com

WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.

ScriptSearch Java
Hundreds of free Java code files to download.

jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.