Trying to access the HTTP session through Javascript Webscripts on QuickStart.
I am unable to find any help in saving elements to some private object for each http session.
Does anyone know of any solutions?
Trying to access the HTTP session through Javascript Webscripts on QuickStart.
I am unable to find any help in saving elements to some private object for each http session.
Does anyone know of any solutions?
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;
}
}
}
Compile and package this class to jar archive and copy it to Share (or Surf based application).
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>
Use your new custom root object in webscript, for example:
var paramName = httpSessionHelper.getFromSession('paramName');
httpSessionHelper.setInSession('paramName','paramValue');