Recipe 17.7 Embedding an SVG File in a JSP


You want to display a Scalable Vector Graphics (SVG) image inside a JSP.


Use the embed HTML element to position the SVG in the JSP.


Developers typically use the embed tag to place an SVG file in an HTML file or JSP. SVG is an XML-based graphics technology that provides developers and designers leverage in producing and displaying interactive graphics.

Browsers use special SVG viewer applications to handle the embedded SVG files. Adobe System's SVG Viewer application can be downloaded from Corel's SVG Viewer can be downloaded from

Example 17-10 embeds an SVG file named testLogo.svg and points the user to the Adobe SVG Viewer download site if they have not installed an SVG Viewer application.

SVG files have extensions of either .svg or (in compressed form) .svgz , even though they are XML files.

Example 17-10. An SVG graphics file embedded in a JSP
 <%@ taglib uri="" prefix="c" %> <jsp:useBean id="date" class="java.util.Date" /> <html> <head><title>SVG in a JSP</title></head> <body> <h2>A Scalable Vector Graphics example</h2> <embed src=     '<c:out value="${param.svg_source}"/>.svg' width=     "200" height="200" type="image/svg-xml" pluginspage=     "" > <br /><c:out value="${date}"/> </body> </html> 

Example 17-10 shows how to place an SVG within other JSP code elements, such as the taglib directive and the jsp:useBean standard action. Example 17-10 also dynamically loads an SVG based on the request parameter named svg_source . The code uses the JSTL c:out tag and the EL's param implicit object to output the parameter value (see Chapter 23 on the JSTL).

Figure 17-5 shows the result of requesting the JSP in Example 17-10, including the name of the SVG file as a request parameter. The request URL looks like:

Figure 17-5. A JSP page shows an SVG graphics file

The SVG shown in Figure 17-5 is derived from Adobe Systems Inc., which creates the Adobe SVG Viewer and an SVG-enabled graphics application, Adobe Illustrator.

See Also

SVG specifications at the W3 Consortium:; Adobe's SVG Viewer install page:; Recipes 17-3-5 on embedding a Flash file in servlets and JSPs; Recipe 17.6 on embedding a QuickTime movie in a JSP.

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

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: