FileDocCategorySizeDatePackage
MoviesServlet.javaAPI DocExample9005Sun Jul 07 09:55:28 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
private File
xmlFile
File to unmarshal and marshal to
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 if (action.equalsIgnoreCase("addMovie")) {
            out.write(" ***** Adding new movie ***** ");
            String movieTitle = req.getParameterValues("title")[0];
            String movieDirector = req.getParameterValues("director")[0];
            
            Movie movie = new Movie();
            movie.setTitle(movieTitle);
            movie.setDirector(movieDirector);
            movie.setCast(new Cast());
            movies.getMovie().add(movie);
            
            // Marshal back to XML
            try {
                movies.validate();
                movies.marshal(new FileOutputStream(xmlFile));
            } catch (StructureValidationException e) {
                out.write("Validation error: " + e.getMessage());
                e.printStackTrace(out);
            }
        } else if (action.equalsIgnoreCase("addActor")) {
            out.write(" ***** Adding new actor ***** ");
            String movieName = req.getParameterValues("title")[0];
            String actorName = req.getParameterValues("actor")[0];
            String headliner = req.getParameterValues("headliner")[0];
            
            List movieList = movies.getMovie();
            for (Iterator i = movieList.iterator(); i.hasNext(); ) {
                Movie movie = (Movie)i.next();
                // See if this is the specified movie
                if (movie.getTitle().equalsIgnoreCase(movieName)) {
                    Cast cast = movie.getCast();
                    Actor actor = new Actor();
                    actor.setContent(actorName);
                    if (headliner.equalsIgnoreCase("true")) {
                        actor.setHeadliner("true");
                    } else {
                        actor.setHeadliner("false");
                    }
                    cast.getActor().add(actor);
                }
            }
            
            // Marshal back to XML
            try {
                movies.validate();
                movies.marshal(new FileOutputStream(xmlFile));
            } catch (StructureValidationException e) {
                out.write("Validation error: " + e.getMessage());
                e.printStackTrace(out);
            }
        } 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
            xmlFile = new File(MOVIES_XML_DOCUMENT);
            FileInputStream inputStream = new FileInputStream(xmlFile);

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