eclips上在servlet中获取数据库中所有的信息并将其显示在页面上
1个回答
展开全部
您好,//import语句就不写了,反正现在的IDE这么先进,这难不了大家的
public class GetA000_photoServlet extends HttpServlet {
private static final String CONTENT_TYPE = "image/jpeg";
/**Initialize global variables*/
public void init() throws ServletException{
}
/**Process the HTTP Get request*/
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException {
//从页面中取得传进来的参数personid
String personID = request.getParameter("personID");
//DataSourceManager 为取数库连接的类
DataSourceManager db = null;
Connection conn = null;
ResultSet rs = null;
PreparedStatement pstmt = null;
//用来存储照片数据的缓冲区
byte [] buf=null;
String sql = "select a.photo from Ftable a where a.PERSONID=?";
try{
db = new DataSourceManager();
if(db != null){
conn = db.getConnection();
if(conn != null){
pstmt = conn.prepareStatement(sql);
pstmt.setString(1,personID);
rs = pstmt.executeQuery();
if(rs.next()){
//buf = rs.getBytes(1); //在网上查到的处理代码是这样处理的,但我这样做后却 解决不了问题,我同学以为是数据的问题,然后认为只要把结果 rs.getBytes(1)写到输出流,但结果确是令人非常失望,这令我不 理解.后来作了如下的修改就基本解决了.(我的图片是以Blob类 型存Oracle中的)
Blob blocco = (Blob)rs.getObject("A000_PHOTO");
int length = (int)blocco.length();
buf = blocco.getBytes(1, length);
}else{
buf = new byte[0];
}
}
}
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(rs != null)
rs.close();
if(pstmt != null)
pstmt.close();
if(conn != null)
conn.close();
}catch(Exception e){
e.printStackTrace();
}
}if(buf == null)buf = new byte[0];
//告诉浏览器输出的是图片
response.setContentType(CONTENT_TYPE);
//图片输出的输出流
OutputStream out = response.getOutputStream();
//将缓冲区的输入输出到页面
out.write(buf);
//输入完毕,清楚缓冲
out.flush();
}
/**Clean up resources*/
public void destroy() {
}
}
//页面调用
<img border="0" src="<%=request.getContextPath()%>/getPhotoServlet/getPhoto?personID=16873&ts=aaaa" width="200" height="250">
TS是一个随机字段用于防止数据库更新后,页面不能显示更新后的图片.我传入的是一个时间戳以保证每次请求的地址都是不一样的.如果你的应用布署带有项目名的话,如http://localhost:8080/myServer/...,请不要忘了把<%=request.getContextPath()%>忘了写上.
//web.xml配置
<servlet>
<servlet-name>getPhotoServlet</servlet-name>
<servlet-class>com.szdc.web.gbkc.GetA000_photoServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>getPhotoServlet</servlet-name>
<url-pattern>/getPhotoServlet/getPhoto</url-pattern>
</servlet-mapping>
public class GetA000_photoServlet extends HttpServlet {
private static final String CONTENT_TYPE = "image/jpeg";
/**Initialize global variables*/
public void init() throws ServletException{
}
/**Process the HTTP Get request*/
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException {
//从页面中取得传进来的参数personid
String personID = request.getParameter("personID");
//DataSourceManager 为取数库连接的类
DataSourceManager db = null;
Connection conn = null;
ResultSet rs = null;
PreparedStatement pstmt = null;
//用来存储照片数据的缓冲区
byte [] buf=null;
String sql = "select a.photo from Ftable a where a.PERSONID=?";
try{
db = new DataSourceManager();
if(db != null){
conn = db.getConnection();
if(conn != null){
pstmt = conn.prepareStatement(sql);
pstmt.setString(1,personID);
rs = pstmt.executeQuery();
if(rs.next()){
//buf = rs.getBytes(1); //在网上查到的处理代码是这样处理的,但我这样做后却 解决不了问题,我同学以为是数据的问题,然后认为只要把结果 rs.getBytes(1)写到输出流,但结果确是令人非常失望,这令我不 理解.后来作了如下的修改就基本解决了.(我的图片是以Blob类 型存Oracle中的)
Blob blocco = (Blob)rs.getObject("A000_PHOTO");
int length = (int)blocco.length();
buf = blocco.getBytes(1, length);
}else{
buf = new byte[0];
}
}
}
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(rs != null)
rs.close();
if(pstmt != null)
pstmt.close();
if(conn != null)
conn.close();
}catch(Exception e){
e.printStackTrace();
}
}if(buf == null)buf = new byte[0];
//告诉浏览器输出的是图片
response.setContentType(CONTENT_TYPE);
//图片输出的输出流
OutputStream out = response.getOutputStream();
//将缓冲区的输入输出到页面
out.write(buf);
//输入完毕,清楚缓冲
out.flush();
}
/**Clean up resources*/
public void destroy() {
}
}
//页面调用
<img border="0" src="<%=request.getContextPath()%>/getPhotoServlet/getPhoto?personID=16873&ts=aaaa" width="200" height="250">
TS是一个随机字段用于防止数据库更新后,页面不能显示更新后的图片.我传入的是一个时间戳以保证每次请求的地址都是不一样的.如果你的应用布署带有项目名的话,如http://localhost:8080/myServer/...,请不要忘了把<%=request.getContextPath()%>忘了写上.
//web.xml配置
<servlet>
<servlet-name>getPhotoServlet</servlet-name>
<servlet-class>com.szdc.web.gbkc.GetA000_photoServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>getPhotoServlet</servlet-name>
<url-pattern>/getPhotoServlet/getPhoto</url-pattern>
</servlet-mapping>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |