Pass data from servlet to jsp

JSP is mostly used as the view component in any Java-based MVC application, its main usage is to present dynamic data processed and generated from server-side controllers like a servlet.

In this tutorial, we explain the different ways of passing attributes/parameters from servlet to JSP, along with several examples specific for passing data types like objects, arrays, lists and maps.

If you’re interested in Spring MVC, then check our “Pass data from Spring controller to JSP” tutorial.

1. Using request attributes

The common way of passing data from servlet to jsp is through defining attributes in the http request and then forwarding it to the corresponding jsp, this is done on the server side using one of the following techniques:

  • request.setAttribute(Name,Value)
    • This technique binds the attribute to the current request, the attribute is only visible to the current request and it keeps alive as far as the request is resolved or keeps being dispatched from servlet to servlet, this technique is very useful in web applications whenever you need to set dynamic attributes specific to the request cycle and need not to be available any more after the request.
    • At the server side, set the attribute in the request then forward the request to jsp page as the following:
    • In jsp, you can access the attribute like:

      Notice the use of ${ATTRIBUTE_NAME} syntax which implicitly checks for the attribute name in the servlet request and replaces it with its value, if attribute not found then empty string is returned.
  • request.getSession().setAttribute(Name,Value)
    • This technique binds the attribute to the user’s session, it’s used to provide a state to a set of related http requests, session attributes are only available to those servlets which join the session and they’re automatically killed whenever the session ends. This technique is normally used for passing contextual information like: ID, language .. etc
    • At the server side, add the attribute to the request session then forward the request to jsp page as the following:
    • In jsp, you can access it like:
  • getServletContext().setAttribute(Name,Value)
    • This technique binds the attribute to the whole context of the application, the attribute is available to all servlets (thus JSP) in that context. By definition, a context attribute exists locally in the VM where it is defined, so it isn’t available on distributed applications. Context attributes are meant for infrastructure, such as shared connection pools.
    • At the server side, add the attribute to the context then forward the request to jsp page as the following:
    • In jsp, you can access it like:

2. Using query string

The second way of passing data from servlet to jsp is through redirecting the response to the appropriate jsp and appending the attributes in the URL as query string.

  • At the server side, redirect the response to the jsp page and append the parameters directly in the url as the following:
  • In jsp, you can access the parameter like:

This technique is mostly used when you want to pass reasonable number of simple attributes.

3. Passing Objects

In order to pass a business object or POJO from servlet to jsp, you can pass it as an attribute using setAttribute() method described above.

Following is an example for passing a Student object from servlet to jsp:

At the server side, the Student object is set as a request attribute:

In jsp, we print the different attributes of Student object as the following:

Output:Student information

4. Passing ArrayList

We modify the previous example so that we pass a list of Student object, again we use setAttribute() method at the server side in order to pass the list from servlet to jsp.

In jsp, we iterate over the list of Student objects as the following:

It is worth to mention that it’s necessary to import List, ArrayList and Student objects at the top of the jsp page in order to be able to use them inside jsp:

The output of the above jsp is:Student Information

5. Passing HashMap

Suppose that we decided to pass the students in a HashMap instead of ArrayList, to do so we again use setAttribute() method and we pass the HashMap as the following:

In jsp, we traverse the HashMap as the following:

In order to use Entry, Map and HashMap objects in the jsp file, we import them at the top using:

The output of the above jsp looks like:Student Information

That’s it.

Hussein Terek

Founder of, I have a passion in software engineering and everything related to java environment.

You may also like...

Leave a Reply

8 Comment threads
1 Thread replies
Most reacted comment
Hottest comment thread
9 Comment authors
WandererpiyushmaheshAndrewKSmitha750 Recent comment authors
newest oldest most voted

Please note a typo in the 1st part: “getServletRequest().setAttribute(Name,Value)”, obviously getServletCONTEXT(


salam. you are a very handsome brother.


Thank you 🙂


I dugg some of you post as I cogitated they were very useful extremely helpful edgcfdeggdadegkc


Very Nice article. Thanks you. I was looking to pass data to a regular HTML page but this will work.


Very Very Thanks for this it helped me a lot………….thanks


Nice Article..bro