I got you.

Thank a lot.

Jian

-----Original Message-----
From: Pierre-Yves Saumont [mailto:[EMAIL PROTECTED]]
Sent: Thursday, August 23, 2001 11:22 AM
To: [EMAIL PROTECTED]
Subject: Re: Get initial properties from a property file for JSP


Hi Jian,

That is not what I mean (although it would be possible). What I mean is : If
you write a bean that reads properties in a property file, you can use this
bean wherever you want. If you decide then to rewrite your application using
JSP, you don't have to change anything to your bean.

And if later you want to create a taglib for the same purpose, you still can
call your bean from a tag class. This is simply portable code !

Now for an example :

package com.mydomain.mybeans;

import java.io.*;
import java.util.*;

public class MyBean {

        private String rootDocumentPath;
        private String propFilePath;
        private String myProperty;

        public void setRootDocumentPath(String rootDocumentPath) {
                if (this.rootDocumentPath == null) {
                        StringBuffer path = new
StringBuffer(rootDocumentPath);
                        if (path.charAt(path.length() - 1) != '/' &&
path.charAt(path.length() -
1) != 92) {
                                path.append('/');
                        }
                        this.rootDocumentPath = path.toString();
                }
        }

        public void setPropFile(String propFile) {
                if (propFilePath == null) {
                        propFilePath = this.rootDocumentPath + propFile;
                        try {
                                FileInputStream fis = new
FileInputStream(propFilePath);
                                properties.load(fis);
                        }
                        catch (IOException ioe) {
                        }
                        this.myProperty =
properties.getProperty("myProperty");
                }
        }

        public String getMyProperty() {
                return this.myProperty;
        }
}

(I hope there are no syntax errors!)

This bean can be used in a JSP the folowing way :

<jsp:useBean id='mybean' class='com.mydomain.mybeans.MyBean'
scope='session'/>
<jsp:setProperty name='mybean' property='rootDocumentPath' value='<%=
getServletConfig().getServletContext().getRealPath("/") %>'/>
<jsp:setProperty name='mybean' property='propFile' value='mypropfile.prop'/>
<html>
        <body>
                <p><jsp:getProperty name='mybean' property='myProperty'/>
        </body>
</html>



Please note this important points :

The root document path has to be set before the property file can be
accessed. So, the property file can't be accessed in the constructor. For
this reason, propFilePath is tested against null so that it wont be red
again and again if the bean has the session (or larger) scope.

The bean has to belong to a package. If not, you have to specifically import
the class with a directive (That sounds studid but that's the way it is!)

Now, when you want to get rid of having to set the root document path, you
will write a taglib. In your tag, you will be able to use the bean without
any modification. So you will end with something like :

<html>
        <body>
                <p><mytaglib:myProperty/>
        </body>
</html>

which will give you the same result (but this is another story!)

Do you think you will ever want to use servlets again? (this just to make
this post servlet related ;-)

Pierre-Yves

-----Message d'origine-----
De : A mailing list for discussion about Sun Microsystem's Java Servlet
API Technology. [mailto:[EMAIL PROTECTED]]De la part de
Ouyang, Jian
Envoy� : jeudi 23 ao�t 2001 15:55
� : [EMAIL PROTECTED]
Objet : Re: Get initial properties from a property file for JSP


Thanks Perre-Yves.

I may missed something here.

Are you saying that I can instantiate a bean in the servlet and then use it
the way you coded? Can you show me a little more details? I know that one
can instantiate a class (bean) in the servlet and in order for a jsp to use
it, one have to save the object in a session or request or something like
that. Thanks again for your help.

Jian

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to