两个页面
验证码页面 image.jsp
单独使用 可以很正常  contentType="image/jpeg"
注册页面 register.jsp 
contentType="text/html; charset=gb2312"
单独使用 ,很正常
但是我把 image.jsp 加到 register.jsp的时候就不行了
我试了两种方法
<%@ include file="image.jsp" model="true"%>
<%@ include file="image.jsp" Model="true"%>
<%@ include file="image.jsp" %>
这样都不行 
出的错误是一样的
"register.jsp": org.apache.jasper.JasperException: file:E:/myporject/school/SchoolModel/image.jsp(1,1) Page directive: illegal to have multiple occurrences of contentType with different values (old: text/html; charset=gb2312, new: image/jpeg)我的web.xml 的配置
<jsp-config>
    <jsp-property-group>
      <description>htmls encoding example</description>
      <display-name>JSPConfiguration</display-name>
      <url-pattern>*.jsp</url-pattern>
      <url-pattern>*.html</url-pattern>
      <el-ignored>true</el-ignored>
      <page-encoding>gb2312</page-encoding>
      <scripting-invalid>false</scripting-invalid>
    </jsp-property-group>
  </jsp-config>
但是似乎对我的页面 没什么影响
我还是得手动添加 contentType="text/html; charset=gb2312"
才能解决乱码问题
我的环境jbuilder tomcat 5 +j2se 1.5
哪位知道怎么解决的麻烦说一下
谢了

解决方案 »

  1.   

    那怎么把servlet的验证吗添加到 jsp中了?
    难道要我的登陆页面都用servlet吗?
      

  2.   

    而且servlet中也存在
    contentType 的问题啊
      

  3.   

    package org.epilot.toolkit.jpeg;import javax.servlet.*;
    import javax.servlet.http.*;
    import java.io.*;
    import java.util.*;
    import java.awt.*;
    import java.awt.image.*;
    import org.epilot.toolkit.log.Log;
    import org.epilot.toolkit.servlet.ServletUtil;/**
     * <p>Title: @</p>
     *
     * <p>Description: Generate the jpeg image and output it to the
     * stream, such as web page stream.</p>
     *
     * <p>Copyright: Copyright (c) 2004</p>
     *
     * <p>Company: Snail CO. LTD</p>
     *
     * @author not attributable
     * @version 1.0
     */public class ImageServlet
        extends HttpServlet {
      public static final String CONTENT_TYPE = "image/jpeg; charset=GBK";
      public static final String IMAGEINFO_ECHO = "randomImage";  // initialize global variables
      private OutputStream out;
      private Object imageInfo;  public void init() throws ServletException {
      }  /**
       * Process the HTTP Get request
       * @param request HttpServletRequest
       * @param response HttpServletResponse
       * @throws ServletException
       * @throws IOException
       */
      public void doGet(HttpServletRequest request,
                        HttpServletResponse response) throws ServletException,
          IOException {
        response.setContentType(CONTENT_TYPE);
        //设置页面不缓存
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);    HttpSession session = request.getSession(true);
        String paramName = ServletUtil.getParamValue(request, "paramName");    this.out = response.getOutputStream();
        this.imageInfo = outputImage(out);
        if(paramName.length() <= 0) {
          session.setAttribute(IMAGEINFO_ECHO, this.imageInfo);
        } else {
          session.setAttribute(paramName, this.imageInfo);
        }
      }  /**
       * Clean up resources
       */
      public void destroy() {
      }  /**
       * 输出图象
       * @param os OutputStream
       * @return Object
       * @throws IOException
       */
      public Object outputImage(OutputStream os) throws IOException  {
        Random rand = new Random();    Dimension size = new Dimension(60, 20);
        BufferedImage image = new BufferedImage(size.width,
                                                size.height,
                                                BufferedImage.TYPE_INT_RGB);    // 获取图形上下文
        Graphics imageG2D = image.getGraphics();    // 设定背景色
        Color background = new Color(255, 255, 255);
        imageG2D.setColor(background);
        imageG2D.fillRect(0, 0, size.width, size.height);    //设定字体
        imageG2D.setFont(new Font("Times New Roman", Font.PLAIN, 18));    //画边框
        imageG2D.setColor(randomRGBColor(100, 50));
        imageG2D.drawRect(0, 0, size.width-1, size.height-1);    // 随机产生100 个干扰点
        imageG2D.setColor(randomRGBColor(160, 40));
        for (int i = 0; i < 100; i++) {
          int x = rand.nextInt(size.width);
          int y = rand.nextInt(size.height);
          int w = rand.nextInt(4);
          int d = rand.nextInt(4);
          imageG2D.fillOval(x, y, w, d);
        }    // 取随机产生的认证码(4位数字)
        String verifyCode = "";
        for (int i = 0; i < 4; i++) {
          String digit = String.valueOf(rand.nextInt(10));
          verifyCode += digit;
          // 将认证码显示到图象中
          Color fontColor = randomRGBColor(20, 110);
          imageG2D.setColor(fontColor);
          imageG2D.drawString(digit, 13 * i + 6, 16);
        }    // 图象生效,释放资源
        imageG2D.dispose();    // 输出图象到页面
        javax.imageio.ImageIO.write(image, "JPEG", os);
    //    outputJpeg(image, os);    return verifyCode;  }  /**
       * Translate the image into the jpeg stream
       * @param theImage Image
       * @param os OutputStream
       */
      private void outputJpeg(Image theImage, OutputStream os) {
        try {
          JpegEncoder je = new JpegEncoder(theImage, 90, os);
          je.Compress();    }
        catch (Exception e) {
          Log.error(e, "Error in generating the jpeg image.");
        }
      }  /**
       *
       * @param base int
       * @param step int
       * @return Color
       */
      private Color randomRGBColor(int base, int step) { //给定范围获得随机颜色
        Random random = new Random();
        int r = base + random.nextInt(step);
        int g = base + random.nextInt(step);
        int b = base + random.nextInt(step);
        if (r > 255) r = 255;
        if (g > 255) g = 255;
        if (b > 255) b = 255;
        return new Color(r, g, b);
      }}
    这个是servlet
    然后在页面里<img src="/common/ImageServlet?paramName=eaiLoginVCode" style="vertical-align: bottom" alt="">你这样看看。我感觉这样比较好些,
      

  4.   

    我使用
    <img src="image.jsp ">
    也可以了
    但是图片不会变
    这是为什么??
      

  5.   

    还有
    package org.epilot.toolkit.jpeg;
    这个是什么包?
      

  6.   

    package com.dev.servlet;import java.io.*;
    import java.awt.*;
    import java.awt.image.*;
    import java.awt.image.BufferedImage;import javax.servlet.*;
    import javax.servlet.http.*;import com.sun.image.codec.jpeg.*;
    import com.sun.image.codec.jpeg.JPEGCodec;import com.dev.util.Picture;
    import com.dev.log.Log;public class CreatePicture
        extends HttpServlet {    static final private String CONTENT_TYPE = "text/html; charset=gb2312";    final String input_back_color_error = "input rgb backcolor is error";    final String input_fore_color_error = "input rgb forecolor is error";    private Picture pic = new Picture();    //Initialize global variables
        public void init() throws ServletException {    }    //Process the HTTP Get reques
        public void doGet(HttpServletRequest request, HttpServletResponse response) throws
            ServletException, IOException {        try {
                int imageWidth = 30;
                int imageHeight = 17;            BufferedImage image = new BufferedImage(imageWidth, imageHeight,
                    BufferedImage.TYPE_INT_RGB);
                Graphics graphics = image.getGraphics();
                graphics.setColor(new Color(171, 213, 247));
                graphics.fillRect(0, 0, imageWidth, imageHeight);
                graphics.setColor(new Color(171, 213, 247));            FileOutputStream fos = new FileOutputStream("attach.jpg");
                BufferedOutputStream bos = new BufferedOutputStream(fos);
                JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos);
                encoder.encode(image);
                bos.close();        }
            catch (Exception e) {
                System.out.println("生成图片出错!");
                e.printStackTrace();
            }        /* String queryNum = request.getParameter("Image");
                             String queryRgb = "";                         if (request.getParameter("Rgb") != null) {
                    queryRgb = request.getParameter("Rgb");
                             } */        try {
                String queryNum = pic.getRandom(); //随机生成的附加码
                String queryRgb = "255|0|0";            response.setHeader("Cache-Control", "no-store");
                response.setContentType("image/jpeg");
                ServletOutputStream out = response.getOutputStream();            //jpg格式的背景色图片(于页面风格一样),宽3.6毫米,高1.8毫米
                InputStream imageIn = new FileInputStream(new File("attach.jpg"));            JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(imageIn);
                BufferedImage image4 = decoder.decodeAsBufferedImage();            //解密
                //queryNum = pic.discrypt(queryNum);            HttpSession session = request.getSession(true);
                session.setAttribute("AdditiveNo", queryNum);            Graphics g = image4.getGraphics();
                if (queryRgb.length() > 1) {
                    if (pic.masterData(queryRgb) != null) {
                        int[] arg = pic.masterData(queryRgb);
                        g.setColor(new Color(arg[0], arg[1], arg[2]));
                    }
                }
                else {
                    g.setColor(new Color(255, 0, 0));
                }            g.drawString(queryNum, 0, 13);
                JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
                encoder.encode(image4);
                out.close();
            }
            catch (Exception ex) {
                ex.printStackTrace();
                try {
                    Log.log("\r\n" + this.getClass() + ":" + ex.toString());
                }
                catch (Exception ex1) {}        }    }
    }
      

  7.   

    package com.dev.util;import java.io.*;public class Picture {    final int key = 1;    final String error_format_int =
            "format of color is not rgb.sample \"212|232|0\"";    final String error_color_input =
            "format of color(num|num|num): num in 0-255";    /**格式化输出数据**/
        public String manage(String temp) {
            String returnStr = "";
            temp = encrypt(temp);        byte[] by = temp.getBytes();        for (int i = 0; i < by.length; i++) {
                returnStr = returnStr + (byte) by[i] + "|";
            }
            return returnStr;
        }    /**格式化输入数据**/
        public byte[] disManage(String temp) {
            int len = 0, index = 0, i = 0, first = 0;        while ( (i = temp.indexOf("|", first)) > -1) {
                len++;
                first = i + 1;
            }        byte[] by = new byte[len];
            first = 0;
            while ( (i = temp.indexOf("|", first)) > -1) {
                by[index] = Byte.parseByte(temp.substring(first, i));
                index++;
                first = i + 1;
            }
            return by;
        }    /**随机生成四位的附加码**/
        public String getRandom() {
            int i1 = (int) (java.lang.Math.random() * 10);
            int i2 = (int) (java.lang.Math.random() * 10);
            int i3 = (int) (java.lang.Math.random() * 10);
            int i4 = (int) (java.lang.Math.random() * 10);        return String.valueOf(i1) + String.valueOf(i2) + String.valueOf(i3) +
                String.valueOf(i4);
        }    /**加密1:错位处理**/
        public String encrypt(String randomStr) {
            String para = random() + randomStr.substring(0, 1) + random() + random() +
                randomStr.substring(1, 2);
            para = para + random() + randomStr.substring(2);
            return jiaMi(para);
        }    /**得到随机数0-9之间**/
        private String random() {
            String temp = String.valueOf( (int) (java.lang.Math.random() * 10));        return temp;
        }    /**加密2:加密处理,此方法可以自己修改**/
        private String jiaMi(String str) {        byte[] by = str.getBytes();
            ByteArrayInputStream in = new ByteArrayInputStream(by);        int ch;
            int index = 0;
            byte[] temp = new byte[in.available()];        while ( (ch = in.read()) != -1) {
                temp[index] = (byte) (ch - key);
                index++;
            }        ByteArrayInputStream ins = new ByteArrayInputStream(temp);
            BufferedReader fReader = new BufferedReader(new InputStreamReader(ins));        try {
                return fReader.readLine();
            }
            catch (Exception e) {
                e.printStackTrace();
                return "";
            }    }    /**从给的数字里得到正确的数字**/
        public String discrypt(String temp) {
            String para = jieMi(disManage(temp));
            return para.substring(1, 2) + para.substring(4, 5) +
                para.substring(6, 8);
        }    /**解密处理**/
        private String jieMi(byte[] by) {        ByteArrayInputStream in = new ByteArrayInputStream(by);
            int ch;
            int index = 0;
            byte[] temp = new byte[in.available()];        while ( (ch = in.read()) != -1) {
                temp[index] = (byte) (ch + key);
                index++;
            }        ByteArrayInputStream ins = new ByteArrayInputStream(temp);
            BufferedReader fReader = new BufferedReader(new InputStreamReader(ins));        try {
                return fReader.readLine();
            }
            catch (Exception e) {
                e.printStackTrace();
                return "";
            }    }    /**分解rgb格式的颜色 num|num|num**/
        public int[] masterData(String temp) {        int index_len = 0, index = 0, next_index = 0;
            int[] return_arr = new int[3];
            boolean break_error = false;        if (getMax(temp, "|") == 2) {
                while ( (index_len = temp.indexOf("|", next_index)) > -1) {
                    if (getInt(temp.substring(next_index, index_len)) == 256) {
                        break_error = true;
                    }
                    else {
                        return_arr[index] = getInt(temp.substring(next_index,
                            index_len));
                        next_index = index_len + 1;
                        index++;
                    }
                    if (break_error) {
                        break;
                    }
                }            if (break_error) {
                    return null;
                }
                else {
                    return_arr[index] = getInt(temp.substring(next_index));
                    return return_arr;
                }
            }
            else {
                System.out.println(error_format_int + ":" + temp);
                return null;
            }
        }    private int getMax(String temp, String temp2) {
            int index = 0, index_len = 0, index_next = 0;        while ( (index = temp.indexOf(temp2, index_next)) > -1) {
                index_len++;
                index_next = index + 1;
            }        return index_len;
        }    private int getInt(String temp) {
            try {
                return Integer.parseInt(temp);
            }
            catch (Exception e) {
                e.printStackTrace();
                System.out.println(error_color_input + ":" + temp);
                return 256;
            }
        }
    }
      

  8.   

    package org.epilot.toolkit.jpeg;
    这个自己改改,改成自己要放到那个包就可以了