This article illustrates the uploading and displaying methods of pictures in JSP. Share to everyone for your reference. Specifically as follows:
1. Introduction
database applications, especially web-based database applications, often involve the storage and display of picture information. Usually we use the method is to display the picture in a specific directory, in the database to save the name of the picture, in the JSP set up the corresponding data source, the use of database access technology to process picture information. However, if we want to display the picture dynamically, the above method will not meet the need. We have to store the pictures in the database and then dynamically display the pictures we need by programming them. In practice, we can use the JSP programming mode to realize the image database storage and display.
2, the establishment of background database
Assuming that the processing is a picture news, then we can create the corresponding database and data table objects. The SQL script we want to access the datasheet structure 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]
go
CREATE TABLE [dbo].[ Picturenews] (
[id] [int] IDENTITY (1, 1) not NULL,
[image] [image] null,
[content] [varchar] (+) COLLATE Chinese_prc_ci_as null,
[detail] [varchar] (5000) COLLATE chinese_prc_ci_as NULL
) on [PRIMARY] textimage_on [PR Imary] Go
In table picturenews, the field ID is identified as an automatic increment of 1 for each row of data stored. Field image
Used to store picture information, whose data type is "image."
3. Store binary pictures to database
Create a new JSP file. The code looks like the following.
<%@ page contenttype= "text/html;charset=gb2312"%>
<HTML>
<HEAD>
<TITLE> Storage Pictures </TITLE>
</HEAD>
<body>
<!--the form below will pass the data to the testimage.jsp file using the Post method-->
<form method=post action= "testimage.jsp" >
News title: <input type= "text" name= "content" ><BR>
News Photo: <input type= "file" name= "image" ><BR>
news content: <textarea name= "Txtmail" rows= "" cols= "" 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 a inputimage.jsp file, where the testimage.jsp file is used to store picture data in the database, as shown in the following code:
<%@ page contenttype= "text/html;charset=gb2312"%> <%@ page import= "java.sql.*"%> <%@ page import= " java.util.* "%> <%@ page import=" java.text.* "%> <%@ page import=" java.io.* "%>
4, the Web page dynamic display picture
Next we're going to programmatically take the picture out of the database, and the code looks like this.
<%@ 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 thing to do is to use HTML tags:
Remove the picture you want to display, where ID is the number of the picture you want to remove. In this example we output the first and last picture information, and the detailed program code is shown below.
<%@ page contenttype= "text/html;charset=gb2312%> <%@ page import=" java.sql. * "%>
The above Web application is debugged in Windows Xp/sql Server 2000/apache Tomcat 4.0/jbuilder environment.
I hope this article will help you with the JSP program design.