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)



  • 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.

Quick Notes Finder Tags

Activities (1) advanced java (1) agile (3) App Servers (6) archived notes (2) Arrays (1) Best Practices (12) Best Practices (Design) (3) Best Practices (Java) (7) Best Practices (Java EE) (1) BigData (3) Chars & Encodings (6) coding problems (2) Collections (15) contests (3) Core Java (All) (55) course plan (2) Database (12) Design patterns (8) dev tools (3) downloads (2) eclipse (9) Essentials (1) examples (14) Exception (1) Exceptions (4) Exercise (1) exercises (6) Getting Started (18) Groovy (2) hadoop (4) hibernate (77) hibernate interview questions (6) History (1) Hot book (5) http monitoring (2) Inheritance (4) intellij (1) java 8 notes (4) Java 9 (1) Java Concepts (7) Java Core (9) java ee exercises (1) java ee interview questions (2) Java Elements (16) Java Environment (1) Java Features (4) java interview points (4) java interview questions (4) javajee initiatives (1) javajee thoughts (3) Java Performance (6) Java Programmer 1 (11) Java Programmer 2 (7) Javascript Frameworks (1) Java SE Professional (1) JPA 1 - Module (6) JPA 1 - Modules (1) JSP (1) Legacy Java (1) linked list (3) maven (1) Multithreading (16) NFR (1) No SQL (1) Object Oriented (9) OCPJP (4) OCPWCD (1) OOAD (3) Operators (4) Overloading (2) Overriding (2) Overviews (1) policies (1) programming (1) Quartz Scheduler (1) Quizzes (17) RabbitMQ (1) references (2) restful web service (3) Searching (1) security (10) Servlets (8) Servlets and JSP (31) Site Usage Guidelines (1) Sorting (1) source code management (1) spring (4) spring boot (3) Spring Examples (1) Spring Features (1) spring jpa (1) Stack (1) Streams & IO (3) Strings (11) SW Developer Tools (2) testing (1) troubleshooting (1) user interface (1) vxml (8) web services (1) Web Technologies (1) Web Technology Books (1) youtube (1)