Use JSP to store and display database images

Source: Internet
Author: User

1. Preface

Database applications, especially WEB-based database applications, usually involve the storage and display of image information.

The common method is to store the image to be displayed in a specific directory, store the name of the image in the database, and create the corresponding data source in JSP, use Database Access technology to process image information. However, if we want to dynamically display images, the above method cannot meet our needs. We must store images in the database and dynamically display the images we need through programming. In practice, the JSP programming mode can be used to store and display image databases.

2. Create a background database

if exists (select * from dbo.sysobjectswhere id = object_id(N'[dbo].[p]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)drop table [dbo].[p]GOCREATE TABLE [dbo].[p] (  [picid] [int] IDENTITY (1, 1) NOT NULL ,  [picname] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,  [pic] [image] NULL) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]GO

3. store binary images to the database

Start Dreamweaver MX and create a new JSP file. The Code is as follows.

<%@ Page contentType = "text/html; charset = gb2312" % ><% String path = request. getContextPath (); String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/"; % ><! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN" >

Save this file as an InputImage. jsp file. The testimage. jsp file is used to store image data to the database. The specific code is as follows:

<%@ Page contentType = "text/html; charset = gb2312" % ><%@ page import = "java. SQL. * "%> <%@ page import =" java. util. * "%> <%@ page import =" java. text. * "%> <%@ page import =" java. io. * "%> <jsp: useBean id =" conn "scope =" page "class =" dbconn. DBResult "/> <% String path = request. getContextPath (); String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/"; % ><! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN" >

4. dynamically display images on the webpage

Next, we will program to retrieve images from the database. The Code is as follows.

<%@ Page contentType = "text/html; charset = gb2312" % ><%@ page import = "java. SQL. * "%> <%@ page import =" java. util. * "%> <%@ page import =" java. text. * "%> <%@ page import =" java. io. * "%> <jsp: useBean id =" conn "scope =" page "class =" dbconn. DBResult "/> <% String path = request. getContextPath (); String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/"; % ><! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN" >

Save this file as a testimageout. jsp file. The next step is to use HTML tags:

﹤%@ page contentType="text/html;charset=gb2312"%﹥﹤%@ page import="java.sql.*" %﹥﹤%@ page import="java.util.*"%﹥﹤%@ page import="java.text.*"%﹥﹤%@ page import="java.io.*"%﹥﹤jsp:useBean id="conn" scope="page" class="dbconn.DBResult"/﹥﹤%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%﹥﹤!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"﹥﹤html﹥ ﹤head﹥﹤base href="﹤%=basePath%﹥"﹥﹤title﹥My JSP 'lookpic.jsp' starting page﹤/title﹥  ﹤meta http-equiv="pragma" content="no-cache"﹥  ﹤meta http-equiv="cache-control" content="no-cache"﹥  ﹤meta http-equiv="expires" content="0"﹥  ﹤meta http-equiv="keywords" content="keyword1,keyword2,keyword3"﹥  ﹤meta http-equiv="description" content="This is my page"﹥  ﹤!--  ﹤link rel="stylesheet" type="text/css" href="styles.css"﹥  --﹥ ﹤/head﹥ ﹤body﹥ ﹤% String sql = "select * from p"; ResultSet rs=conn.getResult(sql);  while(rs.next())  { %﹥﹤ccid_file values="testimageout" % /﹥" width="100" height="100"﹥ ﹤br﹥ ﹤% } rs.close(); %﹥﹤/body﹥﹤/html﹥

  1. JDBC and JSP simulate the MVC three-layer architecture
  2. In-depth analysis of JSP technology Advantages and Disadvantages
  3. Analysis on the functions of Action attributes in JSP

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.