利用サーブレットhttpservlet类
パッケージcom.zyc.demo; java.awt.colorをインポートします。 java.awt.fontをインポートします。 java.awt.graphicsをインポートします。 java.awt.image.bufferedimageをインポートします。 java.io.ioexceptionをインポートします。 java.util.randomをインポートします。 javax.imageio.imageioをインポートします。 javax.servlet.servletexceptionをインポートします。 javax.servlet.http.httpservletをインポートします。 javax.servlet.http.httpservletrequestをインポートします。 javax.servlet.http.httpservletResponseをインポートします。 javax.servlet.http.httpsessionをインポートします。パブリッククラスのdrewimageはhttpservlet { / ** * * / private static final long serialversionuid = 1505032428319459075l;プライベートファイナルフォントmfont = new Font( "Arial Black"、font.plain、16);プライベートファイナルint img_width = 100;プライベートファイナルint img_heigth = 18;プライベートカラー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);新しい色(r、g、b)を返します。 } public void service(httpservletrequest request、httpservletResponse応答)servletexception、ioexception {respons.setheader( "pragma"、 "no-cache"); Response.setheader( "Cache-Control"、 "no-cache"); Response.setDateHeader( "Expires"、0); Response.setContentType( "Image/jpeg"); bufferedimage image = new BufferedImage(IMG_Width、IMG_Heigth、BufferedImage.Type_int_rgb);グラフィックg = image.getGraphics(); RANDOM RANDOM = new Random(); G.SetColor(GetRandColor(200、250)); g.fillrect(1、1、img_width -1、img_heigth -1); G.SetColor(新しい色(102、102、102)); g.drawrect(0、0、img_width -1、img_heigth -1); G.SetColor(GetRandColor(160,200)); for(int i = 0; i <30; i ++){int x = random.nextint(img_width -1); int y = random.nextint(img_heigth -1); int xl = random.nextint(6) + 1; int yl = random.nextint(12) + 1; G.drawline(x、y、x + xl、y + yl); } g.setColor(getRandColor(160,200)); for(int i = 0; i <30; i ++){int x = random.nextint(img_width -1); int y = random.nextint(img_heigth -1); int xl = random.nextint(12) + 1; int yl = random.nextint(6) + 1; G.drawline(x、y、x -xl、y -yl); } g.setfont(mfont);文字列srand = ""; for(int i = 0; i <4; i ++){string tmp = getRandomChar(); srand += tmp; g.setColor(新しい色(20 + random.nextint(110)、20 + random.nextint(110)、20 + random.nextint(110))); G.ドローストリング(TMP、15 * I + 10,15); } httpsession session = request.getSession(true); session.setattribute( "rand"、srand); // system.out.println( "写入セッション"+srand); g.dispose(); Imageio.write(image、 "jpeg"、Response.getOutputStream()); } private string getRandomChar(){int rand =(int)math.round(math.random() * 2);長いitmp = 0; char ctmp = '/u0000'; switch(rand){ケース1:itmp = math.round(math.random() * 25 + 65); ctmp =(char)itmp; return string.valueof(ctmp);ケース2:itmp = math.round(math.random() * 25 + 97); ctmp =(char)itmp; return string.valueof(ctmp);デフォルト:itmp = math.round(math.random() * 9); itmp + ""を返します。 }}}下面是web.xml配置
<?xml version = "1.0" encoding = "utf-8"?> <web-app xmlns:xsi = "http://www.w3.org/2001/xmlschema-instance" xmlns = "http://java.sun.com/xml/nns/javaee xsi:schemalocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id = "webapp_id"バージョン= "3.0 <welcome-file> index.html </welcome-file> <welcome-file> index.htm </welcome-file> <welcome-file> index.jsp </welcome-file> <welcome-file> default.html </welcome-file> default.htm </welcome-file> <welcome-file> <Servlet-Name> img </servlet-name> <servlet-class> com.zyc.demo.drewimage </servlet-class> </servlet> <servlet-mapping> <servlet-name> img </servlet-name> <url-pattern> /img.do </url-pattern>
JSP文件
<%@ page Language = "java" import = "java.util。*" pageencoding = "utf-8"%> <%string path = request.getContextPath(); string basepath = request.getscheme()+"://"+request.getServername()+":"+request.getServerport()+path+"/"; %> <!doctype html public " - // w3c // dtd html 4.01 transitional // en"> <html> <head> <base href = "<%= basepath%>"> <titl> my jsp 'yanzhengma.jsp'スタートページ</title> <meta http-equiv = "pragma" http-equiv = "cache-control" content = "no-cache"> <meta http-equiv = "content =" 0 "> <meta http-equiv =" keyword1、keyword3 "keyword3"> <meta http-equiv = "content =" type = "text/css" href = "styles.css"> - > </head> <body> <img src = "img.do"> <button onclick = "window.location.reload();">刷新</button> </body> </html>
简单实用。
以上就是本文的全部内容、希望对大家的学习有所帮助、也希望大家多多支持武林网。