Public void doget (httpservletrequest request, httpservletresponse response)
Throws servletexception, ioexception {
// ID of the photo in the database
String photoid = NULL;
Try {
Photoid = request. getparameter ("photoid ");
}
Catch (exception e ){
E. printstacktrace ();
}
// Connect to the database, custom database connection pool management class
Dbconnectionmanager connmgr;
Connmgr = dbconnectionmanager. getinstance ();
Connection conn = connmgr. getconnection ("comdb"); // defined in the property File
// Buffer used to store photo data
Byte [] Buf = NULL;
// The extension can be obtained from the database. This parameter is directly specified as JPEG.
String photoname = "Jpeg ";
Try {
// Search for photos by ID
String searchsql = "select photo from employee where id =" + photoid;
Statement stmt = conn. createstatement ();
Resultset rs_photo = stmt.exe cutequery (searchsql );
// Read image data into the buffer zone
If (rs_photo.next ()){
Buf = rs_photo.getbytes (1 );
} Else
{
Buf = new byte [0];
}
} Catch (exception e ){
// Throw E;
}
Finally {
Connmgr. freeconnection ("comdb", Conn );
}
// Response. setcontenttype (content_type );
// Tell the browser that the image is output
Response. setcontenttype ("image/" + photoname );
// Output stream of image output
Outputstream out = response. getoutputstream ();
// Output the buffer input to the page
Out. Write (BUF );
// After the input is complete, clear the buffer
Out. Flush ();
}
/** Clean up resources */
Public void destroy (){
}
}
The compiled servlet getphoto. class will also be automatically placed under the class directory under the WEB-INF of the project file.
2.3 JSP allows you to browse database image and text information
After successfully establishing the servlet, the next step is to mark the original HTML:
replace it with the servlet tag. You can write the following mark in HTML or JSP pages.
Xxx indicates the image ID and AAAAA indicates the timestamp to prevent the image from being refreshed. If you want to browse all the text information in the database, you can add appropriate loop control. In the example web application in this article, the author adds a simple table control to modify the output graphic information.