日期:2014-05-18  浏览次数:20680 次

jsp动态显示图像
<%@ page contentType="image/jpeg";pageEncoding="GBK"%>
<%@ page import="java.awt.*" %>
<%@ page import="java.awt.image.*" %>
<%@ page import="java.util.*"%>
<%@ page import="javax.imageio.*" %>

<title>在JSP中动态生成图像</title>
<%!
//产生随机颜色函数
  Color getRandColor(int fc,int bc){
  Random random = new Random();
  if(fc>255) fc=255;
  if(bc>255) bc=255;
   
  int red=fc+random.nextInt(bc-fc); //红
  int green=fc+random.nextInt(bc-fc); //绿
  int blue=fc+random.nextInt(bc-fc); //蓝
  return new Color(red,green,blue);
  }
%>

<%
//设置页面不缓存
  response.setHeader("Pragma","No-cache");
  response.setHeader("Cache-Control","no-cache");
  response.setDateHeader("Expires", 0);

  //在内存中创建图象
  int width=200, height=160;
  BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

  //获取图形上下文
  Graphics g = image.getGraphics();

  //设定背景色
  g.setColor(getRandColor(200,250));
  g.fillRect(0, 0, width, height);

  //设定字体
  g.setFont(new Font("Times New Roman",Font.PLAIN,18));

  //画边框
  g.setColor(new Color(0,0,0));
  g.drawRect(0,0,width-1,height-1);
  
  //输出文字
  g.drawString("**************",50,50); 
  g.drawString("Hello,BMY!",50,100);  


  //释放图形环境
  g.dispose();

  //输出图象到页面
  ImageIO.write(image, "JPEG", response.getOutputStream());
   
%>

代码如上:运行时报错如下,哪位高人给解释一下?
HTTP Status 500 - 

--------------------------------------------

type Exception report

message 

description The server encountered an internal error () that prevented it from fulfilling this request.

exception 

org.apache.jasper.JasperException: /DynamicPic.jsp(1,1) Unterminated &lt;%@ page tag
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:510)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:375)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


root cause 

org.apache.jasper.JasperException: /DynamicPic.jsp(1,1) Unterminated &lt;%@ page tag
org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:39)
org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:405)
org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:130)
org.apache.jasper.compiler.Parser.parseDirective(Parser.java:520)
org.apache.jasper.compiler.Parser.parseElements(Parser.java:1552)
org.apache.jasper.compiler.Parser.parse(Parser.java:126)
org.apache.jasper.compiler.ParserController.doParse(ParserController.java:211)
org.apache.jasper.compiler.Parse