JSP Éú³ÉËæ»úÑéÖ¤ÂëͼƬ
<%@ 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();
out.clear();
request.setAttribute("gzip", "false");
// Êä³öͼÏóµ½Ò³Ãæ
ImageIO.write(image, "JPEG", response.getOutputStream());
return;
%>
Ïà¹ØÎĵµ£º
Ê×ÏÈ£¬±ØÐë˵Ã÷ÕâЩ²»ÊÇÎÒ×Ô¼ºÐ´µÄ£¬µ«ÊǸøÁËÎҺܴó°ïÖú£¬Ï£Íû¶ÔÄãÒ²ÄÜÓÐËù°ïÖú£¡
×£Äã³É¹¦£¡
Ò»¡¢¿ª·¢»·¾³ÅäÖÃ
µÚÒ»²½£ºÏÂÔØj2sdkºÍtomcat£ºµ½sun¹Ù·½Õ¾£¨http://java.sun.com/j2se/1.5.0/download.jsp£©ÏÂÔØj2sdk£¬×¢ÒâÏÂÔذ汾ΪWindows Offline InstallationµÄSDK£¬Í¬Ê±×îºÃÏÂÔØJ2SE 1.5.0 Documentation£¬È»ºóµ½ ......
°ÑÄÇЩÏÞÖÆ·ÃÎʵÄ×ÊÔ´£¨±ÈÈç˵jspÔ´´úÂ룩·Åµ½WebÓ¦ÓõÄWEB-INFĿ¼Ï£¬¶ÔÓÚ/web-INF/¼°Æä×ÓĿ¼£¬²»ÔÊÐíÖ±½ÓµÄ¹«¹²·ÃÎÊ£¬ËùÒԾͿÉÒÔÆðµ½±£»¤ÕâЩ´úÂëδ¾ÊÚȨµÄ·ÃÎʺͿúÊÓ£¬¸üºÃµÄ±£»¤ÁËÔ´´úÂë¡£
ͨ³£JSP¿ª·¢ÈËÔ±»á°ÑËûÃǵÄÒ³ÃæÎļþ´æ·ÅÔÚWebÓ¦ÓÃÏàÓ¦µÄ×ÓĿ¼Ï¡£ÕâÖÖ·½·¨µÄÎÊÌâÊÇ ......
ÒòΪҪʵÏÖÒ»¸öWebServer£¬Ð´ÍêÁËÒ»¸ö¼òµ¥µÄWebServerºóÍ»·¢ÆæÏ룬ºÎ²»ÊµÏÖÒ»¸öJSP µÄWebServerÄØ£¿
ÓÐÁËÕâ¸öÏë·¨ºó£¬¾Í¿ªÊ¼¿¼ÂÇJSPµÄʵÏÖÔÀí£¬ÆäʵJSPµÄʵÏÖÒ²ºÜ¼òµ¥£¬Ëµ°×Á˾ÍÊǽ«Æäת»»³ÉÒ»¸öJavaÎļþ£¬ÔÙ¶ÔÕâ¸öJavaÎļþ½øÐбàÒ룬Éú³ÉÀàÎļþ£¬½Ó×ÅÔÙÓÉ·þÎñÆ÷ʹÓÃÕâ¸öÀàÎļþ¡£´Ó×ÜÌåÉÏ·Ö£¬¿ÉÒÔ½«JSPÎļþµÄÄÚÈÝ»®·ÖΪÁ½ ......
setAttribute()ÊÇÓÃÓÚ·þÎñÆ÷¶ËÉèÖÃÊôÐÔÏò¿Í»§¶Ë·¢Ë͸ÃÊôÐÔ£¬ÔÚ¿Í»§¶Ëͨ¹ýgetAttribute£¨£©»ñµÃÊôÐÔ¡£setAttribute()Ò²ÄÜ´«µÝ¶ÔÏó¡£
setParameter()ÊÇ¿Í»§¶ËÏò·þÎñÆ÷¶Ë·¢ËÍÊôÐÔ£¬Ö»ÄÜ´«×Ö·û£¬ÓÃÓÚget/postµÄÊýÖµ´«µÝ£¬ÔÚ·þÎñ¶ËÓÃgetParameter£¨£©»ñÈ¡£¬Ò³ÃæÖÐÓÐforward´Ó¶¨ÏòÒ³ÃæµÄµØÖ·ÖеIJÎÊýÒ²ÄÜÓÃgetParameter£¨£©»ñÈ¡¡ ......
ÒýÓÃ×Ô: http://hi.baidu.com/tickwudi/blog/item/1c0ddb6dd4e821fc4316949b.html
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort ......