Java tutorial
/** * PureInfo TGirls * @(#)SingleInitServlet.java 1.0 2009-4-7 * * Copyright(c) 2004-2005, PureInfo Information Technology Corp. Ltd. * All rights reserved, see the license file. * * www.pureinfo.com.cn */ package com.pureinfo.tgirls.sns.servlet; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; 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 org.json.JSONException; import org.json.JSONObject; import com.pureinfo.tgirls.json.model.JsonBase; import com.pureinfo.tgirls.model.Photo; import com.pureinfo.tgirls.model.RandomEvent; import com.pureinfo.tgirls.model.User; import com.pureinfo.tgirls.servlet.ContinueServlet; import com.pureinfo.tgirls.servlet.GetInformationsServlet; import com.pureinfo.tgirls.servlet.GetPic; import com.pureinfo.tgirls.servlet.WelcomeMsgUtils; import com.pureinfo.tgirls.utils.servlet.CookieUtils; public class SingleInitServlet extends HttpServlet { private Logger logger = Logger.getLogger(this.getClass().getName()); @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String picId = request.getParameter("picId"); if (StringUtils.isEmpty(picId)) { picId = "-1"; } int npicId = -1; try { npicId = Integer.parseInt(picId); } catch (Exception e) { logger.error("int parse error.", e); npicId = -1; } JsonBase result = new JsonBase(); response.setContentType("text/json; charset=utf-8"); logger.info("single init with pic[" + npicId + "]"); User loginUser = CookieUtils.getLoginUser(request, response); logger.debug("loginuser:" + loginUser); if (loginUser == null) { response.sendRedirect(request.getContextPath() + "/need-login.html"); return; } try { Photo p;// = GetPic.get("", npicId); try { if (npicId > 0) { p = GetPic.get("", npicId); } else { List<String> viewdPicIds = new ArrayList<String>();// (List<Integer>) String ids = (String) CookieUtils.getRequestCookieValue(request, ContinueServlet.VIEWD_PIC_IDS + loginUser.getTaobaoID()); logger.debug("the ids:" + ids); if (StringUtils.isNotEmpty(ids)) { viewdPicIds = new ArrayList<String>(Arrays.asList(ids.split(","))); } logger.debug("before:" + viewdPicIds.toString()); p = GetPic.getPic("", viewdPicIds); ids = GetPic.idListToString(viewdPicIds); logger.debug("after:" + ids); request.getSession().setAttribute(ContinueServlet.VIEWD_PIC_IDS + loginUser.getTaobaoID(), ids); } } catch (Exception e) { logger.error("error when record view ids.", e); p = GetPic.get("", npicId); } logger.debug("the pic:" + p); if (p != null) { result.put("pic", new JSONObject(p)); } List<JSONObject> infolist = GetInformationsServlet.getInformations(loginUser.getTaobaoID()); if (infolist != null) { result.put("informations", infolist); } result.put("uinfo", new JSONObject(loginUser)); try { RandomEvent wel = WelcomeMsgUtils.doGetWelcomEvent(request, response); if (wel != null) { result.put("event", new JSONObject(wel)); } } catch (Exception e) { logger.error(e); } } catch (JSONException e) { logger.error("json error", e); } //logger.debug("result:" + result.toString()); response.getWriter().write(result.toString()); return; } @Override protected void doPost(HttpServletRequest _req, HttpServletResponse _resp) throws ServletException, IOException { this.doGet(_req, _resp); } }