MY mENU


Saturday, 26 May 2012

Working with Cookie in JSP


Cookie is a small piece of information which is stored in user's computer. Webserver uses cookie to identify the user in the next time visit.

Each time user visits a website which cookie is enable, the web server adds extra data into the HTTP header and responds to the web browser. in the next time when user visits the same site again, the web browser also sends cookie in the HTTP request header to the web server.
User can also disables cookie in the web browser which supports disable cookie function such as Mozilla Firefox, IE...
Cookie is stored in the user's computer as a string of name/value. In addition, cookie has attributes such as domain, path, timeout...
JSP provides API to allows you to work with cookie effectively through object of the class javax.server.http.Cookie. Let's take a look at an example how to set cookie in JSP and respond it back to the client.

Sending cookie from the web server

In the code, first you create a new cookie with the name and value. If you se the methods such as setDomain()  and setPath() to restrict the cookie to the current URL you have to read the cookie exactly from that URL. Cookie has its own lifetime called expiration time. If you don't set the timeout for the cookie, it will be removed when user closes the web browser. The method setMaxage() is used to set the expiration time for the cookie. Finally you add cookie to the response header and store it in the user's computer by using methodaddCookie() of response object.

Reading cookie

To read cookie from a HTTP request, you first call the method getCookies() of the requestobject. This method returns you a list of available cookies in the request header, then you can walk through all of them. Here is the code to read cookie information:
Removing existing cookie
If you want to remove an existing cookie you've sent to the web browser, you can use the method setMaxAge() of that cookie object to set its timeout to zero. This is the sample code to remove all the cookies.

Change “AM” and “PM” Displayed Near the System Clock to Any Text You want


 You can add your name or anything you like that consists of 8 characters or less. This will replace the AM or PM next to the system time. Do not use this tip if you are using any software during the trial period as it will corrupt the trial license. Open the RegEdit and navigate to the following registry key: KEY_CURRENT_USER\ControlPanel\International. Add two new String values, "s1159" and "s2359". Most probably those keys will be already present. If it is not there then create them. Right click the value name and modify. Enter anything you like up to 8 characters. If you enter two different values when modifying, you can have the system tray display the two different values in the AM and PM.

Friday, 25 May 2012

Jsp Sessions


HTTP protocol is stateless. It means that there is no permanent connectionbetween client (web browser) and Web Server. When client request a page from a web server, it opens a connection, retrieve the page and close connection. Web servers doesn't know what happen then in the client side. In addition, If another request from client is made, web server doesn't associate the new connection with the connection has been made.

In order to overcome the stateless of HTTP protocol, JSP provides you the implicit sessionobject which is a HttpSession object. The session object resides in the server side so you can keep arbitrary data about the client and other data as well in session and later on in different requests you can retrieve the saved data for processing. JSP stores data in the server side in the session object by using a single key that client remembers.
The session object has the three most important methods which you use most as bellows:
public void setAttribute(String name, Object value)
    throws IllegalStateException
public Object getAttribute(String name)
    throws IllegalStateException
public void removeAttribute(String name)
    throws IllegalStateException
A session is an object associated with a visitor.  Data can be put in the session and retrieved from it, much like a Hashtable.  A different set of data is kept for each visitor to the site. Here is a set of pages that put a user's name in the session, and display it elsewhere.  Try out installing and using these.First we have a form, let us call it GetName.html
The target of the form is "SaveName.jsp", which saves the user's name in the session.  Note the variable "session".  This is another variable that is normally made available in JSPs, just like outand request variables.  (In the @page directive, you can indicate that you do not need sessions, in which case the "session" variable will not be made available.)

The SaveName.jsp saves the user's name in the session, and puts a link to another page, NextPage.jsp.
NextPage.jsp shows how to retrieve the saved name.
If you bring up two different browsers (not different windows of the same browser), or run two browsers from two different machines, you can put one name in one browser and another name in another browser, and both names will be kept track of. The session is kept around until a timeout period.  Then it is assumed the user is no longer visiting the site, and the session is discarded.
Exercise:  Add another attribute "age" to the above example. 

How session works

When server creates a new session, it always adds a session identifier in the form of cookie. When web browser asks for a page or makes a request, the web browser always sends cookie which are created by the web server in the request. Therefore in the server side, web server checks for that cookie and find the corresponding session that is matched to the received cookie.
The session normally short-lived so the session cookie is not saved into disk. Session also has time out. When the time is out, the session is no longer exist in the server side. You can set time out of session in configuration file in the server.