Reputation: 321
I have the working Java code here to display a user's username upon login:
<%= "Welcome " + session.getAttribute("username")%>
If a user is not logged in and at my index.jsp page, it will display "Welcome null". However, I want to display "Welcome Guest" instead.
Is there any way to do this?
Upvotes: 0
Views: 119
Reputation: 42020
You can do that with JSTL:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
Welcome, <c:out value="${sessionScope.username}" default="guest" />
Upvotes: 1
Reputation: 213223
Use JSTL
tag libraries. JSP scriplets are deprecated almost a decade back.
This is how you do it using JSTL:
Welcome, <c:out value = "${sessionScope.username == null ? 'Guest' : sessionScope.username}" />
See also:
Upvotes: 1