在这个例子中,我们首先通过一个Servlet(ImageServlet)动态的生成一个图像,然后在另一个Servlet(ShowImageServlet)中通过设置<image>元素的src属性指向这个Servlet来动态地生成图像:
创建第一个Servlet用于生成图像,可以通过如下的url指定图像显示的文本内容:
http://localhost:8080/base-webapp/image?text=my image servlet
package com.gof.test.servlet; import java.io.IOException; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.image.BufferedImage; import com.sun.image.codec.jpeg.JPEGImageEncoder; import com.sun.image.codec.jpeg.JPEGCodec; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.ServletOutputStream; public class ImageServlet extends HttpServlet { /** * */ private static final long serialVersionUID = 6311783263824452228L; private Font font = new Font("Courier", Font.BOLD,12); protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{ String text = req.getParameter("text"); if(text == null){ text = "DEMO"; } int len = text.length(); resp.setContentType("image/jpeg"); ServletOutputStream out = resp.getOutputStream(); BufferedImage image = new BufferedImage(11*len, 16, BufferedImage.TYPE_INT_RGB); Graphics g = image.getGraphics(); g.setColor(Color.black); g.fillRect(0, 0, 11*len, 16); g.setColor(Color.white); g.setFont(font); char c; for (int i = 0; i < len; i++){ c = text.charAt(i); g.drawString(c + " ", i*11, 12); } JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); encoder.encode(image); out.close(); } }<span style="font-family:SimSun;font-size:10px;"> </span>
通过浏览器访问上面的URL:
下面创建第二个Servlet用于显示图像,其中图像通过在html正文中指定image的src属性,使其指向上面的servlet:
package com.gof.test.servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ShowImageServlet extends HttpServlet { /** * */ private static final long serialVersionUID = -6345607864598566037L; protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{ resp.setContentType("text/html;charset=utf-8"); PrintWriter out = resp.getWriter(); out.println("<html><head><title>Show Image</title></head>"); out.println("<body>"); String imageLink = "<img src='image?text=testing string'/>"; out.println("The image is: " + imageLink); out.println("</body></html>"); out.close(); } }
通过浏览器访问上面的Servlet:
http://localhost:8080/base-webapp/showimage
可以看到浏览器在遇到<image>元素的时候,再次发出一个http request请求第一个servlet - image;通过两次http request完成上面页面的加载。
配置servlet在web.xml:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name>Base Java Webapp</display-name> <description>A Basic Maven Java Webapp Application</description> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- test url: http://localhost:8080/base-webapp/download?filename=testreport.txt --> <servlet> <servlet-name>downloadtest</servlet-name> <servlet-class>com.gof.test.servlet.DownloadServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>downloadtest</servlet-name> <url-pattern>/download</url-pattern> </servlet-mapping> <!-- test upload file: http://localhost:8080/base-webapp/uploadfile.html --> <servlet> <servlet-name>uploadfiletest</servlet-name> <servlet-class>com.gof.test.servlet.UploadFileServlet</servlet-class> <init-param> <param-name>filePath</param-name> <param-value>/upload</param-value> </init-param> <init-param> <param-name>tempFilePath</param-name> <param-value>/tempupload</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>uploadfiletest</servlet-name> <url-pattern>/uploadfile</url-pattern> </servlet-mapping> <!-- Image Servlet --> <!-- access to this servlet through url: i.e. http://localhost:8080/base-webapp/image?text=123 --> <servlet> <servlet-name>image</servlet-name> <servlet-class>com.gof.test.servlet.ImageServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>image</servlet-name> <url-pattern>/image</url-pattern> </servlet-mapping> <!-- Show Image Servlet --> <!-- access to this servlet through url: i.e. http://localhost:8080/base-webapp/showimage --> <servlet> <servlet-name>showimage</servlet-name> <servlet-class>com.gof.test.servlet.ShowImageServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>showimage</servlet-name> <url-pattern>/showimage</url-pattern> </servlet-mapping> </web-app>