com.pureinfo.tgirls.sns.servlet.TestSNSEntryServlet.java Source code

Java tutorial

Introduction

Here is the source code for com.pureinfo.tgirls.sns.servlet.TestSNSEntryServlet.java

Source

/**
 * PureInfo TGirls
 * @(#)TestSNSEntryServlet.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.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Random;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;

import com.pureinfo.ark.content.ArkContentHelper;
import com.pureinfo.force.exception.PureException;
import com.pureinfo.tgirls.domain.IUserMgr;
import com.pureinfo.tgirls.model.User;
import com.pureinfo.tgirls.sns.constants.APPConstants;
import com.pureinfo.tgirls.utils.servlet.CookieUtils;

public class TestSNSEntryServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest _req, HttpServletResponse _resp) throws ServletException, IOException {
        System.out.println("3333333333333");
        //this.doPost(_req, _resp);
    }

    @Override
    protected void doPost(HttpServletRequest _req, HttpServletResponse _resp) throws ServletException, IOException {
        System.out.println("==================test entry=====POST==============");

        try {
            String userId = _req.getParameter("id");
            if (StringUtils.isEmpty(userId)) {
                userId = "1";
            }

            System.out.println("----user id----" + userId);

            IUserMgr mgr = (IUserMgr) ArkContentHelper.getContentMgrOf(User.class);
            User u = mgr.getUserByTaobaoId(userId);

            System.out.println("user:::;" + u);

            addCookie(u, _req, _resp);

            Cookie[] cookies = _req.getCookies();

            if (cookies == null) {
                System.out.println("=====cookie is null=======");
            } else {
                for (int i = 0; i < cookies.length; i++) {
                    Cookie cookie = cookies[i];
                    System.out.println("cookie[" + i + "]:[" + cookie.getName() + ":" + cookie.getValue() + "("
                            + cookie.getMaxAge() + ")]");
                }

            }

            int i = new Random().nextInt();
            Cookie c = new Cookie("topsessionid", "abc" + i);

            _resp.addCookie(c);

            System.out.println("========================");

            //            Cookie[] cs = _req.getCookies();
            //             for (int ii = 0; ii < cs.length; ii++) {
            //                 Cookie cc = cs[ii];
            //                 System.out.println("cookie[" + cc.getName() + "]:" + cc.getValue());
            //             }

            //_resp.sendRedirect(_req.getContextPath() + "/index.html");

            RequestDispatcher rd = _req.getRequestDispatcher("/index.html");
            rd.forward(_req, _resp);

            //_req.getSession().setAttribute(ArkHelper.ATTR_LOGIN_USER, u);
            // _resp.sendRedirect(_req.getContextPath());
            //            _req.getCookies()[0].
        } catch (PureException e) {
            // TODO Auto-generated catch block
            e.printStackTrace(System.err);
        }

    }

    private void addCookie(User _loginUser, HttpServletRequest _request, HttpServletResponse _response)
            throws UnsupportedEncodingException {
        Cookie name = new Cookie(CookieUtils.NAME, URLEncoder.encode(_loginUser.getName(), "utf-8"));
        Cookie nickName = new Cookie(CookieUtils.NICK_NAME, URLEncoder.encode(_loginUser.getNickname(), "utf-8"));
        Cookie taobaoId = new Cookie(CookieUtils.TAOBAO_ID, URLEncoder.encode(_loginUser.getTaobaoID(), "utf-8"));
        Cookie img = new Cookie(CookieUtils.HEAD_IMG, URLEncoder.encode(_loginUser.getHeadImg(), "utf-8"));
        Cookie topsession = new Cookie(CookieUtils.TOP_SESSION_ID,
                _request.getParameter(APPConstants.REQ_PARAMETER_SESSION));

        _response.addCookie(name);
        _response.addCookie(nickName);
        _response.addCookie(taobaoId);
        _response.addCookie(img);
        _response.addCookie(topsession);

    }

}