Recipe 17.5 Embedding Flash in a Servlet


Problem

You want to embed a Flash file in a servlet's output.

Solution

Use the javax.servlet.RequestDispatcher.include(request,response) method in the doGet( ) method of the servlet that includes the necessary HTML template text.

Discussion

The servlet can include the HTML fragment that loads the Flash movie into the page by using a RequestDispatcher . This process is similar to server-side includes in traditional Common Gateway Interface (CGI) programs. When the servlet receives a request, it includes the text fragment containing the Flash- related tags in its HTML output. This design separates the servlet itself from the tags and parameters that load the Flash movie, so that each of these entities evolves independently. For example, you can change the filename of the Flash movie or some of the object or embed parameters without recompiling the servlet code.

Example 17-7 is a servlet that uses a RequestDispatcher to include the text shown in Example 17-8. The text appears in a flash.txt file that is stored at the top level of the web application.

RequestDispatcher s typically include the output of servlets and JSPs, not just text fragments . See Chapter 6 for more detailed RequestDispatcher -related recipes.


Example 17-7. A servlet uses a RequestDispatcher to include object and embed tags
 package com.jspservletcookbook;            import javax.servlet.*; import javax.servlet.http.*; public class FlashServlet extends HttpServlet {        public void doGet(HttpServletRequest request,     HttpServletResponse response) throws ServletException,      java.io.IOException {            response.setContentType("text/html");       java.io.PrintWriter out = response.getWriter( );       out.println(       "<html><head><title>Embedded Flash content</title></head><body>");              RequestDispatcher dispatcher = request.getRequestDispatcher(       "/flash.txt");       dispatcher.include(request, response);       out.println("</body></html>");   } //doGet         public void doPost(HttpServletRequest request,       HttpServletResponse response) throws ServletException,       java.io.IOException {              doGet(request,response);        }//doPost } 

Example 17-8 shows the text fragment included by the servlet in Example 17-7.

Example 17-8. An included text fragment (flash.txt) that a servlet uses to embed Flash
 <OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase=   "http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#   version=6,0,0,0" WIDTH="550" HEIGHT="400" id="example" ALIGN="" >   <PARAM NAME=movie VALUE="/home/example.swf">    <PARAM NAME=quality VALUE=high>    <PARAM NAME=bgcolor VALUE=#FFFFFF>    <EMBED src="/home/example.swf" quality=high bgcolor=#FFFFFF  WIDTH=     "550" HEIGHT="400" NAME="example" ALIGN="" TYPE=     "application/x-shockwave-flash" PLUGINSPAGE=     "http://www.macromedia.com/go/getflashplayer">   </EMBED> </OBJECT> 

The result in a web browser looks exactly like Figure 17-3.

See Also

Chapter 6 on dynamically including content into servlets; Macromedia technical notes page: http://www.macromedia.com/support/flash/technotes.html; an article about alternative techniques to using the embed tag: http://www.macromedia.com/devnet/mx/dreamweaver/articles/flash_satay.html.



Java Servlet & JSP Cookbook
Java Servlet & JSP Cookbook
ISBN: 0596005725
EAN: 2147483647
Year: 2004
Pages: 326

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net