package zr.zrpower.collengine.web.servlet;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

import zr.zrpower.collectionengine.service.CollectionService;

public class ActionImg
    extends HttpServlet {


  //Initialize global variables
  public void init() throws ServletException {
  }

  //Process the HTTP Get request
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws
      ServletException, IOException {
    try {
      String TableName = request.getParameter("TableName");
      String PrimaryKey = request.getParameter("PrimaryKey");
      String PKID = request.getParameter("PKID");
      String ImgField = request.getParameter("ImgField");
	  CollectionService collMgr = new CollectionService();
      byte[] mImage = collMgr.GetImage(TableName,PrimaryKey,PKID,ImgField);
      if (mImage != null) {
        response.reset();
        response.setContentType("image/jpeg");
        ServletOutputStream op = response.getOutputStream();
        op.write(mImage);
        op.flush();
        op.close();

        collMgr=null;
      }
      else {
        collMgr=null;
        response.sendRedirect("zrsysmanage/images/NoImage.gif");
      }
    }
    catch (Exception ex) {
      response.sendRedirect("zrsysmanage/images/NoImage.gif");
    }

  }

  //Process the HTTP Post request
  public void doPost(HttpServletRequest request, HttpServletResponse response) throws
      ServletException, IOException {
    doGet(request, response);
  }

  //Clean up resources
  public void destroy() {
  }
}
