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

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 infra-structure, 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.

husseinterek

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

You may also like...

5
Leave a Reply

avatar
4 Comment threads
1 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
5 Comment authors
Smitha750ArjunVonhusseinterekAlex Recent comment authors
newest oldest most voted
Alex
Guest
Alex

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

Von
Guest
Von

salam. you are a very handsome brother.

Arjun
Guest
Arjun

Thank you 🙂

Smitha750
Guest
Smitha750

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