Ler XML na pasta WEB-INF

3 respostas
G

Criei uma aplicação e tenho um arquivo .xml que uso para algumas configurações. Já está tudo funcionando mas eu gostaria de colocar este xml na pasta WEB-INF. Para meus testes eu coloquei no C:/ da máquina e indiquei da seguinte maneira:

file = new File("C://Mail-config.xml");

Mas gostaria de colocar no WEB-INF da aplicação porque neste arquivo tem algumas informações como a senha do E-mail do usuário que envia. Só que eu não estou conseguindo ler o arquivo lá. Acho que não estou sabendo indicar o caminho. Alguém pode me ajudar?

3 Respostas

G

Para pegar o caminho utilize o método getRealPath da interface ServletContext:

“Querida API”:
getRealPath

public java.lang.String getRealPath(java.lang.String path)

Returns a String containing the real path for a given virtual path. For example, the path "/index.html" returns the absolute file path on the server's filesystem would be served by a request for "http://host/contextPath/index.html", where contextPath is the context path of this ServletContext.

The real path returned will be in a form appropriate to the computer and operating system on which the servlet container is running, including the proper path separators. This method returns null if the servlet container cannot translate the virtual path to a real path for any reason (such as when the content is being made available from a .war archive).

Parameters:
    path - a String specifying a virtual path
Returns:
    a String specifying the real path, or null if the translation cannot be performed</blockquote>

É sempre bom ter a API em mãos para ajudar quando surgirem dúvidas desse tipo.

http://java.sun.com/j2ee/1.4/docs/api/

G

Obrigado Guilherme. Eu sempre procuro dar uma olhada na API, mas como esta parte de JEE eu estou começando agora, fiquei meio perdido.

Mais uma vez, obrigado! :slight_smile:

G

gilmarcand:
Obrigado Guilherme. Eu sempre procuro dar uma olhada na API, mas como esta parte de JEE eu estou começando agora, fiquei meio perdido.

Mais uma vez, obrigado! :)

Sem problema, se tiver dúvida em relação a utilização do método é só postar aqui.

Criado 3 de abril de 2008
Ultima resposta 3 de abr. de 2008
Respostas 3
Participantes 2