package servlet.login;
import javax.imageio.ImageIO;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
@WebServlet("/yzmServlet")
public class yzmServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int width = 60;
int high = 20;
System.out.println("3");
BufferedImage image = new BufferedImage(width, high, BufferedImage.TYPE_INT_RGB);
Graphics gra = image.getGraphics();
gra.setColor(Color.pink);
gra.fillRect(0, 0, width, high);
gra.setColor(Color.blue);
gra.drawRect(0, 0, width - 1, high - 1);
String str = "0123456789";
// ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
Random r = new Random();
StringBuilder sb = new StringBuilder();
for (int i = 1; i <= 4; i++) {
int a = r.nextInt(str.length());
char ch = str.charAt(a);
sb.append(ch);
gra.drawString(ch + "", width / 5 * i, high / 2);
}
String s = sb.toString();
gra.setColor(Color.green);
for (int j = 0; j < 5; j++) {
int x1 = r.nextInt(width);
int x2 = r.nextInt(width);
int y1 = r.nextInt(high);
int y2 = r.nextInt(high);
gra.drawLine(x1, y1, x2, y2);
}
ImageIO.write(image, "jpg", response.getOutputStream());
System.out.println("5");
// Cookie cookie = new Cookie("yzml", s);
// response.addCookie(cookie);
ServletContext servletContext = this.getServletContext();
servletContext.setAttribute("yzml",s);
// request.setAttribute("yzml",s);
// request.getRequestDispatcher("http://localhost:8080/No2/a.html").forward(request,response);
// return;
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
|
|