Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

Understanding JSP Cookies

In this article we will discuss about the jsp cookies in details. We will see how to create, clear and remove cookies in JavaServer Pages.

We know that modern world is running through online based systems. This online based system directly interact with sever and also check different types of event processing which we use regularly in our daily work. Cookie is a small data file resides in user’s system. The Cookie is made by web server to isolate the users. Basically when the user sends any request to web server, web server knows the information of the user by these cookies. After processing the request, web server responses back by knowing through this cookie. JSP provides cookie classes, javax.servlet.http.Cookie. By using this class, we can create and retrieve cookie. After setting the value in cookie, it lived until cookie gets expired. So we can understand that cookie plays an important role in session, because maximum session is tracked by cookie in JSP.

Role of Cookies in JSP:

Cookies are small text files which are stored by our browser onto our computer or mobile device. Basically they are widely used in order to make websites work or work in a better, more efficient way. These cookies can do this job since the websites can read and write these types of files and empowering them to recognize us and remember significant evidence that will make us to use of a website more conveniently (e.g. by remembering our user preferences). Most of the web browsers spontaneously accept cookies, but if we do not want this information collected this way there is a simple procedure in most browsers that allows us to contradict or accept the cookies feature. We are working with other third party organizations to provide the services on our website. The third party is a society which is not accommodated at the domain address in our browser when we are visiting our website.

These third parties may also use cookies for our website and can also place cookies on our device without visiting their website. The two types of cookies we use are known as “session” cookies and “persistent” cookies. A session cookie is used each time you visit our website to give a session ID. We use this type of cookie to link our actions on our website and each session cookie will only last for a browser session. Each browser session will start when we open our browser window and close at the time when we close our browser window. After our visit to our website all session cookies are deleted. Every interval when we visit our website we will find a different sessionID and this sessionID is very important for the server maintained processes. Persistent cookie has a longer lifetime than a session cookie and lasts for the period of time specified in the cookie. This system is not fixed and it will vary from one cookie to another.

These types of cookies will not be removed when we are closing our browser window and will be activated every time at the movement of visiting a website that is created that particular persistent cookie.

Features of JSP Cookies:

  • Cookies are the small text file.
  • Cookies are the client site process.
  • Generally store in client machine folder.
  • Cookies store in client machine.
  • Cookies are also using in the non-secured data file.
  • We generally do not set cookies in password, accountno fields.

Now given some example and illustrated it with step by step manner process that way all person can easily understand what types of the processing is done by the cookies. The example is given below.

Listing 1: Sample showing cookie setup

<%@ page contentType="text/html; charset=iso-8859-1" language="java" %>
<html>
<body>
<form name="frmcei" method="get" action="cookiegenerate.jsp">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="28%"> </td>
    <td width="72%"> </td>
    </tr>
  <tr>
    <td>Cookie Value Set Process.. </td>
    <td><input type="text" name="cookieSet" /></td>
  </tr>
  <tr>
    <td> </td> //this   is use for the give a space in webpage 
    <td> </td>
  </tr>
  <tr>
    <td> </td>
    <td><input type="submit" name="submit" value="Submit"></td>
    </tr>
  <tr>
    <td> </td>
    <td> </td>
    </tr>
</table>
</form>
</body>
</html>

Listing 2: Sample showing cookie generation

  <%@ page language="java" import="java.util.*"%>
<%
String Setcookie=request.getParameter("Setcookie");
Cookie cookie = new Cookie ("Setcookie ", Setcookie); //here cookie is set as a constructor process
response.addCookie(cookie);
%>
<html>
<head>
<title>Cookie Create Here…</title>
</head>
<body>
  <%
   Cookie[] cookies = request.getCookies();  //array block creation
  for (int i=0; i<cookies.length; i++)
 {
      out.println(cookies[i].getName()+" : "+cookies[i].getValue()+"<br/>"); //this part is use for display
  }
    %>
</body>
</html>

The general syntax for creating cookies is Cookie cookie = new Cookie ("Setcookie ", Setcookie); through this procedure we create a cookie object as the Cookie function is working as constructor function procedure so we have to pass the data through this parameter.

We are creating a new object of cookie class. Here new Cookie (“Key”, “value”);

Then we are accumulation in cookie of response with the object. response.addCookie(cookie); This is adding new data in cookie object.

Procedure to Expire JSP cookies:

As we generate cookies for our different purpose but some time it is required to expire the cookies to avoid the unnecessary disk space.

Cookie cookie = new Cookie ("Setcookie ", Setcookie);
cookie setMaxAge(0);
cookie setMaxAge(10);
cookie setMaxAge(20);

Here we set different types of cookies with various time intervals for expiry. We can set cookie age with different values. The first cookies will be expiring within 0 second, the second cookies will be expiring within 10 second, and the third cookies will be expiring within 20 second.

Procedure to retrieve cookie from Java Server Page:

Cookie is in array object, we need to get in request.getCookies() of array.

Cookie[] cookies = request.getCookies();
int i; 
for (i=0; i<cookies.length; i++) 
{
           out.println(cookies[i].getName()+" : "+cookies[i].getValue()+"<br/>");
}

Cookies and session go hand in hand; the only difference is that cookies are stored in the browser which is another method to handle a session.

Different types of explanation of the JSP Cookies

Listing 3: Some examples on handling Cookies in JavaServer Pages:

Example1:

 
<html>
   <body>
   <form method = "post" action="cookcei2.jsp">
    <font>Username<input type = "text" name = "name"></font>
   </font><br>
   <input type = "submit" name = "submit" value = "submit" >
   </form>
   </body>
   </html>

Example2:

    <%@ page language="java" import="java.util.*"%>
    <%
     String name=request.getParameter("sname");
     Cookie cookie = new Cookie ("sname",sname);
     response.addCookie(cookie);
     cookie.setMaxAge(50 * 50); //Time is in Minutes 
    %>
    <a href="cookcei3.jsp">Continue</a>

Example3:

    <p>Display the cookie value</p>
    <%
     Cookie[] cookies = request.getCookies();
     for (int i=0; i<cookies.length; i++) 
       {
      	 if(cookies[i].getName().equals("name"))  // check the condition is of the given name 
	     out.println("Welcome to Baranagar Computer Educational Institution"+cookies[i].getValue()); 
       }
    %>
<p>In the above example we have found out that in the first illustration "cookcei1.jsp" we can get the username using a page. After that when the page is submitted it goes to the second page "cookcei2.jsp", where the cookie is set an age using the "cookie.setMaxAge" function. The last page like "cookcei3.jsp" is not using the function "request.getCookies()" and using a loop to get the value of the "username" field and display it.</p> <H3>Procedure to Removing Cookies:</H3> <p><strong>Listing 4</strong>: Sample showing removing of cookies</p>
   <%
    Cookie cookie = new Cookie( "cname", "" );
    cookie.setMaxAge(0);
   %>

In the above example we have created a new instance of cookie with a "NULL" value (Actually NULL is not any value. It is void type system) and the age of the cookie is set to "0". This will remove or kill the cookie.

Conclusion

Now we know that cookie is client side architecture and it is basically designed for client machine. So this cookies is used for client computer not server computer system. Through this way we see that using cookies is set for the non-secure data in the web page field. So we can easily remember that what types of data are putting in the field. Hope you have enjoyed the article. Keep reading in mrbool.



Website: www.techalpine.com Have 16 years of experience as a technical architect and software consultant in enterprise application and product development. Have interest in new technology and innovation area along with technical...

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login