I’m not very familiar with JSP, so, let’s make this question an example:
suppose I have a JSP file (index.jsp) which contain those statement:
<%
MyObject mO = new MyObject();
mO.sayHelloWorld();
%>
and in the MyObject.java:
public class MyObject(){
public void sayHelloWorld(){
//something like getJSPApplicationContext.getOut.println("<p>Hello World</p>");
}
}
is there a simple way to reach this goal (without passing the JSPApplicationContext to my class?)
Maybe I’m doing something really wrong, anyway, thanks for yout help 🙂
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
MyObjectas 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 itsdoGetordoPostmethod;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;This will call the
getMessage()method on the session object ‘myObject’ and output it on the page.Just for completeness, your
MyObjectview bean might look like this;