Display Database BLOB Field Image instances in JSP

Source: Internet
Author: User

Step 1: Create a New serverlet to get the image stream. The Code is as follows:

Public class imageservlet extends httpservlet {

Public imageservlet (){
Super ();
}

Public void destroy (){
Super. Destroy (); // just puts "Destroy" string in log
// Put your code here
}

Public void doget (httpservletrequest request, httpservletresponse response)
Throws servletexception, ioexception {

Dopost (request, response );
}

Public void dopost (httpservletrequest request, httpservletresponse response)
Throws servletexception, ioexception {

Response. setcontenttype ("text/html ");
// Printwriter out = response. getwriter ();
Videoservice service = new videoservice ();
String id = request. getparameter ("ID ");
Byte [] imagedata = service. getimagedatabyid (ID );
If (imagedata! = NULL)
{
Response. setcontenttype ("image/PNG ");
Outputstream stream = response. getoutputstream ();
Stream. Write (imagedata );
Stream. Flush ();
Stream. Close ();
}
Else
{
Response. setcontenttype ("text ");
Response. getwriter (). Write ("attribute byarr not found ");
}

}

Public void Init () throws servletexception {
// Put your code here
}

}


Step 2: the JSP code is as follows:

<HTML>
<Head>
</Head>

<Body>
<%
Videoservice service = new videoservice ();
Arraylist <videobean> datalst = service. getvidelistinfobytype ("100", "1", "5 ");
If (datalst = NULL ){
System. Out. println ("datalst is null ...");
} Else {
For (INT I = 0; I <datalst. Size (); I ++ ){
%>
<% = Datalst. Get (I). getname () %> <br/>
"> <br/>
<%
System. Out. println ("name:" + datalst. Get (I). getname ());
}
}
%>

</Body>
</Html>

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.