Dynamic Display of Binary Images Using JSP + servlet (excerpt)

Source: Internet
Author: User
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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.