FileDocCategorySizeDatePackage
MoviesServlet.javaAPI DocExample6542Sun Jul 07 09:53:00 BST 2002javajaxb

MoviesServlet

public class MoviesServlet extends HttpServlet

Fields Summary
private Movies
movies
The Movies database object
private String
errorMessage
Any error that occurred.
private static final String
MOVIES_XML_DOCUMENT
The XML document storing the movie database
Constructors Summary
Methods Summary
public voiddoGet(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse res)


        // Handle any error conditions that might have occurred.
        if (movies == null) {
            error(res);
        }

        // Get output stream
        PrintWriter out = res.getWriter();
        res.setContentType("text/html");

        // Write out movie database
        out.println("<HTML><HEAD><TITLE>Movie Database</TITLE></HEAD>");
        out.println("<BODY>");
        out.println("<H2 ALIGN='center'>Movie Database</H2>");
 
        List movieList = movies.getMovie();
        for (Iterator i = movieList.iterator(); i.hasNext(); ) {
            Movie movie = (Movie)i.next();

            // Title
            out.print("<B><FONT SIZE='+1'>");
            out.print(movie.getTitle());
            out.println("</FONT></B><BR />");

            // Director
            String director = movie.getDirector();
            if (director != null) {
                out.print("Director: ");
                out.print(director);
                out.println("<BR />");
            }

            // Producer
            out.println("Producers:<BR /><UL>");
            List producerList = movie.getProducer();
            for (Iterator j = producerList.iterator(); j.hasNext(); ) {
                out.print("<LI>");
                out.print((String)j.next());
                out.println("</LI>");
            }
            out.println("</UL>");

            // Cast
            out.println("Starring:<BR /><UL>");
            Cast cast = movie.getCast();
            List actorList = cast.getActor();
            for (Iterator j = actorList.iterator(); j.hasNext(); ) {
                Actor actor = (Actor)j.next();
                out.print("<LI>");
                out.print(actor.getContent());
                if (actor.getHeadliner().equalsIgnoreCase("true")) {
                    out.print(" (Headliner)");
                }
                out.println("</LI>");
            }
            out.println("</UL>");

            out.println("<HR WIDTH='80%' />");
        }

        out.println("</BODY></HTML>");
        
        out.close();
    
public voiddoPost(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse res)


        // Get action paramater; default is "list"
        String[] actionValues = req.getParameterValues("action");
        String action = null;
        if ((actionValues == null) || (actionValues[0] == null)) {
            action = "list";
        } else {
            action = actionValues[0];
        }

        // Handle different actions
        PrintWriter out = res.getWriter();
        res.setContentType("text/plain");

        /* **** List current movies **** */        
        if (action.equalsIgnoreCase("list")) {

            out.write(" ***** Movies Database *****\n\n");

            // Print out each movie
            List movieList = movies.getMovie();
            for (Iterator i = movieList.iterator(); i.hasNext(); ) {
                Movie movie = (Movie)i.next();

                // Title
                out.print(" Movie: ");
                out.println(movie.getTitle());

                // Director
                String director = movie.getDirector();
                if (director != null) {
                    out.print("   Director: ");
                    out.println(director);
                    out.println();
                }

                // Producer
                out.println("   Producers:");
                List producerList = movie.getProducer();
                for (Iterator j = producerList.iterator(); j.hasNext(); ) {
                    out.print("     * ");
                    out.print((String)j.next());
                    out.println();
                }
                out.println();

                // Cast
                out.println("   Starring:");
                Cast cast = movie.getCast();
                List actorList = cast.getActor();
                for (Iterator j = actorList.iterator(); j.hasNext(); ) {
                    Actor actor = (Actor)j.next();
                    out.print("     * ");
                    out.print(actor.getContent());
                    if (actor.getHeadliner().equalsIgnoreCase("true")) {
                        out.print(" (Headliner)");
                    }
                    out.println();
                }

                out.println(" -------------------------------- ");
            }
        } else {
            out.write("The action supplied, '");
            out.write(action);
            out.write("', is not currently supported.\n");
        }
        out.close();
    
private voiderror(javax.servlet.http.HttpServletResponse res)

        PrintWriter out = res.getWriter();
        res.setContentType("text/plain");

        out.write(" ************* ERROR OCCURRED ***************\n\n");
        out.write("Error: " + errorMessage + "\n");
        out.close();
    
public voidinit(javax.servlet.ServletConfig config)


          
        super.init(config);

        // Load the database using JAXB
        try {
            // Load the XML
            File xmlFile = new File(MOVIES_XML_DOCUMENT);
            FileInputStream inputStream = new FileInputStream(xmlFile);

            // Unmarshal
            movies = Movies.unmarshal(inputStream);
        } catch (Exception e) {
            errorMessage = e.getMessage();
        }