posts - 89, comments - 241, trackbacks - 0, articles - 1
             :: 首頁 ::  :: 聯系 :: 聚合  :: 管理

          一個不會報錯的jsp彩色驗證碼程序

          Posted on 2009-09-22 14:52 saobaolu 閱讀(516) 評論(3)  編輯  收藏 所屬分類: javaweb
          一個不會報錯的jsp彩色驗證碼程序
          一般的,會報servlet錯誤:
          getOutputStream() has already been called for this response
          但是這個程序不會報錯,原因是把里面的空格都刪除
          -------------------------------------------------------------
          在tomcat中jsp編譯成servlet之后在函數_jspService(HttpServletRequest request, HttpServletResponse response)的最后
          有一段這樣的代碼
          finally {
                if (_jspxFactory != null) _jspxFactory.releasePageContext(_jspx_page_context);
              }
          這里是在釋放在jsp中使用的對象,會調用response.getWriter(),因為這個方法是和
          response.getOutputStream()相沖突的!所以會出現以上這個異常。

          然后當然是要提出解決的辦法,其實挺簡單的(并不是和某些朋友說的那樣--
          將jsp內的所有空格和回車符號所有都刪除掉),

          在使用完輸出流以后調用以下兩行代碼即可:
          out.clear();
          out = pageContext.pushBody();

          --------------------------------------------------------
          分割線內為引用~

           1<%@ page  import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*" %>
           2<%@ page import="java.io.OutputStream" %>
           3<%!
           4Color getRandColor(int fc,int bc){
           5Random random = new Random();
           6if(fc>255) fc=255;
           7if(bc>255) bc=255;
           8int r=fc+random.nextInt(bc-fc);
           9int g=fc+random.nextInt(bc-fc);
          10int b=fc+random.nextInt(bc-fc);
          11return new Color(r,g,b);
          12}

          13%>
          14<%
          15try{
          16response.setHeader("Pragma","No-cache");
          17response.setHeader("Cache-Control","no-cache");
          18response.setDateHeader("Expires"0);
          19int width=60, height=20;
          20BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
          21OutputStream os=response.getOutputStream();
          22Graphics g = image.getGraphics();
          23Random random = new Random();
          24g.setColor(getRandColor(200,250));
          25g.fillRect(00, width, height);
          26
          27g.setFont(new Font("Times New Roman",Font.PLAIN,18));
          28g.setColor(getRandColor(160,200));
          29for (int i=0;i<155;i++)
          30{
          31int x = random.nextInt(width);
          32int y = random.nextInt(height);
          33int xl = random.nextInt(12);
          34int yl = random.nextInt(12);
          35g.drawLine(x,y,x+xl,y+yl);
          36}

          37String sRand="";
          38for (int i=0;i<4;i++){
          39String rand=String.valueOf(random.nextInt(10));
          40sRand+=rand;
          41g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));
          42g.drawString(rand,13*i+6,16);
          43}

          44session.setAttribute("rand",sRand);
          45g.dispose();
          46
          47ImageIO.write(image, "JPEG",os);
          48os.flush();
          49os.close();
          50os=null;
          51response.flushBuffer();
          52out.clear();
          53out = pageContext.pushBody();
          54}

          55catch(IllegalStateException e)
          56{
          57System.out.println(e.getMessage());
          58e.printStackTrace();
          59}
          %>
          60


          沒有所謂的命運,只有不同的選擇!
          主站蜘蛛池模板: 武宣县| 阿图什市| 阿巴嘎旗| 水富县| 云南省| 类乌齐县| 信丰县| 平江县| 安平县| 遂平县| 乌什县| 和顺县| 无锡市| 桐庐县| 丰顺县| 什邡市| 揭西县| 西安市| 曲靖市| 永善县| 云安县| 灵宝市| 平舆县| 徐汇区| 格尔木市| 凤翔县| 丽江市| 安阳县| 呼和浩特市| 青浦区| 昌平区| 九龙县| 新民市| 江达县| 绥棱县| 花垣县| 六枝特区| 新巴尔虎右旗| 武强县| 吉首市| 太湖县|