Examples of Image Upload and display methods in JSP are described in detail.

Source: Internet
Author: User

Examples of Image Upload and display methods in JSP are described in detail.

This document describes how to upload and display images in JSP. Share it with you for your reference. The details are as follows:

1. Introduction

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

Suppose we are processing image news, then we can create corresponding databases and data table objects. The SQL script of the data table structure to be accessed is as follows:

if exists (select * from dbo.sysobjects where id =object_id(N'[dbo].[picturenews]') andOBJECTPROPERTY(id, N'IsUserTable') = 1)drop table [dbo].[picturenews]GOCREATE TABLE [dbo].[picturenews] (    [id] [int] IDENTITY (1, 1) NOT NULL ,    [image] [image] NULL ,    [content] [varchar] (500) COLLATE Chinese_PRC_CI_AS NULL ,    [detail] [varchar] (5000) COLLATE Chinese_PRC_CI_AS NULL) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]GO

In the picturenews table, the field id is used as the identifier. each row of data is stored, which is automatically increased by 1. Field image
Used to store image information. The data type is "image ".
 
3. store binary images to the database

Create a JSP file. The Code is as follows.

<% @ Page contentType = "text/html; charset = gb2312 "%> <HTML> <HEAD> <TITLE> store images </TITLE> </HEAD> <body> <! -- The following form will pass the data to testimage using the Post method. jsp file --> <form method = post action = "testimage. jsp "> new question: <input type =" text "NAME =" content "> <BR> new picture: <input type = "file" NAME = "image"> <BR> news content: <TEXTAREA name = "txtmail" rows = "15" cols = "90" style = "BORDER-BOTTOM: #000000 1px solid; BORDER-LEFT: #000000 1px solid; BORDER-RIGHT: #000000 1px solid; BORDER-TOP: #000000 1px solid; FONT-SIZE: 9pt; HEIGHT: 200px; WIDTH: 100% "wrap =" physical "> </TEXTAREA> <br> <input type =" submit "> </form> </body> </HTML>

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. * "%> 

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. * "%> 

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

"width = 100 height = 100>
Retrieve the image to be displayed. The id is the id of the image to be retrieved. In this example, we output the first and last image information. The detailed program code is as follows.

<% @ Page contentType = "text/html; charset = gb2312" %> <% @ page import = "java. SQL. * "%> 

The preceding WEB application is successfully debugged in the Windows xp/SQL Server 2000/Apache Tomcat 4.0/Jbuilder environment.

I hope this article will help you with JSP program design.

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.