Initialize it in @PostConstruct
.
private String username;
@PostConstruct
public void init() {
username = FacesContext.getCurrentInstance().getExternalContext().getRemoteUser();
}
Or, if you only need it during processing the form submit, just obtain it in action method instead.
Note that getRemoteUser()
basically returns the same as getUserPrincipal().getName()
.
Unrelated to the concrete problem: this kind of bean shouldn't be session scoped, but instead view or conversation scoped. Also, it should not implement the Principal
interface. That makes no utter sense.