forked from piotrpolak/android-http-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSession.java
More file actions
48 lines (38 loc) · 1.88 KB
/
Copy pathSession.java
File metadata and controls
48 lines (38 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/**************************************************
* Android Web Server
* Based on JavaLittleWebServer (2008)
* <p/>
* Copyright (c) Piotr Polak 2008-2016
**************************************************/
package example;
import ro.polak.http.exception.ServletException;
import ro.polak.http.servlet.HttpServletRequest;
import ro.polak.http.servlet.HttpServletResponse;
import ro.polak.http.servlet.HttpServlet;
/**
* Session usage example page
*/
public class Session extends HttpServlet {
@Override
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException {
// Saving session attribute name in a variable for convenience
String attributeName = "pageHits";
// Resetting the counter
int pageHits = 0;
// Getting the page hits from session if exists
if (request.getSession().getAttribute(attributeName) != null) {
// Please note the session attribute is of String type
pageHits = Integer.parseInt((String) request.getSession().getAttribute(attributeName));
}
// Incrementing hits counter
++pageHits;
// Persisting incremented value in session
request.getSession().setAttribute(attributeName, Integer.toString(pageHits));
// Printing out the result
response.getWriter().println("<p>Session page hits: " + pageHits + "</p>");
response.getWriter().println("<p>Session is new: " + request.getSession().isNew() + "</p>");
response.getWriter().println("<p>Session creation time: " + request.getSession().getCreationTime() + "</p>");
response.getWriter().println("<p>Session last accessed time: " + request.getSession().getLastAccessedTime() + "</p>");
response.getWriter().println("<p>Session max inactive interval in seconds: " + request.getSession().getMaxInactiveInterval() + "</p>");
}
}