public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String title = "Session Tracking Example"; HttpSession session = request.getSession(true); String heading; Integer accessCount = (Integer) session.getAttribute("accessCount"); if (accessCount == null) { accessCount = new Integer(0); heading = "Welcome, Newcomer"; } else { heading = "Welcome Back"; accessCount = new Integer(accessCount.intValue() + 1); } session.setAttribute("accessCount", accessCount); out.println( "<BODY BGCOLOR=\"#FDF5E6\">\n" + "<H1 ALIGN=\"CENTER\">" + heading + "</H1>\n" + "<H2>Information on Your Session:</H2>\n" + "<TABLE BORDER=1 ALIGN=\"CENTER\">\n" + "<TR BGCOLOR=\"#FFAD00\">\n" + " <TH>Info Type<TH>Value\n" + "<TR>\n" + " <TD>ID\n" + " <TD>" + session.getId() + "\n" + "<TR>\n" + " <TD>Creation Time\n" + " <TD>" + new Date(session.getCreationTime()) + "\n" + "<TR>\n" + " <TD>Time of Last Access\n" + " <TD>" + new Date(session.getLastAccessedTime()) + "\n" + "<TR>\n" + " <TD>Number of Previous Accesses\n" + " <TD>" + accessCount + "\n" + "</TR>" + "</TABLE>\n"); // the following two statements show how to retrieve parameters in // the request. The URL format is something like: // http://localhost:8080/project2/servlet/ShowSession?myname=Chen%20Li String myname = request.getParameter("myname"); if (myname != null) out.println("Hey " + myname + "<br><br>"); out.println("</BODY></HTML>"); }
/** Get an unused ID string for storing an object in the session */ protected String getNewSessionObjectId() { HttpSession session = getSession(); synchronized (session) { Integer id = (Integer) getSession().getAttribute(SESSION_KEY_OBJECT_ID); if (id == null) { id = new Integer(1); } session.setAttribute(SESSION_KEY_OBJECT_ID, new Integer(id.intValue() + 1)); return id.toString(); } }
/** Get the ID with which the object is associated with the session, if any */ protected String getSessionObjectId(Object obj) { HttpSession session = getSession(); BidiMap map; synchronized (session) { map = (BidiMap) session.getAttribute(SESSION_KEY_OBJ_MAP); if (map == null) { map = new DualHashBidiMap(); session.setAttribute(SESSION_KEY_OBJ_MAP, map); } } synchronized (map) { String id = (String) map.get(obj); if (id == null) { id = getNewSessionObjectId(); map.put(obj, id); } return id; } }
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<br><h4>we are getting data</h4>"); String code = request.getParameter("code"); out.println("<br>code: " + code); out.println("<br>"); try { OAuthClientRequest requestOAuth = OAuthClientRequest.tokenLocation("https://graph.facebook.com/oauth/access_token") .setGrantType(GrantType.AUTHORIZATION_CODE) .setClientId(apiKey) .setClientSecret(secretKey) .setRedirectURI(redirectUri) .setCode(code) .buildBodyMessage(); OAuthClient oAuthClient = new OAuthClient(new URLConnectionClient()); GitHubTokenResponse oAuthResponse = oAuthClient.accessToken(requestOAuth, GitHubTokenResponse.class); accessToken = oAuthResponse.getAccessToken(); expiresIn = oAuthResponse.getExpiresIn(); } catch (OAuthSystemException ae) { ae.printStackTrace(); } catch (OAuthProblemException pe) { pe.printStackTrace(); } // out.println("<br>Access Token: " + accessToken); // out.println("<br>Expires In: " + expiresIn); try { FacebookClient facebookClient = new DefaultFacebookClient(accessToken); myFriends = facebookClient.fetchConnection("me/friends", User.class); myFeeds = facebookClient.fetchConnection("me/home", Post.class); for (User myFriend : myFriends.getData()) { f.add(myFriend.getName()); out.println("<br>id: " + myFriend.getId() + " Name: " + myFriend.getName()); } // out.println("<br>"); out.println("<br>f count: " + f.size()); } catch (FacebookException e) { e.printStackTrace(); } facebookDataBean fdb = new facebookDataBean(); fdb.setName("zishan ali khan"); HttpSession session = request.getSession(); if (session != null) { session.setAttribute("myfdb", fdb); session.setAttribute("yourFriends", f); session.setAttribute("feeds", myFeeds); RequestDispatcher view = request.getRequestDispatcher("result.jsp"); view.forward(request, response); f.clear(); // out.println("<br>I am in"); } else { // out.println("<br>Session Over"); } }
/** Common request handling. */ public void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resetState(); boolean success = false; HttpSession session = req.getSession(false); try { this.req = req; this.resp = resp; if (log.isDebug()) { logParams(); } resp.setContentType("text/html"); if (!mayPageBeCached()) { resp.setHeader("pragma", "no-cache"); resp.setHeader("Cache-control", "no-cache"); } reqURL = new URL(UrlUtil.getRequestURL(req)); clientAddr = getLocalIPAddr(); // check that current user has permission to run this servlet if (!isServletAllowed(myServletDescr())) { displayWarningInLieuOfPage("You are not authorized to use " + myServletDescr().heading); return; } // check whether servlet is disabled String reason = ServletUtil.servletDisabledReason(myServletDescr().getServletName()); if (reason != null) { displayWarningInLieuOfPage("This function is disabled. " + reason); return; } if (session != null) { session.setAttribute(SESSION_KEY_RUNNING_SERVLET, getHeading()); String reqHost = req.getRemoteHost(); String forw = req.getHeader(HttpFields.__XForwardedFor); if (!StringUtil.isNullString(forw)) { reqHost += " (proxies for " + forw + ")"; } session.setAttribute(SESSION_KEY_REQUEST_HOST, reqHost); } lockssHandleRequest(); success = (errMsg == null); } catch (ServletException e) { log.error("Servlet threw", e); throw e; } catch (IOException e) { log.error("Servlet threw", e); throw e; } catch (RuntimeException e) { log.error("Servlet threw", e); throw e; } finally { if (session != null) { session.setAttribute(SESSION_KEY_RUNNING_SERVLET, null); session.setAttribute(LockssFormAuthenticator.__J_AUTH_ACTIVITY, TimeBase.nowMs()); } if ("please".equalsIgnoreCase(req.getHeader("X-Lockss-Result"))) { log.debug3("X-Lockss-Result: " + (success ? "Ok" : "Fail")); resp.setHeader("X-Lockss-Result", success ? "Ok" : "Fail"); } resetMyLocals(); resetLocals(); } }
/** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods. * * @param request servlet request * @param response servlet response */ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); HttpSession session = request.getSession(); try { db.connectDB(); String query = "SELECT * FROM Account_Information WHERE AI_ID='" + session.getAttribute("id") + "'"; ResultSet r = db.get_query(query); AccountInfoBean account = new AccountInfoBean(); while (r.next()) { String login = "******" + r.getString("AI_ID"); String password = "******" + r.getString("AI_Password"); String type = " " + r.getString("AI_Type"); String firstName = " " + r.getString("AI_First_Name"); String middleName = " " + r.getString("AI_Mid_Name"); String lastName = " " + r.getString("AI_Last_Name"); String email = " " + r.getString("AI_EMail"); String phone = " " + r.getString("AI_Phone"); String age = " " + r.getString("AI_Age"); String address1 = " " + r.getString("AI_Address1"); String address2 = " " + r.getString("AI_Address2"); String city = " " + r.getString("AI_City"); String state = " " + r.getString("AI_State"); String zip = " " + r.getInt("AI_Zip"); account.setLogin(login.trim()); account.setPassword(password.trim()); account.setPassword2(password.trim()); account.setType(type.trim()); account.setFirstName(firstName.trim()); account.setMiddleName(middleName.trim()); account.setLastName(lastName.trim()); account.setEmail(email.trim()); account.setPhone(phone.trim()); account.setAge(age.trim()); account.setAddress1(address1.trim()); account.setAddress2(address2.trim()); account.setCity(city.trim()); account.setState(state.trim()); account.setZip(zip.trim()); session.setAttribute("account", account); } } catch (Exception e) { System.out.println(e); } response.sendRedirect("accountedit.jsp"); /* TODO output your page here out.println("<html>"); out.println("<head>"); out.println("<title>Servlet</title>"); out.println("</head>"); out.println("<body>"); out.println("</body>"); out.println("</html>"); */ out.close(); }