cookie tag in struts example code using javax.servlet.http.Cookie class for remembering login details

Cookie is a text string that consists of name-value data which is included with HTTP requests and responses. Cookies helps to maintain state information like sessionid , authentication details , shopping cart contents and others when you navigate different pages of a web site . When you request a page , the cookies are sent to from the server to browser and can be read back again and sent to server.

Cookies can be created by client-side script like javascript and server side code like servlet. Cookies are  stored in memory (session cookies) or hard disk (persistent cookies).

Now let us see how to retrieve the cookie values from the request and store them in page scope variables.

Struts has many bean creation tags that is used to create reference to specific objects and store them in JSP scripting variables. One of the tag is Cookie tag which is used to define a scripting variable based on the value of the cookie included in current the request . You can create a bean containing a javax.servlet.http.Cookie included in the current request with the following syntax

<bean:cookie id=”scriptvariable” name=”cookiename”/> – to retrieve cookie with single value and defines the result as a page scope attribute of type Cookie

OR

<bean:cookie id=”scriptvariable” name=”cookiename” multiple=”true”/> – to retrieve cookie with multiple values and defines the result as a page scope attribute of type Cookie[]

Exception will be thrown, if no cookie with the specified name can be located, and no default value is specified

Attributes used with cookie tag

In many web application or mail application , when you open login screen there will be a check box with label “Keep me signed in ” or “Remember me” or “Stay signed in” . That means when you open the login screen next time , it will automatically login or you will get login screen with already filled in loginid and password details. This can be done with the help of cookies. When a user accesses the application , you can check for cookie values for loginid and password. If present , that values can be used to fill the loginid and password automatically and gets login. Similarly when you register for ticket booking / products / services, cookie helps you to retrieve your specific information like address, mobile number , etc. This may be very useful to simplify the process of entering your personal information without retyping every time which saves your valuable time.

javax.servlet.http.Cookie class

The following code helps you to fill out the loginid and password details automatically , if the cookie values are present in the client machine . To create cookie , javax.servlet.http.Cookie class is used .

This class creates a cookie that is sent to the web browser by the servlet . The servlet sends cookies to the browser by using the HttpServletResponse.addCookie(javax.servlet.http.Cookie) method, which adds fields to HTTP response headers . Then the cookie is saved by the browser, and later sent back to the server. A cookie is created by passing a name , a single value to the constructor of the Cookie class . Cookie has also some optional attributes such as a comment, path and domain qualifiers, a maximum age, and a version number. As per rfc , a typical web browser may support at least 20 cookies per domain and each cookie with at least 4096 bytes and total of 300 cookies .

The following code is written in login action and is executed after the loginid and password is validated

The above code may not be suitable for production use. The cookie data is not encrypted before sending to the browser.

Now the cookie tag can be used to retrieve the the cookie values and write the values in the login id and password fields in the JSP. The code is as follows.

You can enhance the above code by putting the check box with label “Remember me” and the cookie will be created based on the selection of the checkbox. For Login code in struts with MD5 encryption , please visit my earlier post Login code in Struts

Some more code using cookie tag to display JSESSIONID , loginid cookies and it’s value and attributes  in the JSP. The above action code can be used to to create cookies .

You may also like

Leave a Reply