Let me use this opportunity to introduce you to the V (View) in MVC (Model View Controller).
You should generally put data into the view by putting a view bean into the session on your controller. You can think of your class MyObject
as a view bean as it contains information you want to display in the view. The controller in this case is your servlet (you do have a servlet, right?) and would contain the following in its doGet
or doPost
method;
MyObject myObject = new MyObject("Hello world");
request.setAttribute("myObject", myObject);
The next step is to have your JSP display the data from the view bean. You are strongly encouraged to use JSTL for this, rather than by putting code snippets in. The JSTL tag <c:out>
can be used for displaying data in a JSP. Your JSP might contain the following;
<p>
<c:out value="${myObject.message}"/>
</p>
This will call the getMessage()
method on the session object 'myObject' and output it on the page.
Just for completeness, your MyObject
view bean might look like this;
public class MyObject
{
String message;
public MyObject(String message)
{
this.message = message;
}
public String getMessage()
{
return message;
}
}