Francis Francis - 3 months ago 13
Java Question

Display a session if not null in jsp file

I am trying to display a session only if it is not null in jsp file and I am having issues as shown

Syntax error on token "<", invalid Expression 59: 60: <% if(request.getAttribute("message") != null) 61:


this is my jsp file where I am checking if the session is not null but it is not working

<% if(request.getAttribute("message") != null)
{
<%=session.getAttribute("message")%>
}
%>


Please how do I display a session only if it is not null

EJK EJK
Answer

You cannot use the <%= construct within a scriptlet block <% %>. You will have to write Java code to do this:

<% if(request.getAttribute("message") != null)
{
  out.println(session.getAttribute("message"));
}
%>

Note that the <%= expression %> construct is shorthand for out.println(expression).