package com.servlet;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import java.util.*;
import com.bean.*;
import com.biz.*;
import com.sun.image.codec.jpeg.ImageFormatException;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import java.io.*;
public class ActionServlet extends HttpServlet implements IAction {
/**
*
*/
private static final long serialVersionUID = 1L;
private HttpServletRequest req;
private HttpServletResponse rep;
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// response.setContentType("text/html");
// PrintWriter out = response.getWriter();
// ================================
req = request;
rep = response;
String op = request.getParameter("op");
if (op == null) {
response.sendRedirect("../error.jsp");
return;
}
if (op.equals("save")) {
String result = save();
if (result.equals("ok")) {
findAll();
}
return;
}
if (op.equals("update")) {
String result = update();
if (result.equals("ok")) {
findAll();
}
return;
}
if (op.equals("delById")) {
String result = delById();
if (result.equals("ok")) {
findAll();
}
return;
}
if (op.equals("findAll")) {
findAll();
return;
}
if (op.equals("findById")) {
findById();
return;
}
if (op.equals("showPic")) {
showPic();
return;
}
if (op.equals("init")) {
doinit();
return;
}
if (op.equals("detail")) {
finddetail();
return;
}
// =================================
// out.close();
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to
* post.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException
* if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
public String delById() {
// TODO Auto-generated method stub
EmpBiz ebiz = new EmpBiz();
String eid = req.getParameter("eid");
boolean bl = ebiz.delById(eid);
if (bl) {
return "ok";
}
return null;
}
public String findAll() {
// TODO Auto-generated method stub
HttpSession session = req.getSession();
// =====获取部门数据=================
DepBiz depbiz = new DepBiz();
@SuppressWarnings("unchecked")
List<Dep> lsdep = (List<Dep>) depbiz.findAll();
session.setAttribute("lsdep", lsdep);
// ====================================
String cpage = req.getParameter("cp");
String psize = req.getParameter("ps");
EmpBiz ebiz = new EmpBiz();
PageBean pb = (PageBean) session.getAttribute("pb");
if (pb == null) {
pb = new PageBean();
}
if (psize == null)
psize = "" + pb.getPsize();
if (cpage == null)
cpage = "" + pb.getCpage();
int ps = Integer.parseInt(psize);
if (ps < 1)
ps = 5;
int maxpage = ebiz.findMaxPage(ps);
int cp = Integer.parseInt(cpage);
if (cp < 1)
cp = 1;
if (cp > maxpage)
cp = maxpage;
@SuppressWarnings("unchecked")
List<Emp> lsemp = (List<Emp>) ebiz.findAll(cp, ps);
pb.setCpage(cp);
pb.setMaxpage(maxpage);
pb.setPagelist(lsemp);
pb.setPsize(ps);
session.setAttribute("pb", pb);
try {
rep.sendRedirect("../emplist.jsp");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public String findById() {
// TODO Auto-generated method stub
EmpBiz ebiz = new EmpBiz();
String eid = req.getParameter("eid");
Emp emp = (Emp) ebiz.findById(eid);
HttpSession session = req.getSession();
session.setAttribute("emp", emp);
try {
rep.sendRedirect("../empupdate.jsp");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public String finddetail() {
// TODO Auto-generated method stub
EmpBiz ebiz = new EmpBiz();
String eid = req.getParameter("eid");
Emp emp = (Emp) ebiz.findById(eid);
System.out.println(emp.getPhoto());
HttpSession session = req.getSession();
session.setAttribute("emp", emp);
try {
rep.sendRedirect("../empdetail.jsp");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
/** 控制层保存对象 */
public String save() {
/** 处理上传的对象,包括文件和表单元素 */
// 获取工厂对象
DiskFileItemFactory factory = new DiskFileItemFactory();
// 获取上传对象
ServletFileUpload upload = new ServletFileUpload(factory);
try {
// 处理上传请求
@SuppressWarnings("unchecked")
List<FileItem> lsitem = upload.parseRequest(req);
/** 创建Emp对象 */
Emp emp = new Emp();
for (FileItem item : lsitem) {
/** 表单元素的上传 */
if (item.isFormField()) {
if (item.getFieldName().equals("ename")) {
emp.setEname(item.getString("utf-8"));
}
if (item.getFieldName().equals("sex")) {
emp.setSex(item.getString("utf-8"));
}
if (item.getFieldName().equals("address")) {
emp.setAddress(item.getString("utf-8"));
}
if (item.getFieldName().equals("birthday")) {
emp.setBirthday(item.getString("utf-8"));
}
if (item.getFieldName().equals("depid")) {
emp.setDepid(item.getString("utf-8"));
}
} else {
/** 文件的上传 */
emp.setPhoto(item.get());
}
}
EmpBiz ebiz = new EmpBiz();
boolean bl = ebiz.save(emp);
if (bl) {
return "ok";
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public String showPic() {
// 显示照片
BufferedImage bi = new BufferedImage(200, 90,
BufferedImage.TYPE_INT_RGB);
String eid = req.getParameter("eid");
EmpBiz ebiz = new EmpBiz();
Emp emp = (Emp) ebiz.findById(eid);
byte[] b = emp.getPhoto();
try {
BufferedOutputStream bout = new BufferedOutputStream(
rep.getOutputStream());
bout.write(b);
bout.flush();
JPEGImageEncoder encode = JPEGCodec.createJPEGEncoder(bout);