Reputation: 831
I am trying to open a text file from jsp by specifying its path from local drive. Here is the code snippet which doesnt working.
Source for bro.jsp:
<html>
<body>
<a href="D:/data.txt">Click Me</a>
</body>
</html>
Almost the similar code with HTML extension works.
Source for bro.html:
<html>
<body>
<a href="D:/data.txt">Click Me</a>
</body>
</html>
I can understand the server may not able to read the files from local drive. Hence I have changed the path to several ways like this, but none of them are working from JSP. Any ideas?
<a href="//D:/data.txt">Click Me</a>
<a href="file://D:/data.txt">Click Me</a>
<a href="file:///D:/data.txt">Click Me</a>
<a href="file://localhost/D:/data.txt">Click Me</a>
Upvotes: 0
Views: 4581
Reputation: 2381
I agree with the answer before, and want to add a clarification. It's not really a JSP issue, but and issue of HTTP and browsers.
if you create an html file (not even jsp) with the following:
<a href="file://d://data.txt">click</a>
Then the browser looks into the D: drive of the local machine (e.g. the home machine of your user, not the server). Furthermore, on most cases it would fail due to browser security restrictions. What you usually want, is for the browser to ask for a page on the server machine, and one solution is indeed to go back to the application (href="/myapp/test/data.txt") and ask it to server the page, assuming it has it.
Upvotes: 1
Reputation: 13844
The other way is to keep data.txt
inside web-inf folder and inside web-inf keep another folder say for example test
that is web-inf/test/data.txt
.
Then use this way
<a href="${pageContext.request.contextPath}/test/data.txt">Click Me</a>
Upvotes: 1