user2297666
user2297666

Reputation: 321

Display "guest" on webpage if User is not logged in

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

Answers (2)

Paul Vargas
Paul Vargas

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

Rohit Jain
Rohit Jain

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

Related Questions