| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 793 人关注过本帖
标题:jsp指令问题
只看楼主 加入收藏
女孩天下
Rank: 1
等 级:新手上路
帖 子:28
专家分:0
注 册:2008-12-7
结帖率:100%
收藏
 问题点数:0 回复次数:6 
jsp指令问题
我用<jsp:include page="" flash="">指令把一个把一个从数据库中取图片的页面包含在其中,结果只显示去图片页面。这是为什么呢?谁能指点一下
搜索更多相关主题的帖子: jsp 指令 
2010-05-08 22:07
流星雨
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:JAVA风暴
等 级:版主
威 望:43
帖 子:1854
专家分:1868
注 册:2004-5-30
收藏
得分:0 
可以利用JSP的编程模式来实现图片的数据库存储和显示。

感谢你们带我找到星空下美丽神话,无论经历多少苦痛也不放弃的梦;插上希望翅膀乘风我和你们飞翔,飞过海天尽头携手把梦想实现.....
2010-05-09 00:17
流星雨
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:JAVA风暴
等 级:版主
威 望:43
帖 子:1854
专家分:1868
注 册:2004-5-30
收藏
得分:0 
给你看个例子

建立后台数据库

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

向数据库存储二进制图片
启动Dreamweaver MX后,新建一个JSP文件。其代码如下所示。
<%@ 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">
<html>
<head>
   <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 is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
   <form action="testimage.jsp" method="POST"><br>
   题目<input name="picname" type="text"><br>
   图片<input name="pic" type="file"><br>
   <input type="Submit" name="button1" value="提交"><br>
</form>
</body>
</html>

将此文件保存为InputImage.jsp文件,其中testimage.jsp文件是用来将图片数据存入数据库的,具体代码如下所示:

<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*" %>
<%@ page import="java.util.*"%>
<%@ page import="java.text.*"%>
<%@ page import="*"%>
<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 '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 is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
   request.setCharacterEncoding("gb2312");
//建立Statement对象
String picname=request.getParameter("picname");
String pic=request.getParameter("pic");
//获得所要显示图片的标题、存储路径、内容,并进行中文编码
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();
//将数据存入数据库
out.println("Success,You Have Insert an Image Successfully");
%>
</body>
</html>

网页中动态显示图片
接下来我们要编程从数据库中取出图片,其代码如下所示。

<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*" %>
<%@ page import="java.util.*"%>
<%@ page import="java.text.*"%>
<%@ page import="*"%>
<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 '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 is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<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();
       //图片输出的输出流
       InputStream in = rs.getBinaryStream(1);
       byte b[] = new byte[0x7a120];
       for(int i = in.read(b); i != -1;)
       {
         sout.write(b);
         //将缓冲区的输入输出到页面
         in.read(b);
       }
       sout.flush();
       //输入完毕,清除缓冲
       sout.close();
}
   %>
</body>
</html>

将此文件保存为testimageout.jsp文件。下一步要做的工作就是使用HTML标记:

<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*" %>
<%@ page import="java.util.*"%>
<%@ page import="java.text.*"%>
<%@ page import="*"%>
<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>

感谢你们带我找到星空下美丽神话,无论经历多少苦痛也不放弃的梦;插上希望翅膀乘风我和你们飞翔,飞过海天尽头携手把梦想实现.....
2010-05-09 00:18
HeiN
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:53
专家分:193
注 册:2010-3-30
收藏
得分:0 
顶顶
2010-05-10 03:51
女孩天下
Rank: 1
等 级:新手上路
帖 子:28
专家分:0
注 册:2008-12-7
收藏
得分:0 
回复 3楼 流星雨
这个例子很好谢谢啊!可是我的问题不是存取,而是把一个取图片的文件用<jsp: include page="" flash="">指令包含在其他文件里面是不能正常显示。而单个的文件都能显示的!
2010-05-10 13:50
aina
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:91
专家分:117
注 册:2009-9-22
收藏
得分:0 
不错。谢谢了
2010-05-10 21:59
快速回复:jsp指令问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.024678 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved