1

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 :)

ArtoAle
  • 2,939
  • 1
  • 25
  • 48

3 Answers3

3

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;
  }
}
Qwerky
  • 18,217
  • 6
  • 44
  • 80
  • Ok, this is not what I need; Sorry for having stolen your time :) I'm quite familiar with MVC, I use Yii framework for production purpose; what I want to do here is to build up a (very small) framawork in Java/Jsp (wich foce MVC architectural pattern :D ) for accademical purpose; Now, I solved that problem but I need some more help. Is there a way from within a java class to display a JSP page (assuming I have reference to the response.out writer) within this class? – ArtoAle Jan 24 '11 at 23:03
2

This isn't how it is used.

For the purpose you have demonstrated.

You should include a Servlet or jsp or static HTML just to print Hello World like

<jsp:include page="/staticfile/helloworld.html" />

in helloworld.html

just

hello world

or include servlet

<jsp:include page="/helloworldServlet" />

and in HelloWorld Servlet doGet()

out.println("hello world");

Also See

Community
  • 1
  • 1
jmj
  • 237,923
  • 42
  • 401
  • 438
  • actually, i don't want to create an hello wordwebapp =) I'm trying to build up a small framework for accademic purpose, and I need some basic information to set up the framework architecture. – ArtoAle Jan 24 '11 at 16:38
  • *OK* , I just suggested standard practices :) – jmj Jan 24 '11 at 16:39
0

Without passing the context to the class method, and without storing it in a ThreadLocal variable (which I think would be a bad idea), I don't see how you could do that.

If your class needs access to the JSP context it probably means that the class should be a JSP fragment or a custom JSP tag (<custom:sayHello/>).

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255