Package demo.servlet

Source Code of demo.servlet.HomeServlet

package demo.servlet;

import java.io.IOException;

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

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

import com.renren.api.client.RenrenApiClient;
import com.renren.api.client.RenrenApiConfig;
import com.renren.api.client.param.impl.SessionKey;

/**
* Servlet implementation class HomeServlet
*/
public class HomeServlet extends HttpServlet {
  private static final long serialVersionUID = 1L;
      
    /**
     * @see HttpServlet#HttpServlet()
     */
    public HomeServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

  /**
   * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
   */
  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
      //判断是否授权过
      String isAuth=(String)request.getParameter("xn_sig_added");
      if(isAuth.equals("0")){
          response.sendRedirect("auth");
          return;
      }
      String sessionKey = request.getParameter("xn_sig_session_key");
        String renrenUserId = request.getParameter("xn_sig_user");
        if (sessionKey != null && renrenUserId != null) {
            request.getSession().setAttribute("session_key", sessionKey);
           
            RenrenApiClient apiClient = RenrenApiClient.getInstance();
            JSONArray userInfo = apiClient.getUserService().getInfo(renrenUserId, "name,headurl",new SessionKey(sessionKey));
            if (userInfo != null && userInfo.size() > 0) {
                JSONObject currentUser = (JSONObject) userInfo.get(0);
                if (currentUser != null) {
                    String userName = (String) currentUser.get("name");
                    String userHead = (String) currentUser.get("headurl");
                    request.getSession().setAttribute("userName", userName);
                    request.getSession().setAttribute("userHead", userHead);
                }
            }
            //获取好友列表数据
            JSONArray friendsList = apiClient.getFriendsService().getFriends(1, 30,new SessionKey(sessionKey));//最多取30个好友
            //获得安装了该应用的列表
            JSONArray appuserList=apiClient.getFriendsService().getAppUsers("tinyurl",new SessionKey(sessionKey));
            request.setAttribute("friendsList", friendsList);
            request.setAttribute("appUsersList", appuserList);
        }
        request.setAttribute("appId", RenrenApiConfig.renrenAppID);
      RequestDispatcher welcomeDispatcher = request.getRequestDispatcher("/views/home.jsp");
        welcomeDispatcher.forward(request, response);
  }

  /**
   * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
   */
  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
  }

}
TOP

Related Classes of demo.servlet.HomeServlet

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.