⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 codeservlet.java

📁 简单的SREVLET的实验项目 大家可以下载试用一下看看或许有什么帮助
💻 JAVA
字号:
package com.zg.servlet;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class CodeServlet extends HttpServlet {

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		 response.setContentType("image/jpeg");//设置MIME类型为图片
		    
		    int width = 50;//设置宽
		    int height = 17;//设置高
		    
		    OutputStream os = response.getOutputStream();//得到输入输出流
		    
		    BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);//生成图片缓存 参数:图片的宽和高,类型(RGB)
	
		    //得到图片的生成对象
		    Graphics gs = image.getGraphics();
		    
		    //设置整个图片的背景色
		    gs.setColor(Color.white);
		    
		    //生成图片
		    gs.fillRect(0, 0, width, height);
		    
		    //设置字体  参数:字体类型,字体样式,字体大小
		    gs.setFont(new Font("黑体",Font.BOLD,18));
		    
		    Color[] col = new Color[20];
		    
		    Random ran = new Random();
		    
		    for(int i=0;i<col.length;i++){
		    	
		       int x = ran.nextInt(250);
		       int y = ran.nextInt(250);
		       int z = ran.nextInt(250);
		       
		       col[i] = new Color(x,y,z);
		    }
		    String yan = "";
		    for(int i = 0;i < 4;i++){
		    	
		    	int x = ran.nextInt(10);
		    	
		    	int x1 = ran.nextInt(10)*5;
		    	int y1 = ran.nextInt(10)*1;
		    	int x2= ran.nextInt(10)*10;
		    	int y2 = ran.nextInt(10)*1;
		    	
		    	//设置字体的颜色
		    	gs.setColor(col[ran.nextInt(10)]);
		    	
		    	
		    	//数字画到图片上
		    	gs.drawString(String.valueOf(x),9*(i+1),14);
		    	
		    	yan += x;
		    	
		    	//画线
		    	gs.drawLine(x1, y1, x2, y2);
		    	
		    	
		    }
		    //清空缓存
		    gs.dispose();
		    
		    HttpSession session = request.getSession();
		    
		    session.setAttribute("yan", yan);
		    
		    //输出图片
		    ImageIO.write(image, "jpeg",os);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

	doGet(request, response);
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -