JSON
JSON JavaScript Object Notation a lightweight text-based open standard designed for human- readable data interchange. Derived from JavaScript for representing singleton
JSON Example { “someString: “asas”, “someNum” : 4, “someArr” : [“aa”,”bb”], “someJson”: { someStr2:”aaa”} }
JSON and AJAX JSON is the most standard data format for ajaxing http://localhost:8888/ajax.html
JSON and JavaScript String to Object: str.parseJSON(); Object to String JSON.stringify(obj);
JSON in Java GSON A free open source that helps translate object<-> JSON http://code.google.com/p/google-gson/ http://sites.google.com/site/gson/gson-user-guide
Cookies
Cookies A piece of text stored by a user's web browser Key-value format May be encrypted With or without an expiration date. Cookies without an expiration date exist until the browser terminates Cookies can’t be used as a virus
Usage HttpSession Personalization Tracking (e.g. “Remember me?”) 3rd parties cookies
Implementation
Cookies and HTTP Cookies are part of the Http request and Response headers Request: “Set-Cookie: name=value“ Response: “Cookie: name=value”
Cookies and JavaEE Adding a cookie: Reading cookies: Cookie cookie = new Cookie("CName","Cookie Value"); cookie.setMaxAge(100); //in seconds response.addCookie(cookie); Reading cookies: Cookie[] cookies = request.getCookies();
Cookies and JavaScript Creating a cookie: document.cookie = name+"="+value+expires+"; Reading all cookies: var cookiesArray = document.cookie.split(';'); Erasing a cookie Same as creating whereas “expires = -1”;
Session hijecking
HttpFilters
Filter is like a lightweight servlet that doesn't generate its own content instead it plugs into the request handling process and executes in addition to the normal page processing. Filters can be applied to any resource Filter implements the interface javax.servlet.Filter, and configured in the web application web.xml file doFilter()
Filter Example doFilter(request, response, FilterChain chain) { long startTime = System.currentTimeMillis(); chain.doFilter(request, response); long stopTime = System.currentTimeMillis(); System.out.println("Time to execute request: " + (stopTime - startTime) + " milliseconds"); {
In Web.xml <filter> <filter-name>Timer</filter-name> <filter-class>internet.demo.Timer</filter-class> </filter> <filter-mapping> <url-pattern>/*</url-pattern> // any file.. </filter-mapping
Other usages Auto-redirect to login page … Block access to resources such as pictures..
Servlet 3.0
New Version for servlets Annotations @Servlet(urlMappings={"/MyApp"}) // no web.xml @GET on @POST on any method @ServletFilter @FilterMapping("/foo") // no web.xml Asynchronous Support the Servlet Engine “understands” that this thread can be suspended
Chrome Extensions
Chrome Extension Chrome is a web browser developed by Google It was first released on September 2008 Chrome is the third most widely used browser, with 13.35% of worldwide usage Chrome includes V8 A JS VM
Extension An extension is a zipped bundle of files—HTML, CSS, JavaScript, images.. adds functionality to the Chrome browser. Extensions are essentially web pages.
Popup extension Manifast.json { "name": "My First Extension", "version": "1.0", "description": "The first extension that I made.", "browser_action": "default_icon": "icon.png" "popup": "popup.html" }, "permissions": [ "http://api.flickr.com/" ] }
Background extension Manifast.json { "name": "My First Extension", "version": "1.0", "description": "The first extension that I made.", "background_page": "bg.html", "permissions": [ "http://api.flickr.com/" ] }
The backgroundPage.html: <html> <body> <script type="text/javascript"> //the extension functionality here </script> </body> </html>
API for background chrome.browserAction.onClicked.addListener( function(tab){ //do something when someone click} ); chrome.tabs.executeScript(tabs[i].id, {“code”:code} chrome.management.onInstalled.addListener().. API: http://code.google.com/chrome/extensions/api_index. html
Tutorial http://project67555.appspot.com/extension.html
Q/A