我在网上找了几个例子:例如:
<%@ page contentType="image/jpeg" import="java.awt.*,
java.awt.image.*,java.util.*,javax.imageio.*" %>
<%!
Color getRandColor(int fc,int bc){//给定范围获得随机颜色
        Random random = new Random();
        if(fc>255) fc=255;
        if(bc>255) bc=255;
        int r=fc+random.nextInt(bc-fc);
        int g=fc+random.nextInt(bc-fc);
        int b=fc+random.nextInt(bc-fc);
        return new Color(r,g,b);
        }
%>
<%
//设置页面不缓存
response.setHeader("Pragma","No-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires", 0);// 在内存中创建图象
int width=60, height=20;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);// 获取图形上下文
Graphics g = image.getGraphics();//生成随机类
Random random = new Random();// 设定背景色
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());
//g.drawRect(0,0,width-1,height-1);// 随机产生155条干扰线,使图象中的认证码不易被其它程序探测到
g.setColor(getRandColor(160,200));
for (int i=0;i<155;i++)
{
 int x = random.nextInt(width);
 int y = random.nextInt(height);
        int xl = random.nextInt(12);
        int yl = random.nextInt(12);
 g.drawLine(x,y,x+xl,y+yl);
}// 取随机产生的认证码(4位数字)
String sRand="";
for (int i=0;i<4;i++){
    String rand=String.valueOf(random.nextInt(10));
    sRand+=rand;
    // 将认证码显示到图象中
    g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));
//调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成
    g.drawString(rand,13*i+6,16);
}// 将认证码存入SESSION
session.setAttribute("rand",sRand);// 图象生效
g.dispose();// 输出图象到页面
ImageIO.write(image, "JPEG", response.getOutputStream());
%>
但我在weblogic8或tomcat5+jdk1.4中都没有显示成功!在weblogic中就显示一个红“X”的图像,在tomcat中显示:
HTTP Status 500 - --------------------------------------------------------------------------------type Exception reportmessage description The server encountered an internal error () that prevented it from fulfilling this request.exception javax.servlet.ServletException
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:825)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:758)
org.apache.jsp.admin.test.code_jsp._jspService(code_jsp.java:118)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
root cause java.lang.NoClassDefFoundError
java.lang.Class.forName0(Native Method)
java.lang.Class.forName(Class.java:141)
java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment(GraphicsEnvironment.java:62)
java.awt.image.BufferedImage.createGraphics(BufferedImage.java:1041)
java.awt.image.BufferedImage.getGraphics(BufferedImage.java:1031)
org.apache.jsp.admin.test.code_jsp._jspService(code_jsp.java:67)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
note The full stack trace of the root cause is available in the Apache Tomcat/5.0.28 logs.请问如何解决!谢谢!

解决方案 »

  1.   

    是不是你的相应的类包没有放到classpath下面
      

  2.   

    奇怪,你把jsp存成utf-8格式的再试试,我感觉这个和编码好象关系不大。你的系统是什么系统,是否是linux环境,linux据说是对awt有点什么毛病的,具体的我也忘记了,你搜搜看。另外,这主要是说明你这个包找不到。我在windows下测试是好的。
      

  3.   

    我的系统是:linux+tomcat ;windows2000server+weblogic8
    请问这个jsp要调用哪些类包?上面错误提示哪个类包找不到?如何解决?谢谢!!!
      

  4.   

    刚才帮你查了一下,有两种解决方式:(1) jdk1.4以前版本:需要安装 XFree86和XFree86-Xvfb ,加入 export DISPLAY=hostdomain:0.0 (2) jdk 1.4以后版本,在执行命令java 加入参数-Djava.awt.headless=true,表示这是一个没有键盘 没有显示器的无头服务器,意称机房托管的服务器。
      

  5.   

    http://www.evget.com/articles/evget_866.html
    另外,建议楼主google用"awt linux"来找结果
      

  6.   

    回复:
    我的weblogic的startweblogic.cmd如下:
    @ECHO OFF@REM WARNING: This file is created by the Configuration Wizard.
    @REM Any changes to this script may be lost when adding extensions to this configuration.SETLOCAL@REM *************************************************************************
    @REM This script is used to start WebLogic Server for the domain in the
    @REM current working directory.  This script simply sets the SERVER_NAME
    @REM variable and starts server.
    @REM 
    @REM To create your own start script for your domain, all you need to set is
    @REM SERVER_NAME, then starts the server.
    @REM 
    @REM Other variables that startWLS takes are:
    @REM 
    @REM WLS_USER     - cleartext user for server startup
    @REM WLS_PW       - cleartext password for server startup
    @REM PRODUCTION_MODE    - true for production mode servers, false for
    @REM                development mode
    @REM JAVA_OPTIONS - Java command-line options for running the server. (These
    @REM                will be tagged on to the end of the JAVA_VM and MEM_ARGS)
    @REM JAVA_VM      - The java arg specifying the VM to run.  (i.e. -server,
    @REM                -hotspot, etc.)
    @REM MEM_ARGS     - The variable to override the standard memory arguments
    @REM                passed to java
    @REM 
    @REM For additional information, refer to the WebLogic Server Administration
    @REM Console Online Help(http:\\e-docs.bea.com\wls\docs81\ConsoleHelp\startstop.html)
    @REM *************************************************************************@REM Initialize the common environment.set WL_HOME=C:\bea\weblogic81
    for %%i in ("%WL_HOME%") do set WL_HOME=%%~fsiset PRODUCTION_MODE=set JAVA_VENDOR=Sunset JAVA_HOME=C:\bea\jdk141_05
    for %%i in ("%JAVA_HOME%") do set JAVA_HOME=%%~fsi@REM Call commEnv here AFTER setting the java_vendor to get common environmental settings.call "%WL_HOME%\common\bin\commEnv.cmd"@REM Set SERVER_NAME to the name of the server you wish to start up.set SERVER_NAME=myserverset CLASSPATH=%WEBLOGIC_CLASSPATH%;%POINTBASE_CLASSPATH%;%JAVA_HOME%\jre\lib\rt.jar;%WL_HOME%\server\lib\webservices.jar;%CLASSPATH%@REM Call WebLogic Serverecho .
    echo CLASSPATH=%CLASSPATH%
    echo .
    echo PATH=%PATH%
    echo .
    echo ***************************************************
    echo *  To start WebLogic Server, use a username and   *
    echo *  password assigned to an admin-level user.  For *
    echo *  server administration, use the WebLogic Server *
    echo *  console at http:\\[hostname]:[port]\console    *
    echo ***************************************************%JAVA_HOME%\bin\java %JAVA_VM% %MEM_ARGS% %JAVA_OPTIONS% -Dweblogic.Name=%SERVER_NAME% -Dweblogic.ProductionModeEnabled=%PRODUCTION_MODE% -Djava.security.policy="%WL_HOME%\server\lib\weblogic.policy" weblogic.ServerENDLOCAL我的jdk是JAVA_HOME=C:\bea\jdk141_05请问您说的参数要加在什么地方?谢谢您!
      

  7.   

    呵呵,楼主这还真是为难到我了,我一直在用websphere,从来就没用过weblogic。不过你可以在
    %JAVA_HOME%\bin\java %JAVA_VM% %MEM_ARGS% %JAVA_OPTIONS% -Dweblogic.Name=%SERVER_NAME% -Dweblogic.ProductionModeEnabled=%PRODUCTION_MODE% -Djava.security.policy="%WL_HOME%\server\lib\weblogic.policy" weblogic.Server这里试试,变成
    %JAVA_HOME%\bin\java %JAVA_VM% %MEM_ARGS% %JAVA_OPTIONS% -Dweblogic.Name=%SERVER_NAME% -Dweblogic.ProductionModeEnabled=%PRODUCTION_MODE% -Djava.security.policy="%WL_HOME%\server\lib\weblogic.policy" -Djava.awt.headless=true weblogic.Server
    再看看。
      

  8.   

    回复: mxlmwl(飞星) 
    对不起啊,我在tomcat+linux上已经成功了!但在weblogic上还是不行!我按照您的方法修改了的startweblogic.cmd文件,问题依旧!望高手指点!谢谢了!
      

  9.   

    http://www.idautomation.com/kb/xwindow-error.html里面有这么一段话,你看看
    The best solution to this problem is to run Java in headless mode. We have modified our Java Barcode Products to be compatible with headless operation as of April 2, 2003. If you have (1) Java Packages with dates older than this and (2) you are using the "Headless" capabilities of Java 1.4 and greater and (3) you have active Extended Support and Upgrade Protection, please email us your order ID and we will send you the update. If you need to add or update your Extended Support and Upgrade Protection, you may order it here. After your order is processed, you will receive a download link to the latest version of the product. After installing Headless Java, you must enable it. For example, to enable headless mode on Sun JDK 1.4.1 running on Linux, enter
    export CATALINA_OPTS="-Djava.awt.headless=true
    to the /etc/profile file. 我现在在公司,没有linux系统,没办法帮你试。不过我觉得应该是加在weblogic的那个地方啊,那是什么地方有问题呢,奇怪了。
    还有,你这个怎么很象*.bat文件,不是*.sh文件啊,是不是你给我的启动文件不对??
      

  10.   

    ......
    %JAVA_HOME%\bin\java %JAVA_VM% %MEM_ARGS% %JAVA_OPTIONS% -Dweblogic.Name=%SERVER_NAME% -Dweblogic.ProductionModeEnabled=%PRODUCTION_MODE% -Djava.security.policy="%WL_HOME%\server\lib\weblogic.policy" weblogic.Server这里试试,变成
    %JAVA_HOME%\bin\java %JAVA_VM% %MEM_ARGS% %JAVA_OPTIONS% -Dweblogic.Name=%SERVER_NAME% -Dweblogic.ProductionModeEnabled=%PRODUCTION_MODE% -Djava.security.policy="%WL_HOME%\server\lib\weblogic.policy" -Djava.awt.headless=true weblogic.Server
    ......+response.setContentType("image/jpeg");