Portlet Factory, Version 6.1.2
Getting and setting variable values
You can access all of the variables in the web application by calling the webAppAccess.getVariables() method. Typically, you get or set the value of one variable at a time.
- To get the value of a variable of a simple type (String, int, etc.)
- String valueOfMyVariable = webAppAccess.getVariables().getString("MyVariable");
- To get the XML structure stored in a variable of the XMLData type
- IXml valueOfMyXMLVariable = webAppAccess.getVariables().getXml("MyXMLVariable");
- To get the text value of an element in a variable of the XMLData type
- You can get the text value for a particular element in the XML structure contained in a variable by calling the getXmlText() method and supplying the variable name and the XPath to the element whose value you want to return. The following code sample returns the text for the <name /> element of the second <customer /> element in the <customers /> structure:webAppAccess.getVariables().getXmlText("VariableTest", "customers/customer[1]/name");
- To get an element in a variable of the XMLData type
- You can get a particular element in the XML structure contained in a variable by calling the getXmlElement() method and supplying the variable name and the XPath to the element you want to return. The following code sample returns the second <customer /> element in the <customers /> structure:
webAppAccess.getVariables().getXmlElement("VariableTest", "customers/customer[1]");- To set the value of a variable of a simple type
- webAppAccess.getVariables().setInt("MyIntVariable", 4);
- To modify the XML structure stored in a variable of the XMLData type
- You can alter the XML structure stored in a variable by getting the XML structure with the getXML() method and then using the IXml API to modify the structure. The getXML() method returns a reference to the structure, so once you make the modifications to the structure returned by getXML(), there is no need to "set" the structure back into the variable.
The following code sample removes the second <customer /> element from the <customers /> structure:
IXml customers = webAppAccess.getVariables().getXml("CustomerList"); customers.removeChildElement(customers.findElement("customers/customer[1]"));- To set the text value of an element in a variable of the XMLData type
IXml customers = webAppAccess.getVariables().getXml("CustomerList"); customers.setText("customers/customer[2]/name", "NewName");
Parent topic: Overview: working with Java
- Getting system properties
You can get the values of IBM WebSphere Portlet Factory properties stored in the cluster.properties and bowstreet.properties files.
- About getting data from the request
You can access the HttpServletRequest object by calling the webAppAccess.getHttpServletRequest() method.
- WebSphere Portlet Factory API documentation
Library | Support |