Meaningless Drivel is fun!*
The moose likes XML and Related Technologies and the fly likes Servlet cannt find path of xml file[jbuilder 7.0 servlet,] Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Servlet cannt find path of xml file[jbuilder 7.0 servlet,] " Watch "Servlet cannt find path of xml file[jbuilder 7.0 servlet,] " New topic
Author

Servlet cannt find path of xml file[jbuilder 7.0 servlet,]

shaonan zhang
Greenhorn

Joined: Dec 12, 2002
Posts: 9

why ? I use the Jbuilder7.0 , Jdk1.4 and Jdom8.0
my file selectdata.xml is at D:\wepapp\defaultroot\selectdata.xml.
In servlet.I use this
Document doc = sb.build(new FileInputStream("/selectdata.xml")); but I cannt get it .
Error message:
java.io.FileNotFoundException: \selectdata.xml at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.(FileInputStream.java:103)
at java.io.FileInputStream.(FileInputStream.java:66)
at webapp.XmlSelectSet.doPost(XmlSelectSet.java:32)
****************************
my servlet code is:
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import org.jdom.input.*;
import org.jdom.output.*;
import org.jdom.*;
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
String nodeName=request.getParameter("nodeName");
String addItem=request.getParameter("addItem");
String delItem=request.getParameter("delItem");
String itemText=request.getParameter("Description");
int item;
try{
SAXBuilder sb = new SAXBuilder();
Document doc = sb.build(new FileInputStream("/selectdata.xml"));//here if I use absolute path, can get this xml file. but, of course xml data cannt access.
Element root = doc.getRootElement();
if(addItem==null||addItem.length()==0){
item=Integer.parseInt(delItem);
Element selectTitle = root.getChild(nodeName);
java.util.List selectItem=selectTitle.getChildren();
selectItem.remove(item);
}else{
item=Integer.parseInt(addItem);
Element selectTitle = root.getChild(nodeName);
java.util.List selectItem=selectTitle.getChildren();
int l=selectItem.size();
Element select=new Element("select"+l);
select.setText(itemText);
selectItem.add(l,select);
}
XMLOutputter outp = new XMLOutputter("\t", false,"Shift_JIS");
outp.output(doc, new FileOutputStream("/selectdata.xml"));
}catch(JDOMException e){out.print(e.toString());}
****************************
my selectdata.xml code:
<?xml version="1.0" encoding="Shift_JIS"?>
<!-- save web all select element concent -->
<selectcon>
<collectmoney>
<position>\siyikyu\SikyuMessage.html</position>
<select1>--NO Select--</select1>
<select2>China</select2>
<select3>New York Mets</select3>
<select4>Japan</select4>
</collectmoney>
<useDiffer>
<position>\siyikyu\SikyuMessage.html</position>
<select1>--NO Select--</select1>
<select2>use1</select2>
<select3>use2</select3>
<select4>use3</select4>
</useDiffer>
<askDiffer>
<position>\siyikyu\SikyuMessage.html</position>
<select1>--NO Select--</select1>
<select2>ask1</select2>
<select3>ask2</select3>
<select4>ask3</select4>
</askDiffer>
</selectcon>
please help me. the servlet code is correct? How can I do ? Thanks
[ January 15, 2003: Message edited by: shaonan zhang ]
[ January 15, 2003: Message edited by: shaonan zhang ]
[ January 15, 2003: Message edited by: shaonan zhang ]
[ January 16, 2003: Message edited by: shaonan zhang ]

study hard, day day up!
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Shaonan,
The only way to achieve this would be:
1. Use an absolute file path to the hard drive location were the file sits
2. Change the server's policy file to allow your code to do so (otherwise you will get security exceptions)
Obviously this is not an easy task...
In a web application, you should not be seing files that are located outside of the web application archive. This is for security reasons.
Hence, the selectdata.xml file should be moved to the /WEB-INF/classes directory of your web application.
And then, instead of using:

use the getResourceAsStream method of the servlet context:

Cheers
[ January 16, 2003: Message edited by: Beno�t d'Oncieu ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Servlet cannt find path of xml file[jbuilder 7.0 servlet,]