Mohammed Joraid
Mohammed Joraid

Reputation: 6480

Get website URL for XML file JSP-Java

Let's say i have created the file

String path = application.getRealPath("userSearchFolder");
String name = path + "/" + (String) session.getAttribute("username") + ".xml";
File file = new File(name);

And later I want to make it available as a link, for example

 <a href"<%=file.toURI()%>">File</a>

What happens is I get the directory path not url path ->

file:/D:/Documents/NetBeansProjects/2012/GATE_Project/build/web/userSearchFolder/mjoraid.txt.xml

And when it reaches Firefox, I hover over the link and what i see is

file:///D:/Documents/NetBeansProjects/2012/GATE_Project/build/web/userSearchFolder/mjoraid.xml

When I right click and choose Copy link Location and paste it in URL the xml file opens, but when I click the link, nothing happens.

How could I get a link like this

http://localhost:8080/GATE_Project/somepage/somepage/mjoraid.xml

Upvotes: 0

Views: 980

Answers (3)

Mohammed Joraid
Mohammed Joraid

Reputation: 6480

Ok, i did it manually, similar to how i used to do it in php, create a variable that contains the website main directory.

  String searchFolderURL = "http://localhost:8080/GATE_Project/userSearchFolder/"; 

and then

 <a href="<%=searchFolderURL + file.getName()%>" target="_blank"  >See original txt File </a>

Thanks btw.

Upvotes: 0

plasma147
plasma147

Reputation: 2211

You could use a servlet to serve the file.

This tutorial shows how to serve a pdf file(!)

the theory is the same:

  • you load the file in the servlet
  • Set any required headers
  • write the data to the response

The ContentType should probably be "application/xml".

Upvotes: 0

Joop Eggen
Joop Eggen

Reputation: 109577

The getRealPath will give a File system path (hence "real"), as opposed to web app path. So you cannot make it a href.

The following should suffice.

<a href="/userSearchFolder/${userName}.xml">

(Of course you are risking data mining for such public accessible XML files.)

Upvotes: 1

Related Questions