Search.jsp页面

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
Insert title here

产品检索

产品编号:
  

Show.jsp页面

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
Insert title here
产品编号<%=request.getParameter("id") %>
产品图片">
 

Web.xml配置servlet

 
   
    ProductPhotoShowAction
    ProductPhotoShowAction
    com.city.oa.action.ProductPhotoShowAction
   
     
      driver
      sun.jdbc.odbc.JdbcOdbcDriver
   
   
     
      url
      jdbc:odbc:DB
   
 
 
    ProductPhotoShowAction
    /product/showPhoto.do
 
 

ProductPhotoShowAction类中doPost()方法

String id = request.getParameter("id");
              String sql = "select ProductNO,Photo,PhotoType from Product where ProductNO = ?"; //photo为二进制数据,phototype为图片类型
              String photoType = "";
              String photo = "";
              try {
                     if(!id.trim().equals("")){
                            PreparedStatement ps = con.prepareStatement(sql);
                            ps.setInt(1, Integer.parseInt(id));
                            ResultSet rs = ps.executeQuery();
                            InputStream in = null;
                            ServletOutputStream out = null;
                            if(rs.next()){
                                   photoType = rs.getString("PhotoType");
                                   response.setContentType(photoType);
                                   response.setCharacterEncoding("utf-8");
                                   in = rs.getBinaryStream("Photo");
                                   out = response.getOutputStream();
                                   byte[] data = new byte[200];
                                   int len = 0;
                                   while((len = in.read(data))!=-1){
                                          out.write(data,0,len);
                                   }
                            }
                            in.close();
                            out.flush();
                            out.close();
                           
                     }else
                            response.sendRedirect("search.jsp");
              } catch (Exception e) {
                     // TODO: handle exception
              }

ProductPhotoShowAction类中init()方法

driver = this.getInitParameter("driver");
              url = this.getInitParameter("url");
              try {
                     Class.forName(driver);
                     con = DriverManager.getConnection(url);
              } catch (Exception e) {
                     // TODO: handle exception
              }
 

ProductPhotoShowAction类中destroy()方法

try {
                     if(con!=null&&!con.isClosed())
                            con.close();
              } catch (Exception e) {
                     // TODO: handle exception
              }