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
  • 354,903
  • 100
  • 647
  • 710
user2297666
  • 321
  • 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
  • 209,639
  • 45
  • 409
  • 525
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
  • 41,222
  • 15
  • 102
  • 148