🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
## 19.2 编写一个简单的Servlet应用 ServletDemo1类实现了一个非常简单的servlet,该servlet返回了一个PNG图,PNG图是使用JFreeChart生成的直方条形图表。当运行该程序时,servlet在客户端仅仅显示一幅图片,而没有任何的HTML修饰,参见下图19.1。 ![](img/jfc117021.png) 如图19.1 浏览器中的servlet效果 我们以这种方式显示图片,是没有特殊意义,仅仅是为了: + 很好演示servlets的请求-响应交互特征; + 作为测试实例非常有用,我们会了解如何配置一个服务环境,如何让页面控件工作。 我们可以浏览后面更复杂的实例,显示使用HMTL表单如何请求不同的图表,并且将产生的图表的输出植入到HTML中。下面是基本servlet的代码。 ``` package demo; import java.io.IOException; import java.io.OutputStream; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartUtilities; import org.jfree.chart.JFreeChart; import org.jfree.chart.plot.PlotOrientation; import org.jfree.data.category.DefaultCategoryDataset; /** * A basic servlet that returns a PNG image file generated by JFreeChart. This * class is described in the JFreeChart Developer Guide in the "Servlets" * chapter. */ public class ServletDemo1 extends HttpServlet { /** * Creates a new demo. */ public ServletDemo1() { // nothing required } /** * Processes a GET request. * * @param request * the request. * @param response * the response. * * @throws ServletException * if there is a servlet related problem. * @throws IOException * if there is an I/O problem. */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { OutputStream out = response.getOutputStream(); try { DefaultCategoryDataset dataset = new DefaultCategoryDataset(); dataset.addValue(10.0, "S1", "C1"); dataset.addValue(4.0, "S1", "C2"); dataset.addValue(15.0, "S1", "C3"); dataset.addValue(14.0, "S1", "C4"); dataset.addValue(-5.0, "S2", "C1"); dataset.addValue(-7.0, "S2", "C2"); dataset.addValue(14.0, "S2", "C3"); dataset.addValue(-3.0, "S2", "C4"); dataset.addValue(6.0, "S3", "C1"); dataset.addValue(17.0, "S3", "C2"); dataset.addValue(-12.0, "S3", "C3"); dataset.addValue(7.0, "S3", "C4"); dataset.addValue(7.0, "S4", "C1"); dataset.addValue(15.0, "S4", "C2"); dataset.addValue(11.0, "S4", "C3"); dataset.addValue(0.0, "S4", "C4"); dataset.addValue(-8.0, "S5", "C1"); dataset.addValue(-6.0, "S5", "C2"); dataset.addValue(10.0, "S5", "C3"); dataset.addValue(-9.0, "S5", "C4"); dataset.addValue(9.0, "S6", "C1"); dataset.addValue(8.0, "S6", "C2"); dataset.addValue(null, "S6", "C3"); dataset.addValue(6.0, "S6", "C4"); dataset.addValue(-10.0, "S7", "C1"); dataset.addValue(9.0, "S7", "C2"); dataset.addValue(7.0, "S7", "C3"); dataset.addValue(7.0, "S7", "C4"); dataset.addValue(11.0, "S8", "C1"); dataset.addValue(13.0, "S8", "C2"); dataset.addValue(9.0, "S8", "C3"); dataset.addValue(9.0, "S8", "C4"); dataset.addValue(-3.0, "S9", "C1"); dataset.addValue(7.0, "S9", "C2"); dataset.addValue(11.0, "S9", "C3"); dataset.addValue(-10.0, "S9", "C4"); JFreeChart chart = ChartFactory.createBarChart("Bar Chart", "Category", "Value", dataset, PlotOrientation.VERTICAL, true, true, false); response.setContentType("image/png"); ChartUtilities.writeChartAsPNG(out, chart, 400, 300); } catch (Exception e) { System.err.println(e.toString()); } finally { out.close(); } } } ``` 当一个客户端(通常是一个web浏览器)发出一个请求时,Servlet引擎调用DoGet()方法,以响应这个请求,servlet执行下面几步: + 为客户端返回的输出获得一个输出流引用 + 创建一个图表; + 响应的内容类型设置为image/png,这告诉客户端接受的数据类型是什么; + 一个图表的PNG图表beii写进输出流; + 输出流关闭。