Question

I'm new to jspx and I'm not sure how I would do this. I have created a model-view-controller and have created a session in controller. Once the user has logged in, it creates the session.

HttpSession session = request.getSession();
session.setAttribute("user", username);

How can I access and display the username in the welcome.jspx page, so it would say

hello username
Was it helpful?

Solution

Use EL to access it.

hello ${user}

To prevent XSS attacks by username, show it using JSTL <c:out> so that XML special characters are escaped:

<jsp:root xmlns:c="http://java.sun.com/jsp/jstl/core" ...>
...

hello <c:out value="${user}" />

OTHER TIPS

Try something like this (For JSP):

<%
  String username = (String)session.getAttribute("user");
  out.println("<b>Welcome " + username + "!</b>");
%>

I have used scriptlet tag here. You can study about it from here .

(may be it could help someone else.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top