[ Team LiB ] Previous Section Next Section

Recipe 17.5 Embedding Flash in a Servlet


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


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


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.

RequestDispatchers 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 {

      java.io.PrintWriter out = response.getWriter( );
      "<html><head><title>Embedded Flash content</title></head><body>");
      RequestDispatcher dispatcher = request.getRequestDispatcher(

      dispatcher.include(request, response);

  } //doGet
  public void doPost(HttpServletRequest request, 
     HttpServletResponse response) throws ServletException, 
     java.io.IOException {

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=
  version=6,0,0,0" WIDTH="550" HEIGHT="400" id="example" ALIGN=""

  <PARAM NAME=movie VALUE="/home/example.swf"> 
  <PARAM NAME=quality VALUE=high> 

  <EMBED src="/home/example.swf" quality=high bgcolor=#FFFFFF  WIDTH=
    "550" HEIGHT="400" NAME="example" ALIGN="" TYPE=
    "application/x-shockwave-flash" PLUGINSPAGE=


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.

    [ Team LiB ] Previous Section Next Section