JSP 서브렛 인증 코드
18646 단어 servlet
1 package net.cmono.javaweb.c3;
2
3 import java.awt.Color;
4 import java.awt.Font;
5 import java.awt.Graphics2D;
6 import java.awt.image.BufferedImage;
7 import java.io.IOException;
8 import java.io.PrintWriter;
9 import java.util.Random;
10
11 import javax.servlet.ServletException;
12 import javax.servlet.ServletOutputStream;
13 import javax.servlet.http.HttpServlet;
14 import javax.servlet.http.HttpServletRequest;
15 import javax.servlet.http.HttpServletResponse;
16
17 import com.sun.corba.se.impl.ior.NewObjectKeyTemplateBase;
18 import com.sun.image.codec.jpeg.JPEGCodec;
19 import com.sun.image.codec.jpeg.JPEGImageEncoder;
20
21 public class IdentityServlet extends HttpServlet {
22
23 /**
24 * Constructor of the object.
25 */
26 public IdentityServlet() {
27 super();
28 }
29
30 /**
31 * Destruction of the servlet. <br>
32 */
33 public void destroy() {
34 super.destroy(); // Just puts "destroy" string in log
35 // Put your code here
36 }
37
38 /**
39 * The doGet method of the servlet. <br>
40 *
41 * This method is called when a form has its tag value method equals to get.
42 *
43 * @param request the request send by the client to the server
44 * @param response the response send by the server to the client
45 * @throws ServletException if an error occurred
46 * @throws IOException if an error occurred
47 */
48 public void doGet(HttpServletRequest request, HttpServletResponse response)
49 throws ServletException, IOException {
50
51 response.setContentType("image/jpeg");
52
53 String randomString = getRandomString();
54 request.getSession(true).setAttribute("randomString",randomString);
55
56 int width = 100;
57 int height = 30;
58
59 Color color = getRandomColor();
60 Color reverse = getReverseColor(color);
61
62 BufferedImage bi = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
63
64 Graphics2D g = bi.createGraphics();
65 g.setFont(new Font(Font.SANS_SERIF,Font.BOLD,16));
66 g.setColor(color);
67 g.fillRect(0,0,width,height);
68 g.setColor(reverse);
69 g.drawString(randomString,18,20);
70
71 for(int i = 0,n = random.nextInt(100); i< n;i++)
72 {
73 g.drawRect(random.nextInt(width),random.nextInt(height),1,1);
74 }
75
76 ServletOutputStream out = response.getOutputStream();
77
78 JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
79
80 encoder.encode(bi);
81
82 out.flush();
83
84 }
85
86 /**
87 * The doPost method of the servlet. <br>
88 *
89 * This method is called when a form has its tag value method equals to post.
90 *
91 * @param request the request send by the client to the server
92 * @param response the response send by the server to the client
93 * @throws ServletException if an error occurred
94 * @throws IOException if an error occurred
95 */
96 public void doPost(HttpServletRequest request, HttpServletResponse response)
97 throws ServletException, IOException {
98
99 }
100
101 /**
102 * Initialization of the servlet. <br>
103 *
104 * @throws ServletException if an error occurs
105 */
106 public void init() throws ServletException {
107 // Put your code here
108 }
109
110 public static final char[] CHARS = {'2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','J','K',
111 'L','M','N','P','Q','R','S','T','U','V','W','X','Y','Z'};
112
113 public static Random random = new Random();
114
115 public static String getRandomString()
116 {
117 StringBuffer buffer = new StringBuffer();
118 for(int i = 0;i < 6;i++)
119 {
120 buffer.append(CHARS[random.nextInt(CHARS.length)]);
121 }
122
123 return buffer.toString();
124 }
125
126 public static Color getRandomColor()
127 {
128 return new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255));
129 }
130
131 public static Color getReverseColor(Color c)
132 {
133 return new Color(255 - c.getRed(),255 - c.getGreen(),255-c.getBlue());
134 }
135
136 }
1 <script type="text/javascript">
2 function reloadImage()
3 {
4 document.getElementById('btn').disabled = true;
5 document.getElementById('identity').src='servlet/IndentityServlet?ts' + new Date().getTime();
6 }
7 </script>
8
9 <img src="servlet/IdentityServlet" id="identity" onload="btn.disabled = false;"
10
11 <input type="button" value=" " onclick="reloadImage()" id="btn" />
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
5분 안에 JSP 배우기Java 생태계는 지난 20년 동안 많이 발전했으며 이는 시장에서 변함없이 인기를 얻고 있음으로 알 수 있습니다. 글쎄, 이것은 다른 프로그래밍 언어 중 하나에 대한 탄탄한 배경이 있거나 새로운 개발자이지만 시간이 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.