Technology Blogs by SAP
Learn how to extend and personalize SAP applications. Follow the SAP technology blog for insights into SAP BTP, ABAP, SAP Analytics Cloud, SAP HANA, and more.
cancel
Showing results for 
Search instead for 
Did you mean: 
former_member193577
Active Contributor
1,894

Hi,


In this short blog post, you can find sample code on how to read/save cookies in portal component java code or using javascript.


First we build a portal component that demonstrates both.


The main doContent method, calling both techniques:



package com.sap.portal.examples;
import javax.servlet.http.Cookie;
import com.sapportals.portal.prt.component.*;
import com.sapportals.portal.prt.resource.IResource;
public class WebCookie extends AbstractPortalComponent
{
public void doContent(IPortalComponentRequest request, IPortalComponentResponse response)
{
handleCookiesByJS(request,response);
handleCookiesInJava(request,response);
}
....




1. First method, writing cookies in Javascript and then reading them:



private void handleCookiesByJS(IPortalComponentRequest request, IPortalComponentResponse response)
{
//include the javascript source that holds the js method to store/read cookie
IResource resource = request.getResource(IResource.SCRIPT,"scripts/cookiescript.js");
response.include(request, resource);
//launch a js method to write cookie
response.write("<script>write_cookie();</script>");
//launch a js method to read cookie and print on screen
response.write("Cookie reads = " + "<script>document.write(gettimes());</script>");
}

Above you can see javascript "cookiescript.js" is included in the response of the portal component. This js contains two methods write_cookie() and gettimes()


After which write_cookie() is called and eventually gettimes()  is called.


javascript code cookiescript.js:


The write_cookie() method  searches for a cookie named "Basic_Cookie" and adds 1 to the value of the cookie


Eventually the cookie actually stores the number of times this method was called:



var cookie_name = "Basic_Cookie"
function write_cookie() {
var index =document.cookie? document.cookie.indexOf(cookie_name) : -1;
if (index == -1){
//creating new cookie which will expire in 2040
document.cookie=cookie_name+"=1; expires=Wednesday, 01-Aug-2040 08:00:00 GMT";
}
else{
//cookie exists, read its value
var countbegin = (document.cookie.indexOf("=", index) + 1);
var countend = document.cookie.indexOf(";", index);
if (countend == -1) {
countend = document.cookie.length;
}
var count = eval(document.cookie.substring(countbegin, countend)) + 1;
//update the cookie
document.cookie=cookie_name+"="+count+"; expires=Wednesday, 01-Aug-2040 08:00:00 GMT";
}
}

The gettimes()  method read value of the "Basic_Cookie" and returns number stored in it:



//this method reads the value of the cookie and return number stored in it.
function gettimes() {
var count = 0;
if(document.cookie) {
var index = document.cookie.indexOf(cookie_name);
if (index != -1){
var countbegin = (document.cookie.indexOf("=", index) + 1);
var countend = document.cookie.indexOf(";", index);
if (countend == -1){
countend = document.cookie.length;
}
count = document.cookie.substring(countbegin, countend);
}
}
return (count + " times");
}

2. Second method, accessing cookie in portal component java code:



private void handleCookiesInJava(IPortalComponentRequest request, IPortalComponentResponse response)
{
response.write("<br>");
saveToCookie(request,response,"MyCookie","MyCookieValue");
String val = getCookieValue(request,"MyCookie");
response.write("Got cookie MyCookie with its value="+val); //first time it will be null, since the response is not written yet
}

Here the component calls a java method saveToCookie with key and value.


Then it reads the cookie value by calling getCookieValue method and prints it.


saveToCookie method:


cookie is searched in all cookie that came with request, then its deleted and a new cookie is created:
private void saveToCookie(IPortalComponentRequest request , IPortalComponentResponse response , String cookieName, String cookieValue)
{
//getting all cookies from request
Cookie [] cookies = request.getServletRequest().getCookies();
if(cookies != null)
{
for(int i = 0 ; i < cookies.length ; i++)
{
Cookie cookie = cookies[i];
//find the specific cookie and deletes it
if(cookie != null && cookie.getName().equals(cookieName))
{
cookie.setMaxAge(0);
break;
}
}
}
//create a new cookie with new value and save it
Cookie cookie = new Cookie(cookieName , cookieValue);
cookie.setPath("/");
response.addCookie(cookie);
}

getCookieValue method:


Cookie is searched and its value (or null if not found) is returned:
private String getCookieValue(IPortalComponentRequest request, String cookieName)
{
//get all cookies from request
Cookie [] cookies = request.getServletRequest().getCookies();
if(cookies != null)
{
for(int i = 0 ; i < cookies.length ; i++)
{
Cookie cookie = cookies[i];
//search for the cookie and get its value
if(cookie != null && cookie.getName().equals(cookieName))
{
return cookie.getValue();
}
}
}
return null;
}

After deploying this portal component, If we execute it  3 times, we will get the following output , from both techniques:


Cookie reads = 3 times

Got cookie MyCookie with its value=MyCookieValue

Related Links:


Important -  Security consideration using cookies


http://www.allaboutcookies.org/cookies/


http://en.wikipedia.org/wiki/HTTP_cookie

Enjoy,

Tal

You want to influence the future product roadmap of Enterprise Portal? You plan to implement your portal in the Cloud? Then you should participate in the following survey https://blogs.sap.com/2016/10/11/2016-elections-vote-now/.