6

I have a servlet (front-controller), which analyse the request, prepare some necessary data (model) and then should pass it to the jsp to be rendered.

How should I pass the data from servlet to jsp? (I hoped that it was possible to add new parameters to parameters map in the request object, but that map is unmodifiable).

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

All the data should be in the request scope. What's the correct way?

1
  • 1
    Here's a hello world: stackoverflow.com/tags/servlets/info There are by the way a lot of dupes in "Related" section at the right column, which you should already have seen in top of your question while entering the question. Commented Feb 8, 2011 at 15:42

2 Answers 2

6

I can add attributes to the request but I don't know how to retrieve them from the jsp.
You don't need to specifically 'retrieve' them, just referring them works

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

and then

Title here: ${titleAttribute}
Sign up to request clarification or add additional context in comments.

Comments

3

You can use the request or the session scope for this. Apart from the answer by Nikita Rybak, you can use

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

And then use it in JSP using scriplet.

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

Note that in the example given by Nikita, Expression Language(EL) has been used(I am not sure if it's JSTL tags). You need to explicitly state that EL is not to be ignored by using the isELIgnored property.

<%@ page isELIgnored="false" %>

1 Comment

This is exactly the code-examples I needed to get me past this hump -- thanks! +1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.