Java tutorial
/** * PureInfo TGirls * @(#)SearchUserPicsServlet.java 1.0 2009-3-12 * * Copyright(c) 2004-2005, PureInfo Information Technology Corp. Ltd. * All rights reserved, see the license file. * * www.pureinfo.com.cn */ package com.pureinfo.tgirls.servlet; import java.io.IOException; import java.net.URLDecoder; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.lang.StringUtils; import org.apache.log4j.Logger; import com.pureinfo.ark.content.ArkContentHelper; import com.pureinfo.tgirls.domain.IPhotoMgr; import com.pureinfo.tgirls.domain.IUserMgr; import com.pureinfo.tgirls.json.model.JsonBase; import com.pureinfo.tgirls.json.model.JsonBase.ErrorCode; import com.pureinfo.tgirls.model.Photo; import com.pureinfo.tgirls.model.User; public class SearchUserPicsServlet extends HttpServlet { /** * Comment for <code>serialVersionUID</code> */ private static final long serialVersionUID = 1L; private Logger logger = Logger.getLogger(this.getClass().getName()); IUserMgr mgr = null;// IPhotoMgr pmgr = null; @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String name = URLDecoder.decode(request.getParameter("name"), "utf-8"); JsonBase result = new JsonBase(); response.setContentType("text/json; charset=utf-8"); logger.debug("to search " + name + " pics"); if (StringUtils.isEmpty(name)) { result.setErrorCode(ErrorCode.ERROR.getCode()); result.setErrorMsg("??"); response.getWriter().write(result.toString()); return; } try { if (mgr == null) { mgr = (IUserMgr) ArkContentHelper.getContentMgrOf(User.class); } User u = mgr.lookupByName(name); if (u == null) { throw new Exception("."); } if (pmgr == null) { pmgr = (IPhotoMgr) ArkContentHelper.getContentMgrOf(Photo.class); } List<Photo> pics = pmgr.getUserUploadPublicPics(u.getTaobaoID()); if (pics == null || pics.isEmpty()) { throw new Exception(""); } result.put("picId", pics.get(0).getId()); response.getWriter().write(result.toString()); return; } catch (Exception e) { result.setErrorCode(ErrorCode.ERROR.getCode()); if (StringUtils.isNotEmpty(e.getMessage())) { result.setErrorMsg(e.getMessage()); } else { result.setErrorMsg(""); } response.getWriter().write(result.toString()); return; } } }