Top Description Methods
javax.portlet

public Interface PortletContext

Known Direct Implementers
javax.portlet.filter.PortletContextWrapper
Imports
java.net.MalformedURLException

The PortletContext interface defines a portlet view of the portlet container. The PortletContext also makes resources available to the portlet. Using the context, a portlet can access the portlet log, and obtain URL references to resources.

There is one context per "portlet application" per Java Virtual Machine. (A "portlet application" is a collection of portlets, servlets, and content installed under a specific subset of the server URL namespace, such as /catalog. They are possibly installed via a .war file.) As a web application, a portlet application also has a servlet context. The portlet context leverages most of its functionality from the servlet context of the portlet application.

Attributes stored in the context are global for all users and all components in the portlet application.

In the case of a web application marked "distributed" in its deployment descriptor, there will be one context instance for each virtual machine. In this situation, the context cannot be used as a location to share global information (because the information is not truly global). Use an external resource, such as a database to achieve sharing on a global scope.

Method Summary

Modifier and TypeMethod and Description
public Object

Returns:

an Object containing the value of the attribute, or null if no attribute exists matching the given name
getAttribute
(String
a String specifying the name of the attribute
name
)

Returns the portlet container attribute with the given name, or null if there is no attribute by that name.

public Enumeration<String>

Returns:

an Enumeration of attribute names
getAttributeNames
()

Returns an Enumeration containing the attribute names available within this portlet context, or an empty Enumeration if no attributes are available.

public ClassLoader

Returns:

the class loader for this portlet application.
getClassLoader
()

Returns the class loader for this portlet context.
public Enumeration<String>

Returns:

container runtime options keys supported by this container as String values.
getContainerRuntimeOptions
()

Returns the container container runtime options keys supported by this portlet container.

public String

Returns:

the context path for this portlet application.
getContextPath
()

Returns the context path for this portlet application.
public int

Returns:

the major version
getEffectiveMajorVersion
()

Returns the major version of the Portlet API used by the portlet application running in this PortletContext.
public int

Returns:

the minor version
getEffectiveMinorVersion
()

Returns the minor version of the Portlet API used by the portlet application running in this PortletContext.
public String

Returns:

a String containing the value of the initialization parameter, or null if the parameter does not exist.
getInitParameter
(String
a String containing the name of the requested parameter
name
)

Returns a String containing the value of the named context-wide initialization parameter, or null if the parameter does not exist.

public Enumeration<String>

Returns:

an Enumeration of String objects containing the names of the context initialization parameters
getInitParameterNames
()

Returns the names of the context initialization parameters as an Enumeration of String objects, or an empty Enumeration if the context has no initialization parameters.

public int

Returns:

the major version
getMajorVersion
()

Returns the major version of the Portlet API that this portlet container supports.

public String

Returns:

a String specifying the MIME type of the file
getMimeType
(String
a String specifying the name of a file
file
)

Returns the MIME type of the specified file, or null if the MIME type is not known.

public int

Returns:

the minor version
getMinorVersion
()

Returns the minor version of the Portlet API that this portlet container supports.

public PortletRequestDispatcher

Returns:

a PortletRequestDispatcher object that acts as a wrapper for the named servlet
getNamedDispatcher
(String
a String specifying the name of a servlet to be wrapped
name
)

Returns a PortletRequestDispatcher object that acts as a wrapper for the named servlet.

public String

Returns:

The name of the web application or null if no name has been declared in the deployment descriptor.
getPortletContextName
()

Returns the name of this portlet application correponding to this PortletContext as specified in the web.xml deployment descriptor for this web application by the display-name element.

public String

Returns:

a String specifying the real path, or null if the transformation cannot be performed.
getRealPath
(String
a String specifying a virtual path
path
)

Returns a String containing the real path for a given virtual path.

public PortletRequestDispatcher

Returns:

a PortletRequestDispatcher object that acts as a wrapper for the resource at the specified path.
getRequestDispatcher
(String
a String specifying the pathname to the resource
path
)

Returns a PortletRequestDispatcher object that acts as a wrapper for the resource located at the given path.

public URL

Returns:

the resource located at the named path, or null if there is no resource at that path
getResource
(String
a String specifying the path to the resource
path
)

Returns a URL to the resource that is mapped to a specified path.

public InputStream

Returns:

the input stream
getResourceAsStream
(String
the path to the resource
path
)

Returns the resource located at the given path as an InputStream object.

public Set<String>

Returns:

a Set containing the directory listing, or null if there are no resources in the web application of which the path begins with the supplied path.
getResourcePaths
(String
the partial path used to match the resources, which must start with a slash
path
)

Returns a directory-like listing of all the paths to resources within the web application longest sub-path of which matches the supplied path argument.

public String

Returns:

the string containing at least name and version number
getServerInfo
()

Returns the name and version of the portlet container in which the portlet is running.

public void
log(String
a String specifying the message to be written to the log file
msg
)

Writes the specified message to a portlet log file, usually an event log.

public void
log(String
a String that describes the error or exception
message
,
Throwable
the Throwable error or exception
throwable
)

Writes an explanatory message and a stack trace for a given Throwable exception to the portlet log file.

public void
removeAttribute(String
a String specifying the name of the attribute to be removed
name
)

Removes the attribute with the given name from the portlet context.

public void
setAttribute(String
a String specifying the name of the attribute
name
,
Object
an Object representing the attribute to be bound
object
)

Binds an object to a given attribute name in this portlet context.

Method Detail

getAttributeback to summary
public Object getAttribute(String name)

Returns the portlet container attribute with the given name, or null if there is no attribute by that name. An attribute allows a portlet container to give the portlet additional information not already provided by this interface. A list of supported attributes can be retrieved using getAttributeNames.

The attribute is returned as a java.lang.Object or some subclass. Attribute names should follow the same convention as package names. The Java Portlet API specification reserves names matching java.*, javax.*, and sun.*.

Parameters
name:String

a String specifying the name of the attribute

Returns:Object

an Object containing the value of the attribute, or null if no attribute exists matching the given name

Exceptions
IllegalArgumentException:
if name is null.
See Also
getAttributeNames
getAttributeNamesback to summary
public Enumeration<String> getAttributeNames()

Returns an Enumeration containing the attribute names available within this portlet context, or an empty Enumeration if no attributes are available. Use the getAttribute method with an attribute name to get the value of an attribute.

Returns:Enumeration<String>

an Enumeration of attribute names

See Also
getAttribute
getClassLoaderback to summary
public ClassLoader getClassLoader()

Returns the class loader for this portlet context.
Returns:ClassLoader

the class loader for this portlet application.

Exceptions
SecurityException:
- if a security manager denies access to the requested class loader
Since
3.0
getContainerRuntimeOptionsback to summary
public Enumeration<String> getContainerRuntimeOptions()

Returns the container container runtime options keys supported by this portlet container.

Returns:Enumeration<String>

container runtime options keys supported by this container as String values.

Since
2.0
getContextPathback to summary
public String getContextPath()

Returns the context path for this portlet application.
Returns:String

the context path for this portlet application.

Since
3.0
getEffectiveMajorVersionback to summary
public int getEffectiveMajorVersion()

Returns the major version of the Portlet API used by the portlet application running in this PortletContext.
Returns:int

the major version

Since
3.0
See Also
getEffectiveMinorVersion()
getEffectiveMinorVersionback to summary
public int getEffectiveMinorVersion()

Returns the minor version of the Portlet API used by the portlet application running in this PortletContext.
Returns:int

the minor version

Since
3.0
See Also
getEffectiveMajorVersion()
getInitParameterback to summary
public String getInitParameter(String name)

Returns a String containing the value of the named context-wide initialization parameter, or null if the parameter does not exist. This method provides configuration information which may be useful for an entire "portlet application".

Parameters
name:String

a String containing the name of the requested parameter

Returns:String

a String containing the value of the initialization parameter, or null if the parameter does not exist.

Exceptions
IllegalArgumentException:
if name is null.
See Also
getInitParameterNames
getInitParameterNamesback to summary
public Enumeration<String> getInitParameterNames()

Returns the names of the context initialization parameters as an Enumeration of String objects, or an empty Enumeration if the context has no initialization parameters.

Returns:Enumeration<String>

an Enumeration of String objects containing the names of the context initialization parameters

See Also
getInitParameter
getMajorVersionback to summary
public int getMajorVersion()

Returns the major version of the Portlet API that this portlet container supports.

Returns:int

the major version

See Also
getMinorVersion()
getMimeTypeback to summary
public String getMimeType(String file)

Returns the MIME type of the specified file, or null if the MIME type is not known. The MIME type is determined by the configuration of the portlet container and may be specified in a web application deployment descriptor. Common MIME types are text/html and image/gif.

Parameters
file:String

a String specifying the name of a file

Returns:String

a String specifying the MIME type of the file

getMinorVersionback to summary
public int getMinorVersion()

Returns the minor version of the Portlet API that this portlet container supports.

Returns:int

the minor version

See Also
getMajorVersion()
getNamedDispatcherback to summary
public PortletRequestDispatcher getNamedDispatcher(String name)

Returns a PortletRequestDispatcher object that acts as a wrapper for the named servlet.

Servlets (and also JSP pages) may be given names via server administration or via a web application deployment descriptor.

This method returns null if the PortletContext cannot return a PortletRequestDispatcher for any reason.

Parameters
name:String

a String specifying the name of a servlet to be wrapped

Returns:PortletRequestDispatcher

a PortletRequestDispatcher object that acts as a wrapper for the named servlet

See Also
PortletRequestDispatcher
getPortletContextNameback to summary
public String getPortletContextName()

Returns the name of this portlet application correponding to this PortletContext as specified in the web.xml deployment descriptor for this web application by the display-name element.

Returns:String

The name of the web application or null if no name has been declared in the deployment descriptor.

getRealPathback to summary
public String getRealPath(String path)

Returns a String containing the real path for a given virtual path. For example, the path /index.html returns the absolute file path of the portlet container file system.

The real path returned will be in a form appropriate to the computer and operating system on which the portlet container is running, including the proper path separators. This method returns null if the portlet container cannot translate the virtual path to a real path for any reason (such as when the content is being made available from a .war archive).

Parameters
path:String

a String specifying a virtual path

Returns:String

a String specifying the real path, or null if the transformation cannot be performed.

getRequestDispatcherback to summary
public PortletRequestDispatcher getRequestDispatcher(String path)

Returns a PortletRequestDispatcher object that acts as a wrapper for the resource located at the given path. A PortletRequestDispatcher object can be used include the resource in a response. The resource can be dynamic or static.

The pathname must begin with a slash ( / ) and is interpreted as relative to the current context root.

This method returns null if the PortletContext cannot return a PortletRequestDispatcher for any reason.

Parameters
path:String

a String specifying the pathname to the resource

Returns:PortletRequestDispatcher

a PortletRequestDispatcher object that acts as a wrapper for the resource at the specified path.

See Also
PortletRequestDispatcher
getResourceback to summary
public URL getResource(String path) throws MalformedURLException

Returns a URL to the resource that is mapped to a specified path. The path must begin with a slash (/) and is interpreted as relative to the current context root.

This method allows the portlet container to make a resource available to portlets from any source. Resources can be located on a local or remote file system, in a database, or in a .war file.

The portlet container must implement the URL handlers and URLConnection objects that are necessary to access the resource.

This method returns null if no resource is mapped to the pathname.

Some containers may allow writing to the URL returned by this method using the methods of the URL class.

The resource content is returned directly, so be aware that requesting a .jsp page returns the JSP source code. Use a RequestDispatcher instead to include results of an execution.

This method has a different purpose than java.lang.Class.getResource, which looks up resources based on a class loader. This method does not use class loaders.

Parameters
path:String

a String specifying the path to the resource

Returns:URL

the resource located at the named path, or null if there is no resource at that path

Exceptions
MalformedURLException:
if the pathname is not given in the correct form
getResourceAsStreamback to summary
public InputStream getResourceAsStream(String path)

Returns the resource located at the given path as an InputStream object. The data in the InputStream can be of any type or length. The method returns null if no resource exists at the given path.

In order to access protected resources the path has to be prefixed with /WEB-INF/ (for example /WEB-INF/myportlet/myportlet.jsp). Otherwise, the direct path is used (for example /myportlet/myportlet.jsp).

Parameters
path:String

the path to the resource

Returns:InputStream

the input stream

getResourcePathsback to summary
public Set<String> getResourcePaths(String path)

Returns a directory-like listing of all the paths to resources within the web application longest sub-path of which matches the supplied path argument. Paths indicating subdirectory paths end with a slash (/). The returned paths are all relative to the root of the web application and have a leading slash. For example, for a web application containing

/welcome.html
/catalog/index.html
/catalog/products.html
/catalog/offers/books.html
/catalog/offers/music.html
/customer/login.jsp
/WEB-INF/web.xml
/WEB-INF/classes/com.acme.OrderPortlet.class,

getResourcePaths("/") returns {"/welcome.html", "/catalog/", "/customer/", "/WEB-INF/"}
getResourcePaths("/catalog/") returns {"/catalog/index.html", "/catalog/products.html", "/catalog/offers/"}.

Parameters
path:String

the partial path used to match the resources, which must start with a slash

Returns:Set<String>

a Set containing the directory listing, or null if there are no resources in the web application of which the path begins with the supplied path.

getServerInfoback to summary
public String getServerInfo()

Returns the name and version of the portlet container in which the portlet is running.

The form of the returned string is containername/versionnumber.

Returns:String

the string containing at least name and version number

logback to summary
public void log(String msg)

Writes the specified message to a portlet log file, usually an event log. The name and type of the portlet log file is specific to the portlet container.

This method mapps to the ServletContext.log method. The portlet container may in addition log this message in a portlet container specific log file.

Parameters
msg:String

a String specifying the message to be written to the log file

logback to summary
public void log(String message, Throwable throwable)

Writes an explanatory message and a stack trace for a given Throwable exception to the portlet log file. The name and type of the portlet log file is specific to the portlet container, usually an event log.

This method is mapped to the ServletContext.log method. The portlet container may in addition log this message in a portlet container specific log file.

Parameters
message:String

a String that describes the error or exception

throwable:Throwable

the Throwable error or exception

removeAttributeback to summary
public void removeAttribute(String name)

Removes the attribute with the given name from the portlet context. After removal, subsequent calls to getAttribute to retrieve the attribute's value will return null.

Parameters
name:String

a String specifying the name of the attribute to be removed

Exceptions
IllegalArgumentException:
if name is null.
setAttributeback to summary
public void setAttribute(String name, Object object)

Binds an object to a given attribute name in this portlet context. If the name specified is already used for an attribute, this method removes the old attribute and binds the name to the new attribute.

If a null value is passed, the effect is the same as calling removeAttribute().

Attribute names should follow the same convention as package names. The Java Portlet API specification reserves names matching java.*, javax.*, and sun.*.

Parameters
name:String

a String specifying the name of the attribute

object:Object

an Object representing the attribute to be bound

Exceptions
IllegalArgumentException:
if name is null.