I had much of the same problem with reading a ini-file for a servlet that
can be run
om different  IBM Websphere webservers.

I used the getPathTranslated() method of the HttpServletRequest object.
This method returns the full path of the servlet:
e.g http://www.server.com/MyServlet becomes
C:\Dir\AnotherDir\servlet\MyServlet

This code that goes into the doGet() or something is:

String iniPath = getIniPath (req.getPathTranslated(),"file.ini" );

Then I placed the ini-file (file.ini) in the AnotherDir directory and made
the getIniPath() method:

private String getIniPath(String fullPath, String iniFile)
{
     StringTokenizer st = new StringTokenizer(fullPath,"\\");
     boolean ready = false;
     String nextToken = "";
     String returnPath = "";

     while(!ready && st.hasMoreTokens())
     {
          nextToken = st.nextToken();
          if(nextToken.equals("servlet"))
               ready = true;
          else
               returnPath += (nextToken + "\\");
     }
     return returnPath + iniFile;
}

Works fine when the webserver creates a virtual 'servlet' directory.
Hope this can help some of your problems.


Terje Hermanseter
PMSC, Oslo Norway





Sun JunXu <[EMAIL PROTECTED]> on 12.01.2000 09:53:54

Please respond to "A mailing list for discussion about Sun Microsystem's
      Java              Servlet API Technology."
      <[EMAIL PROTECTED]>

To:   [EMAIL PROTECTED]
cc:    (bcc: Terje Hermanseter/PMSN)
Subject:  Re: relative path in servlet




thanks,
it doesn't work. getRealPath(String) cann't return any path.
and what I want is read from a normal virtual directory on webserver into a
servlet mounted directory, is there other way?

thank you very much.

sincerely

> ----------
> From:         Francesca Quattrocchi[SMTP:[EMAIL PROTECTED]]
> Reply To:     A mailing list for discussion about Sun Microsystem's Java
> Servlet API Technology.
> Sent:         Wednesday, January 12, 2000 4:19 PM
> To:   [EMAIL PROTECTED]
> Subject:      Re: relative path in servlet
>
> You have to use getRealPath(String) method of ServletContext:
>
> ex:
>
> String virtual_path = "/something/somepage.txt";
> ServletContext sc = getServletContext();
> String physical_path = sc.getRalPath(virtual_path);
>
> I hope will be helpfull!
> F.
>
> > -----Original Message-----
> > From: Sun JunXu [SMTP:[EMAIL PROTECTED]]
> > Sent: Wednesday, January 12, 2000 8:55 AM
> > To:   [EMAIL PROTECTED]
> > Subject:      relative path in servlet
> >
> > hi, there,
> > how to use a relative path in a servlet? ie. a file exists in
> > /home/admin/file.txt, and on webserver, its virtual directory is
> > 127.0.0.1/apps, in 127.0.0.1/appsclass/app.class, I want to get this
> file
> > through relative path   ../apps/file.txt.
> > File file=new File("../apps/file.txt");
> >
> > thanks a lot.
> >
> > sincerely
> > sun
> >
> >
>
__________________________________________________________________________
> > _
> > 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
>

___________________________________________________________________________
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