-3

I alreayd saw many questions about this but it the proposed solutions didn't work for me:

In my servlet i do this:

protected void doGet(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
        request.setAttribute("title", "Level0and1ForUser");

        request.getRequestDispatcher("./testPages/resultSetFormatter.jsp")
                .forward(request, response);
    }
}

And in the jsp page:

<title>
<%= request.getParameter("title") %>
</title>

the result is null as you see:

enter image description here

Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
Ania David
  • 1,168
  • 1
  • 15
  • 36

2 Answers2

2

You can use:

${requestScope.title}

NOTE:

Never use java code inside a jsp page, its deprecated and highly discouraged

The usage of scriptlets is always discouraged. Instead one should use tag libs like JSTL or Expression Language (EL).

The question always arises that why should we not use scriptlets in JSP when the same task can be performed using scriptlets as well.

Here is why one should avoid using scriplets in JSPs:

  • JSP Scriptlets reduces the maintainability and readibility of the code and hence making it difficult to read and make further changes.
  • JSP Scriptlets can not be re-used.
  • JSP Scriptlets are not unit-testable.
  • JSP Scriplets merge the presentation with business logic which is highly prone to errors.
  • Last but not least, if JSP Scriptlets throws an exception, it breaks the whole page there and doesn’t move further in the processing.
Community
  • 1
  • 1
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
1

if you put the value in an attribute of the request, you have to get it in the same way:

<title>
<%= (String)request.getAttribute("title") %>
</title>

but much more beautiful is Jordi's answer.

f1sh
  • 11,489
  • 3
  • 25
  • 51
nano_nano
  • 12,351
  • 8
  • 55
  • 83