Ok on a Java Server Page I can use a function in two ways:
Either I declare
<jsp:useBean id="myBean" scope="session" class="mypack.mysubpack.MyClass" />
or I declare
<%@ page import = "mypack.mysubpack.MyClass" %>
In each case I can call a function later by coding
MyClass.myFunction();
So what is the difference between the two variantes?
Why are Beans invented if we don't need them because we have the import statement ?
Ulf
Alex Kizub - 13 Oct 2004 23:16 GMT
It's hard to suggest what you have to do.
Maybe read book "Java in 21 days" again... Or did you read "Java in 2 days"?
Anyway, your book was not good enough because it didn't explain difference between
static and object methods.
If you have class like this
public class A {
public static String STATIC(){ return "STATIC";}
public String regular(){ return "regular";}
}
you can call A.STATIC() anywhere.
But you need instance of A before you try to call regular() like this:
A a=new A(); a.regular();
Of course you can call a.STATIC() too, bad this bad behaviuor.
I hope you can find book which can explain the other differences for static/none static
behaviour.
Also I suggest for you always try your code first. At least you will mention that in
first case you should use this:
myBean.myFunction()
and in second:
MyClass.myFunction();
See the difference?
Alex Kizub.
> Ok on a Java Server Page I can use a function in two ways:
> Either I declare
[quoted text clipped - 11 lines]
>
> Ulf
The primary reason for the <jsp:useBean/> tag is to allow you to use the
tag
<jsp:getProperty name="name" property="property" />
rather than writing <%= name.getProperty %>. This conforms more to HTML
standards.
Also, the if you use the <%@ page import, you can execute a static method on
the class, but you still have no instance of the object. If you want get an
object that was passed to you in either the HttpServletRequest object or the
HttpSession, you have to write code to retrive it such as <% String myString
= (String) session.getAttribute("myString"); %>
The <jsp:useBean > not only declares the bean but can also retrieve it from
the session or request object and instantiate it ready for use.
Merrill
> Ok on a Java Server Page I can use a function in two ways:
> Either I declare
[quoted text clipped - 11 lines]
>
> Ulf
Tor Iver Wilhelmsen - 14 Oct 2004 08:33 GMT
> Ok on a Java Server Page I can use a function in two ways:
> Either I declare
> <jsp:useBean id="myBean" scope="session" class="mypack.mysubpack.MyClass" />
This checks if an object called "myBean" exists in the session
attributes, and if necessary creates and puts it there. It also adds a
variable called "myBean" that can be referenced later, in e.g.
<%=myBean.getFoo()%>.
> or I declare
> <%@ page import = "mypack.mysubpack.MyClass" %>
This does nothing more than add an import statement in the beginning
of the generated servlet.
> In each case I can call a function later by coding
>
> MyClass.myFunction();
>
> So what is the difference between the two variantes?
See above.
> Why are Beans invented if we don't need them because we have the
> import statement ?
Because beans aren't limited to what you artificially limit them to.
You can do much more with beans than just call static methods in their
classes.