I understand that you have the image in webapp
folder. So the problem is that you need to rewrite the URL to be context relative using UrlUtils.rewriteToContextRelative
.
Something like this might work:
public void renderHead(IHeaderResponse response) {
StringBuilder script = new StringBuilder();
script.append("$('#username').focus(function() {");
script.append("$('#username-img').attr(\"src\",\"");
script.append(UrlUtils.rewriteToContextRelative("img/logo.png", RequestCycle.get()));
script.append("\");");
script.append("});");
response.render(OnDomReadyHeaderItem.forScript(script.toString()));
}