How to pass variable from servlet to jsp page?

I have a servlet (front controller) that analyzes the request, prepares some necessary data (model), and then passes it to jsp, which will be displayed.

How to transfer data from servlet to jsp? (I was hoping that you can add new parameters to the parameter map in the request object, but this map cannot be changed).

I can add attributes to request , but I don't know how to get them from jsp.

All data must be in the request area. What is the right way?

+6
java jsp servlets
source share
2 answers

I can add attributes to the request, but I do not know how to get them from jsp.
You do not need to specifically โ€œextractโ€ them by simply referring to them.

 request.setAttribute("titleAttribute", "kittens are fuzzy"); 

and then

 Title here: ${titleAttribute} 
+6
source share

You can use a query or session scope for this. Besides Nikita Rybakโ€™s answer, you can use

 request.getSession().setAttribute("key","value"); 

And then use it in JSP with scriplet.

 <%=session.getAttribute("key")%> 

Note that in the example pointed out by Nikita, the expression language (EL) was used (I'm not sure if these are JSTL tags). You need to explicitly indicate that EL should not be ignored using the isELIgnored property.

 <%@ page isELIgnored="false" %> 
+3
source share

All Articles