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. Setting up a background database
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[p]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [dbo].[p] GO CREATE 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 pictures to the database
When you start Dreamweaver mx, create a new JSP file. The code looks like the following.
<%@ 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" > <base href= "<%=basePath%>" > <title>my JSP ' inputimage.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 are my page" > <!-- <link rel= "stylesheet" type= "Text/css" href= "Styles.css" > --> <body> <form action= "testimage.jsp" method= "POST" ><br> Title <input name= "Picname" type= "text" ><br> Picture <input name= "pic" type= "file" ><br> <input type= "Submit" Name= "Button1" value= "submitted" ><br> </form> </body> |
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.*"%> <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" > <base href= "<%=basePath%>" > <title>my JSP ' testimage.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 are my page" > <!-- <link rel= "stylesheet" type= "Text/css" href= "Styles.css" > --> <body> <% Request.setcharacterencoding ("gb2312"); Create a statement object String picname=request.getparameter ("Picname"); String pic=request.getparameter ("pic"); Get the title, storage path, and content of the picture you want to display and encode it in Chinese FileInputStream str=new fileinputstream (pic); String sql= "INSERT into P (picname,pic) VALUES (?,?)"; PreparedStatement pstmt=conn.getpreparedstatement (SQL); Pstmt.setstring (1,picname); Pstmt.setbinarystream (2,str,str.available ()); Pstmt.execute (); Storing data in a database Out.println ("Success,you Have Insert an Image successfully"); %> </body> |
4. Dynamic display of pictures on Web pages
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.*"%> <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" > <base href= "<%=basePath%>" > <title>my JSP ' testimageout.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 are my page" > <!-- <link rel= "stylesheet" type= "Text/css" href= "Styles.css" > --> <body> <% int id= integer.parseint (request.getparameter ("Picid")); String sql = "Select pic from P WHERE picid=" +ID; ResultSet rs=conn.getresult (SQL); while (Rs.next ()) { Servletoutputstream sout = Response.getoutputstream (); Output stream of picture output InputStream in = Rs.getbinarystream (1); byte b[] = new byte[0x7a120]; for (int i = In.read (b); I!=-1;) { Sout.write (b); Output the buffer input to the page In.read (b); } Sout.flush (); Input complete, clear buffer Sout.close (); } %> </body> |
Save this file as a testimageout.jsp file. The next thing to do 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" > <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 are my page" > <!-- <link rel= "stylesheet" type= "Text/css" href= "Styles.css" > --> <body> <% String sql = "SELECT * from P"; ResultSet rs=conn.getresult (SQL); while (Rs.next ()) { %> <ccid_file values= "testimageout"%/> "width=" height= ">" <br> <% } Rs.close (); %> </body> |