I have VF Login page for portal user where I am trying to save few fields which includes inputtext and picklist so when user close the browser and come back again then the same information appear back to them and they don't need to enter or select the same information again.

I am setting & getting the cookies in my controller and able to get the cookie information but when I close the browser and come back again then user selection is not populated on login screen and form is reset to default but cookies still present.

I tried to save the form data through javascript (setting cookies, localstorage) but there is same problem persists.

Any other possible way? Would be appreciated. Thanks in advance.


<label for="UserName" id="UserNameLabel">
 <apex:inputText id="UserName" value="{!username}"/>
<label for="Password" id="PasswordLabel">
 <apex:inputSecret id="Password" value="{!password}"/>
<apex:selectList value="{!selectedLang}" size="1">
  <apex:actionSupport event="onchange" action="{!setLang}" reRender="null"/>
  <apex:selectOptions value="{!langList}"/>
<apex:commandButton value="{!$Label.Submit}" action="{!login}"/>

Apex Controller:

public with sharing class LoginController

    public String username {get;set;}

    public List<SelectOption> languageList {get; set;}
    public String selectedLanguage {get; set;}

    public LoginController()


    public PageReference login()


        setCookie('username', username);
        setCookie('lang', selectedLanguage);

    return siteReference;

    public PageReference setLanguage()
        setCookie('lang', selectedLanguage);

        return forwardToTranslatedWeb();

    public PageReference forwardToTranslatedWeb()
        String fullSiteURL = URL.getCurrentRequestUrl().toExternalForm();
        string languageSettingFromCookie = getCookie('lang');

        //*** forward to requested URL if applicable and if already logged in
        if (!UserInfo.getUserType().equalsIgnoreCase('Guest')) {
            if (ApexPages.currentPage().getParameters().get('forwardURL') != null)
                return new PageReference(ApexPages.currentPage().getParameters().get('forwardURL').replace('\\?', '?'));
                return new PageReference('/apex/logout');

        selectedLanguage = languageSettingFromCookie;

        if(selectedLanguage == '')
            selectedLanguage = 'en';

        return null;

    public string getCookie(string name)
        Cookie cookie = ApexPages.currentPage().getCookies().get(name);

        if(cookie == null)
            return '';

        return cookie.getValue();

    // Set cookie
    public void setCookie(string name, string value)
        Cookie cookie = new Cookie(name, value, null, -1, false);

        ApexPages.currentPage().setCookies(new Cookie[]{cookie});

    // Initializes the list of allowed language options
    public void intializeLanguageList()
        List<string> languageSettings = From customsettings;
        languageList = new List<SelectOption>();

        // Initialize the language options
        for(Integer i = 0; i < languageSettings.size(); i += 2)
            languageList.add(new SelectOption(languageSettings[i], languageSettings[i + 1]));


Also JavaScript onkeypress setCookie and onload getCookies:

<script type="text/javascript"> 

var today = new Date(); 
var expiry = new Date(today.getTime() + 30 * 24 * 3600 * 1000); 
setCookie(name, value) { 
document.cookie=name + "=" + escape(value) + "; path=/; expires=" + expiry.toGMTString(); 

function getCookie(name) { 
var re = new RegExp(name + "=([^;]+)"); 
var value = re.exec(document.cookie); 
return (value != null) ? unescape(value[1]) : null; 
  • I think you'd probably have to post some code for us to look at – greenstork Jul 22 '14 at 15:34
  • @greenstork I have added the code. – JavaAster Jul 22 '14 at 16:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.