JSP初級教程之跟我學JSP(八)(2)_JSP教程
教程Tag:暫無Tag,歡迎添加,賺取U幣!
推薦:JSP初級教程之跟我學JSP(七)第七章“備注型”超長文本的操作——Clob類型數(shù)據(jù)的存取 存放 oracle 留言板的正文內(nèi)容,用VARCHAR2()是不行的,VARCHAR2()(可變長度的字符串)只能存4000字節(jié),也就是2000個漢字,這也太少了啊,查一下 數(shù)據(jù)庫 類型的資料,發(fā)現(xiàn)有這么幾個類型: LONG,2G
out.print("<script>");
out.print("alert(’操作成功!’);");
out.print("window.location.href=’upphoto.htm’;");
out.print("</script>");
}//if
}//try
catch(Exception e)
{out.print(e);}
%>
</body>
</html>
----------------------------------------------------------------------------
upphoto.jsp對圖片進行存入數(shù)據(jù)庫操作。注意需要將得到的文件的路徑改變一下格式:c:\d\e.jpg改成c:\\d\e.jpg
------------------------------showphoto.jsp---------------------------------
<%@ page contentType="text/html;charset=gb2312"%>
<html>
<head>
<title>顯示圖片</title>
</head>
<body>
<%
String id=request.getParameter("vid");
%>
<table>
<tr>
<td colspan="3"> <img border="1" src="http://ringz/photo?id=<%=id%>"></td>
</tr>
</table>
</body>
</html>
---------------------------------------------------------------------------
showphoto.jsp的這句是關(guān)鍵:src="http://ringz/photo?id=<%=id%>",它說明調(diào)用了一個Servlet,這個Servlet的名字叫photo,而且需要給它傳一個值(id)。下面看這個Servlet的代碼:
---------------------------PhotoServlet.java------------------------------
package ringz.servlet;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.lang.*;
import java.sql.*;
public class PhotoServlet extends HttpServlet//和javabean一樣是“類”,所以類名同樣要和文件名一致
{
private String CLASSFORNAME = "oracle.jdbc.driver.OracleDriver";
private String SERVANDDB = "jdbc:oracle:thin:name/password@ringz:1521:rock";
Connection con = null;
PreparedStatement psmt = null;
ResultSet rs = null;
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
int id = Integer.parseInt(request.getParameter("id"));
try
{
Class.forName(CLASSFORNAME);
con=DriverManager.getConnection(SERVANDDB);
con.setAutoCommit(false);
String sql = "select * from clb where id="+id;
psmt = con.prepareStatement(sql);
rs = psmt.executeQuery();
if (rs.next())
{
Blob bb = rs.getBlob("blob");
InputStream instream = bb.getBinaryStream();
response.setContentType("image/*");
OutputStream outStream = response.getOutputStream();
byte[] bytes = new byte[1024];
int i=0;
while ( (i = instream.read(bytes)) != -1)
{
outStream.write(bytes, 0, i);
}
instream.close();
outStream.close();
outStream = null;
con.commit();
rs.close();
psmt.close();
con.close();
}//if
}//try
catch (Exception ex)
{}
}//doGet
}
分享:JSP初級教程之跟我學JSP(六)第六章 jsp 實現(xiàn)畫柱狀統(tǒng)計圖 這一節(jié)的內(nèi)容是用jsp生成一個統(tǒng)計——統(tǒng)計一年內(nèi)每個月完成的報修任務(wù)量。 Java里和畫圖有關(guān)的是java.awt包,由于我構(gòu)想的圖只是由矩形組成,那么用到的方法也就這么幾個:fillRect,drawRect,setColor,setFont,drawString。
相關(guān)JSP教程:
- jsp response.sendRedirect不跳轉(zhuǎn)的原因分析及解決
- JSP指令元素(page指令/include指令/taglib指令)復習整理
- JSP腳本元素和注釋復習總結(jié)示例
- JSP FusionCharts Free顯示圖表 具體實現(xiàn)
- 網(wǎng)頁模板:關(guān)于jsp頁面使用jstl的異常分析
- JSP頁面中文傳遞參數(shù)使用escape編碼
- 基于jsp:included的使用與jsp:param亂碼的解決方法
- Java Web項目中連接Access數(shù)據(jù)庫的配置方法
- JDBC連接Access數(shù)據(jù)庫的幾種方式介紹
- 網(wǎng)站圖片路徑的問題:絕對路徑/虛擬路徑
- (jsp/html)網(wǎng)頁上嵌入播放器(常用播放器代碼整理)
- jsp下顯示中文文件名及絕對路徑下的圖片解決方法
- 相關(guān)鏈接:
- 教程說明:
JSP教程-JSP初級教程之跟我學JSP(八)(2)
。