在jsp中直接显示从数据库中存储的图像

本文介绍如何使用JBuilder2005创建一个简单的Web应用程序,该程序能够从数据库中检索图片并在网页上展示。具体步骤包括创建JSP页面以显示图片链接及员工姓名,同时开发Servlet来处理图片请求。
摘要由CSDN通过智能技术生成
在jbuilder2005中测试通过
1、创建jsp文件
 
<%@ page contentType="text/html; charset=GBK" %>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<%
  int table_num=4; //指定表格每行显示记录的条数
  out.println("<table width='75%' border='1'align='center'>");
  out.println("<tr>");
  for (int i=1;i<5;i++)
  {
  String pic01=String.valueOf(i);
     pic01=""+pic01+"";
     String employeename="name"+pic01;
     //加入表格控制
    out.println("<td>");
    out.println("<div align=/"center/">");
    out.println("<p>");
    out.println("<a href=/"getphoto?photoid="+pic01+"/">");
    out.println("<img border=/"0/"src=/"getphoto?photoid="+pic01+"&ts=AAAAA/" width=/"90/" height=/"120/" align=/"absmiddle/">");
    out.println("</a>");
    out.println("</p>");
    out.println("<p>");
    out.println("<a href=/"inf_ employee _details.jsp?inf_employee_id=pic01/">");
    out.println(employeename);
    out.println("</a>");
    out.println("</p>");
    out.println("</div>");
    out.println("</td>" );
  }
    out.println(" </table>");
%>
</body>
</html>
</html>
2、创建sevlet getphoto.java,
 
package lp;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;
public class getphoto extends HttpServlet {
    private static final String CONTENT_TYPE = "image/jpeg";
    //Initialize global variables
    private String driver_class = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
    private String connect_string ="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=test_db";
    String user="sa";
    String psw="123";
    Connection conn = null;
    /********************************************    *
   定义应用变量
   ******************************************/
     private String SQLString = ""; //定义查询语句
    //Initialize global variables
    public void init() throws ServletException {
        try { Class.forName(driver_class);}
        catch (java.lang.ClassNotFoundException e1) {System.out.println(e1.getMessage() );}
        try{
            conn = DriverManager.getConnection(connect_string,user,psw );
        }
        catch(SQLException e3){System.out.println(e3.getMessage() );}
    }
    //Process the HTTP Get request
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws
            ServletException, IOException {
        response.setContentType(CONTENT_TYPE);
       //在数据库中照片的ID
       String PHOTOID = null;
       try {
           PHOTOID = request.getParameter("photoid");
       }
       catch(Exception e) {
           e.printStackTrace();
       }
       byte [] buf=null;
       //扩展名可以从数据库得到,这里直接指定为JPEG
       String photoname="jpeg";
       try{
               //根据ID查找照片
               String searchSql="SELECT pic_img FROM testimg WHERE pic_id="+PHOTOID;
               Statement stmt = conn.createStatement();
               ResultSet RS_photo = stmt.executeQuery(searchSql);
               //将图片数据读入缓冲区
               if (RS_photo.next()){
                   buf = RS_photo.getBytes(1);
               }else
               {
                   buf = new byte[0];
               }
       }catch (Exception e){
               //throw e;
       }
       //response.setContentType(CONTENT_TYPE);
     //告诉浏览器输出的是图片
     response.setContentType("image/"+photoname);
     //图片输出的输出流
     OutputStream out = response.getOutputStream();
     //将缓冲区的输入输出到页面
     out.write(buf);
     //输入完毕,清楚缓冲
     out.flush();
    }
    //Process the HTTP Post request
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws
            ServletException, IOException {
        doGet(request, response);
    }
    //Clean up resources
    public void destroy() {
      if(conn!=null)//关闭数据库连接
         try {
             conn.close();
         }
         catch (SQLException ex) {
         }

    }
}
web.xml自己配置吧!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值