Static and temporary files in servlets

by

Put not-to-be-web-accessible static files under WEB-INF. If you need a real path – e.g. to pass outside Java – and your container unpacks to a filesystem then get it as:

request.getServletContext().getRealPath("/WEB-INF/file")

or, if stream access within Java is acceptable (this works even if the .war isn’t unpacked to a filesystem):

request.getServletContext().getResourceAsStream("/WEB-INF/file")

For temporary files, use:

File tempDir = (File) request.getServletContext().getAttribute(ServletContext.TEMPDIR);
File tempFile = File.createTempFile("prefix-", ".tmp", tempDir);

(thanks) (thanks)

Insert Photo

Paragraph

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: