Issue
This is the scenario
This is the JSP from where I get to the servlet. There is an entity called CurrentUser
wich holds multiple fields of information about the user. I want to pass the servlet all the information about the user in a form of entity.
<a href="Controller?action=profile&entity=<%=_currentUser%>"> Information about the current user, like: name, id, profile picture etc... </a>
Inside the servlet:
if(request.getParameter("action").equals("profile")){
CurrentUser _currentUser= (CurrentUser) request.getParameter("entity");
}
If I do a system.out to see whats inside "entity" parameter i get something like: Package.CurrentUser@abc1235
... and I cannot cast that, to receive it as an Entity.
incompatible types: String cannot be converted to CurrentUser
Is there any way to get that refference and the info inside those fields?
Solution
Inside the JSP, store your _currentUser
entity to the Session
as
<% session.setAttribute("entity", _currentUser); %>
<a href="Controller?action=profile">Information ... </a>
Now, inside the Controller
Servlet you can retrieve this entity
as
if(request.getParameter("action").equals("profile")){
CurrentUser _currentUser= (CurrentUser) session.getAttribute("entity");
}
Note, that storing the current user object into Session
should ideally be done by the Servlet
that forwarded the request to the JSP first. Use of Java code scriptlets <% %>
inside a JSP is deprecated as JSPs should mostly be used for presentation purposes only.
Answered By - Ravi K Thapliyal
Answer Checked By - Mary Flores (JavaFixing Volunteer)