0

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?

Matt Ball
  • 344,413
  • 96
  • 627
  • 693
user2297666
  • 281
  • 2
  • 6
  • 20

2 Answers2

1

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:

Community
  • 1
  • 1
Rohit Jain
  • 203,151
  • 43
  • 392
  • 509
1

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" /> 
Paul Vargas
  • 40,346
  • 15
  • 98
  • 144