} JavaServer Pages and JavaBeans One of the

} JavaServer Pages and JavaBeans One of the most interesting and powerful ways to use JavaServer Pages is in cooperation with JavaBeans components. JavaBeans are reusable Java classes whose methods and variables follow specific naming conventions to give them added abilities. They can be embedded directly in a JSP page using tags. A JavaBean component can perform a well-defined task (execute database queries, connect to a mail server, maintain information about the client, etc.) and make its resulting information available to the JSP page through simple accessor methods.* The difference between a JavaBeans component embedded in a JSP page and a normal third-party class used by the generated servlet is that the web server can give JavaBeans special treatment. For example, a server can automatically set a bean’s properties (instance variables) using the parameter values in the client’s request. In other words, if the request includes a nameparameter and the server detects through introspection (a technique in which the methods and variables of a Java class can be programatically determined at runtime) that the bean has a nameproperty and a setName(String name)method, the server can automatically call setName()with the value of the nameparameter. There’s no need for getParameter(). * For more information on JavaBeans, see http://java.sun.com/bean/ and the book Developing Java Beans by Robert Englander (O’Reilly). A bean can also have its scope managed automatically by the server. A bean can be assigned to a specific request (where it is used once and destroyed or recycled) or to a client session (where it’s automatically made available every time the same client reconnects). Sessions and session tracking are covered in depth in Chapter 7, Session Tracking. A bean can even be implemented as a servlet! If the server detects that a bean implements the javax.servlet.Servletinterface (either directly or by extending GenericServletor HttpServlet), it will call the bean’s service()method once for each request and the bean’s init() method when the bean is first created. The utility of this functionality is debatable, but it can be used by beans that need to prepare somehow before handling requests. Beans are embedded in a JSP page using the tag. It has the following syntax: You can set the following attributes of the tag: NAME Specifies the name of the bean. This is the key under which the bean is saved if its scope extends across requests. If a bean instance saved under this name already exists in the current scope, that instance is used with this page. For example: NAME=”userPreferences” VARNAME Specifies the variable name of the bean. This is the name used by the page to refer to the bean and invoke its methods. For example:
Quick Hint: If you are looking for best quality webspace to host and run your tomcat application check Vision tomcat hosting services

Comments are closed.