Cookies, Mobile

tenderlaSoftware and s/w Development

Dec 13, 2013 (3 years and 11 months ago)

76 views

Advanced Topics

BCIS 3680 Enterprise Programming

Overview

2


Cookies


Mobile Apps


Android


SQLite

Cookie

3


A cookie is piece of textual data (
name
-
value pair)
that is
stored on the client hard drive.


The server saves a cookie to the client. In the future,
when revisiting the same server,
the client includes the
cookie in its request

for pages from the server.


Cookies can be set to persist for up to 3 years.


Browsers generally accept only 20 cookies from each site
and 300 cookies in total. They can also limit each cookie
to 4 KB.


A cookie is usually associated with the domain of the
server that sets the cookie. But it can be set to associate
to other domains.

Pattern in Making a Cookie

4


Gather the info to store in the cookie (values of various properties of the cookie)


Create a Cookie object (needs cookie name and its value)


Set additional cookie attributes if necessary


Write cookie to the client

Creating Cookies

5


With the exception of the session cookie, the
programmer must create cookies manually.


Cookie is
not
an implicit object. To create a cookie, call
the constructor of the
Cookie

class. Its signature is:

Cookie(String
cookieName
, String
cookieValue
);


For example,

Cookie c = new Cookie("Type", "Chocolate
Chips");


Neither the cookie name nor the cookie value should
contain white space or any of the following characters:

[ ] ( ) = , “ / ? @ : ;

Setting Cookie Attributes

6


Before adding the cookie, you can set additional
attributes of the cookie.


Typically attributes are maximum age, path, domain, etc.


To set an attribute, use its corresponding setter method:


setMaxAge
(
int

maxAgeInSeconds
)
, e.g.,
c.setMaxAge
(60*60*24*365);

sets the age at
1

year.


setPath
(String path)
, e.g.,
c.setPath
("/")

makes
the cookie accessible anywhere in the entire application.


When you’re ready to write the cookie to the client, call
the
addCookie
()

method of the
response

object,
for example:


response.addCookie
(c);

Pattern in Reading a Cookie

7


Declare an array of
Cookie

objects


Retrieve all cookies from the client


Loop through array and compare the name of each cookie to that of the cookie
you are looking for


Read attributes from the matching cookie

Reading Cookies

8


All cookies are returned from the client as fields added to
the HTTP request headers.


Before reading the cookies, first collect them all through
the request object:


request.getCookies
();


This method returns an
array

of
Cookie

objects that
have been sent to client previously.


If there is no cookie in
request
, it returns
null
.


Before everything else, use an
if

statement to check
whether cookies exist.


Proceed to steps in the next slide if array is not
null
.

Reading Cookie Value and Attributes

9


Loop through the cookie array.


During each iteration, use the
getName
()

method to
retrieve the name of the cookie.


Compare the name of that cookie to the name of the
cookie you’re looking for.


If found, use the
getValue
()

method to retrieve the
value stored in the cookie.


Use the proper
get<Attribute>()

method(s) to
retrieve the value of additional cookie attribute(s) that is
(are) of interest to you.

Updating & Removing a Cookie

10


To update a cookie, write a cookie with the same name
but the new value.


To remove a cookie, find the cookie in the cookies array,
set its maximum age to 0.

Mobile Apps

11


Android apps are Java applications.


An Android project includes, among other things:


An
/
src

subfolder that contains source code files for the app.


The naming convention is the same for regular Java packages.
So if an app is named
edu.unt.Droid
. The
.java

files for
the app are then stored in
/
src
/
edu
/
unt
/Droid
.


A manifest file called
AndroidManifest.xml
, which
contains configuration info for the app. Its function is similar to
the manifest file in a JAR file.

Developing Android Apps

12


Java SE JDK


Make sure it’s installed.


Android SDK


Makes use of the Java SE JDK.


Provides the API libraries and developer tools necessary to
build, test, and debug apps for Android.


Download it from
http://developer.android.com/sdk/index.html
.


The ADT bundle contains a version of the Eclipse IDE pre
-
configured for
Android development.


Eclipse IDE


If you already have the Eclipse IDE installed, you add a plug
-
in
for the Android Developer Tools (ADT).


SQLite

13


SQLite

is an embedded DBMS.


Does not use the client
-
server architecture.


No independent service or process that manages the DBMS.


The DBMS engine is integrated in the application itself.


No network or server configuration is needed


less complex.


Uses one single file for a database.


Moving or backing up is as easy as copying the file.


Suited for embedded systems running limited OS.


Using the default configuration, the compiled
SQLite

library is less
than 700 KB in size and requires less than 4 MB of memory to
operate. Without advanced features, the size can decrease to 300 KB
or less and the required memory to 256 KB.

Traditional DBMS Architecture

14

Source:
Kreibich
, J.A.
Using
SQLite
,
O’Reilly, 2010.

SQLite

Architecture

15

Source:
Kreibich
, J.A.
Using
SQLite
,
O’Reilly, 2010.

Programming
SQLite

16


Although
SQLite

comes with a command line tool for
running queries and performing administrative tasks, the
native
SQLite

API is in C.


When building applications, use
SQLite

API and link to the
SQLite

DLLs.


When the apps runs and needs access to data, load the
SQLite

DLLs to interact with the database engine.


Extensions are available for scripting languages (e.g., JSP)
to access the functionalities of the APIs.


A JDBC driver for
SQLite

can be obtained from
https://bitbucket.org/xerial/sqlite
-
jdbc
.