Java tutorial
/** * PureInfo TGirls * @(#)ContinueServlet.java 1.0 2009-4-11 * * 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.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.User; import com.pureinfo.tgirls.utils.servlet.CookieUtils; public class ContinueServlet extends HttpServlet { /** * Comment for <code>serialVersionUID</code> */ private static final long serialVersionUID = 1L; private Logger logger = Logger.getLogger(this.getClass().getName()); public static final String VIEWD_PIC_IDS = "viewdPicIds"; @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String type = request.getParameter("pictype"); String id = request.getParameter("picId"); if (StringUtils.isEmpty(id)) { id = "-1"; } int picId = -1; try { picId = Integer.parseInt(id); } catch (Exception e) { logger.error("error parse int", e); picId = -1; } logger.debug("to get pic with type[" + type + "] id[" + picId + "]"); response.setContentType("text/json; charset=utf-8"); JsonBase json = new JsonBase(); try { User loginUser = CookieUtils.getLoginUser(request, response); if (loginUser == null) { // response.sendError(HttpServletResponse.SC_BAD_REQUEST); // return; } Photo p = null; if (picId > 0) { p = GetPic.get(type, picId); } else { try { List<String> viewdPicIds = new ArrayList<String>();// (List<Integer>) String ids = (String) CookieUtils.getRequestCookieValue(request, 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(type, viewdPicIds); ids = GetPic.idListToString(viewdPicIds); logger.debug("after:" + ids); request.getSession().setAttribute(VIEWD_PIC_IDS + loginUser.getTaobaoID(), ids); //Cookie c = new Cookie(VIEWD_PIC_IDS + loginUser.getTaobaoID(), ids); //response.addCookie(c); } catch (Exception e) { logger.error("error when record view ids.", e); p = GetPic.getRandomPic(type); } } json.put("pic", new JSONObject(p)); String[] moneyKey = MakeMoneyUtil.getMoney(request, response); if (moneyKey != null) { json.put("money", moneyKey[0]); json.put("moneyValidateKey", moneyKey[1]); request.getSession().setAttribute(MakeMoneyUtil.VALIDATE_KEY, moneyKey[1]); } if (loginUser != null) { List<JSONObject> infolist = GetInformationsServlet.getInformations(loginUser.getTaobaoID()); if (infolist != null) { json.put("informations", infolist); } } } catch (JSONException e) { e.printStackTrace(System.err); } //logger.debug("result:" + json.toString()); response.getWriter().write(json.toString()); return; } }