File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "" Watch "" New topic

Azz Romaysa
Ranch Hand

Joined: Dec 08, 2004
Posts: 66

I'm trting to send an attachment file using javaMail. My problem is if I set the whole path of the file (for example: C:/myFile.jsp), then it works. But not if I set a relative path.

This is the code:

Ans I call this method from my servlet

The exception that I get: \vrijblijvend_offerte.jsp (The system cannot find the path specified)

My structure:

Thank you,
[ October 22, 2005: Message edited by: Azz Romaysa ]
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13037
You "relative" file path usage depends on the "current" directory - something you can't control in a servlet. You can convert a relative path in a servlet context to an absolute path - see the ServletContext getRealPath method.
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

There are two methods you should read up on:
getRealPath and getResourceAsStream.

Both are methods of ServletContext

Note: getRealPath has a serious drawback. If you're running your application from a packed war file getRealPath will return null.

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Azz Romaysa
Ranch Hand

Joined: Dec 08, 2004
Posts: 66
Thank you very much,
The getRealPath() has fixed the problem.
I agree. Here's the link:
It's not a secret anymore!