Import java. Io .*;
Import java. util .*;
Import com.sun.image.codec.jpeg .*;
Import javax. servlet .*;
Import javax. servlet. http .*;
Import java. AWT .*;
Import java. AWT. image .*;
Public class validatecode extends httpservlet {
Private font mfont = new font ("", Font. Plain, 12); // set the font
// Process post
Public void dopost (httpservletrequest request, httpservletresponse response)
Throws servletexception, ioexception {
Doget (request, response );
}
Public void doget (httpservletrequest request, httpservletresponse response)
Throws servletexception, ioexception {
// Obtain a random number of 1000-9999
String S = "";
Int intcount = 0;
Intcount = (new random (). nextint (9999 );//
If (intcount <1000) intcount + = 1000;
S = intcount + "";
// Save the input session for comparison with the user input.
// Clear the session after comparison.
Httpsession session = request. getsession (true );
Session. setattribute ("validatecode", S );
Response. setcontenttype ("image/GIF ");
Servletoutputstream out = response. getoutputstream ();
Bufferedimage image = new bufferedimage (35,14, bufferedimage. type_int_rgb );
Graphics gra = image. getgraphics ();
// Set the background color
Gra. setcolor (color. Yellow );
Gra. fillrect );
// Set the font color
Gra. setcolor (color. Black );
Gra. setfont (mfont );
// Output number
Char C;
For (INT I = 0; I <4; I ++ ){
C = S. charat (I );
Gra. drawstring (C + "", I * 7 + 4, 11); // 7 indicates the width, and 11 indicates the upper and lower heights.
}
Required imageencoder encoder = required codec. createjpegencoder (out );
Encoder. encode (image );
Out. Close ();
}
}
The image processing package of Java requires a graphical environment, but the graphic environment is not started on Linux. Therefore, this error is reported because the server (X11 window server using ': 100') in the graphic environment cannot be found. By specifying the Java-djava. AWT. Headless = true parameter, you can avoid finding a graphical environment in Java 2D.
You can either try it like this. In the servlet, write the following statement at the beginning:
System. setproperty ("Java. AWT. Headless", "true ");
Java. AWT. Headless = true
Take Tomcat as an Example
You can add:
Catalina_opts = "...-djava. AWT. Headless = true"
Others can also look at the startup script. You can add this parameter.