Parameters and Attributes in Servlet

Parameters may come into our application from the client request, or may be configured through deployment descriptor (web.xml) elements or their corresponding annotations. When you submit a form, form values are sent as request parameters to a web application. In case of a GET request, these parameters are exposed in the URL as name value pairs and in case of POST, parameters are sent within the body of the request. 

Servlet init parameters and context init parameters are set through the deployment descriptor (web.xml) or their corresponding annotations. All parameters are read-only from the application code. We have methods in the Servlet API to retrieve various parameters. 

Attributes are objects that are attached to various scopes and can be modified, retrieved or removed. Attributes can be read, created, updated and deleted by the web container as well as our application code. We have methods in the Servlet API to add, modif, retrieve and remove attributes. When an object is added to an attribute in any scope, it is called binding as the object is bound into a scoped attribute with a given name.

 

Important differences between attributes and parameters in JSP/servlets are:

  1. Parameters are read only, attributes are read/write objects. 

  2. Parameters are String objects, attributes can be objects of any type.

 

Servlet API methods to manipulate parameters

 

The API methods to retrieve the request parameters are:

  • ServletRequest.getParameterValues(String parmName)

    • returns a String array with all values present, or null if no value exists for the parameter name.

  • ServletRequest.getParameter(String parmName)

    • returns the first value for the given parameter.

  • ServletRequest.getParameterNames()

    • returns an Enumeration of String objects representing the names of all the parameters in the request. If there are no parameters Enumeration will be empty.

  • ServletRequest.getParameterMap()

    • returns a java.util.Map object, where the keys in the map are of type String (and represent each unique parameter name) and the values in the map of type String array (representing the values for the parameter).

 

The API methods to retrieve the ServletContext initialization parameters from a ServletContext object are:

  • ServletContext.getInitParameterNames()

    • will always return an enumeration of names.

  • ServletContext.getInitParameter(String paramName)

    • will return a String or null.

 

The API methods to retrieve the ServletConfig initialization parameters from a ServletConfig object are:

  • ServletConfig.getInitParameterNames()

    • returns an enumeration of all the parameter names available to the servlet.

  • ServletConfig.getInitParameter(String paramName)

    • return a parameter value.

Both the methods are implemented in the GenericServlet abstract class.

 

Servlet API methods to manipulate attributes 

 

The attribute manipulation methods for request, session and application scopes are identical and differ only on the interfaces they are defined. They are:

  • public void setAttribute(String name, Object value)

  • public Object getAttribut(String name)

  • public Enumeration getAttributeNames()

  • public void removeAttribute(String name)

 

Note:

  • If the object passed has a value of null, it has the same effect as calling removeAttribute() for that attribute.

  • If any of the attribute manipulation functions are invoked in session scope when the session is invalid, IllegalStateException will be thrown.

  • Null is returned if no attribute of the given name exist.

  • If we call getAttributeNames() when no attributes are there, an empty enumeration will be returned. However, since some attributes will be always supplied by the web container during the application context, the enumeration returned by getAttributeNames() will never be empty in the application scope.

  • There is no guarantee of the order of the attributes within the enumeration returned by the getAttributeNames() method.

Search the Web

Custom Search

Searches whole web. Use the search in the right sidebar to search only within javajee.com!!!