How to send data from Java 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 sending data from a Java servlet to JSP, along with several examples specific for passing data types as objects, arrays, lists, and maps.

1. Using HttpServletRequest

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 be available anymore after the request.
    • At the server-side, set the attribute in the request then forward the request to the JSP page like 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 an 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 the JSP page like 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 the JSP page like the following:
    • In JSP, you can access it like:

2. Redirect to JSP 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 a 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 a reasonable number of simple attributes.

3. Passing Objects from servlet to JSP

In order to pass a business object or POJO from servlet to JSP, you can pass it as an attribute using the 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 the Student object like the following:

Output:Student information

4. Passing ArrayList from Servlet to JSP

We modify the previous example so that we pass a list of Student objects, again we use the 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 mentioning 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 from Servlet to JSP

Suppose that we decided to pass the students in a HashMap instead of ArrayList, to do so we again use the 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 this:Student Information

3.3 3 votes
Article Rating

Hussein Terek

Owner of programmergate.com, I have a passion for software engineering and everything related to Java environment.

guest
11 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Alex
Alex
3 years ago

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

Von
Von
3 years ago

salam. you are a very handsome brother.

Arjun
Arjun
3 years ago

Thank you 🙂

Smitha750
Smitha750
3 years ago

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

AndrewK
AndrewK
2 years ago

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

mahesh
mahesh
2 years ago

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

piyush
piyush
2 years ago

Nice Article..bro

Wanderer
Wanderer
1 year ago

Legend!

Andre Dionisio
Andre Dionisio
1 year ago

There no citation to allow EL so I am putting here the tag necessary to enable EL at jsp: ” page isELIgnored=”false””

Jim Anderson
Jim Anderson
3 months ago

Hussein, Your article was very helpful. Thank you! I’m still a novice at JSP. Most importantly, I learned the flow for passing data from a java servlet to JSP. Unfortunately, my sample test case is not yet working. While creating the test case, I found an error. If you to to the first section: 1. Using request attributes, first bullet item:request.setAttribute(Name,Value), third sub-bullet item: In jsp, you can access the attribute like: I believe the line: <h3> My name is request.getAttribute(“name”)</h3> needs to be corrected. I’m not sure what is correct, but I think one of the two lines will… Read more »