cancel
Showing results for 
Search instead for 
Did you mean: 

Http Session

drozes
Champ in-the-making
Champ in-the-making
Seriously, I think my brain is slow this last day.  Spent 3 hours trying to access the user HTTP Session through the JS Webscript with no luck.

Anyone able to tell me how to do this.

I just need a similar concept of the  php Session, allowing me to save data for each user privately.


Thank you in advance.
5 REPLIES 5

drozes
Champ in-the-making
Champ in-the-making
Day 3 of trying to access HTTP session without luck.  Help is greatly appreciated.

vladimirr
Champ in-the-making
Champ in-the-making
Any luck with this?

ddraper
World-Class Innovator
World-Class Innovator
If you're running a WebScript on it's own (e.g. via the /service URL prefix then you will have access to the WebScriptRequest object:

HttpServletRequest httpRequest = ((WebScriptServletRequest)req).getHttpServletRequest();
HttpSession httpSession = httpRequest.getSession();


However, if the WebScript is being run within the context of a page (e.g. it is associated with a Component bound into a Share page) then you will need to access it as follows:

HttpSession httpSession = ServletUtil.getSession(false);


But in both cases you will need to use <b>Java-backed</b> WebScripts. Hope that helps,

Regards,
Dave

lukasz_turakiew
Champ in-the-making
Champ in-the-making
Hi,

1. Create a custom JavaScript root object - it is a Java class which implements org.springframework.extensions.webscripts.processor.BaseProcessorExtension class, for example:

package pl.test;

import javax.servlet.http.HttpSession;

import org.springframework.extensions.surf.ServletUtil;
import org.springframework.extensions.webscripts.processor.BaseProcessorExtension;

public class HttpSessionHelper extends BaseProcessorExtension {
   
    public void setInSession(String paramName, String paramValue) {
        HttpSession session = ServletUtil.getSession();
        session.setAttribute(paramName, paramValue);
    }
   
    public String getFromSession(String paramName) {
        HttpSession session = ServletUtil.getSession();
       
        Object paramValue = session.getAttribute(paramName);
        if (paramValue != null) {
            return paramValue.toString();
        } else {
            return null;
        }
    }
}

2. Compile and package this class to jar archive and copy it to Share (or Surf based application).

3. Register this class in spring-surf-script-services-context.xml, for example:

<bean id="exampleExtension" parent="baseScriptExtension" class="pl.test.HttpSessionHelper">
      <property name="extensionName">
         <value>httpSessionHelper</value>
      </property>
</bean>

4. Use your new custom root object in webscript, for example:

var paramName = httpSessionHelper.getFromSession('paramName');
….
httpSessionHelper.setInSession('paramName','paramValue');

Best regards,
Lukasz Turakiewicz

isy
Champ in-the-making
Champ in-the-making
The previous solution doesn't work for me, ServletUtil.getSession() returns null
Is there another solution ?
Thx