Sunil Garg Sunil Garg - 2 months ago 14
Java Question

how to call method in jstl jsp

This question can be duplicate and have lots of answers on StackOverflow.

But I still did not get what is the issue in my code.

There is an object stored in session that is cmdResponse of type MessageResponse class

This is how I am getting from session

Command Response :



<%
Object cmdResponse = session.getAttribute("cmdResponse");
MessageResponse messageResponse = (MessageResponse) cmdResponse;
%>


There is getMessage() method that is getter method. Here is the code of MessageResponse class

public class MessageResponse extends Response {
String message;

public MessageResponse() {
// TODO Auto-generated constructor stub
}

public MessageResponse(String command, String message) {
super(command);
this.message = message;
}


public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

}


and this is how I am trying to render

<c:out value="${messageResponse.getMessage()}" />


but above line renders nothing and no error on the server. What can be the issue ?

Answer

You have to put the messageResponse variable as an attribute to the request, if you want to make it accessible from EL.

request.setAttribute("messageResponse", messageResponse);

Or you can use

<c:out value="${cmdResponse.getMessage()}" />

as EL tries the session when it does not find the name in request.

Last, but not least, <c:out value="${messageResponse.message}" /> (or <c:out value="${cmdResponse.message}" /> should be enough, EL calls the appropriate getter oh its own.