【发布时间】:2012-11-12 10:30:00
【问题描述】:
我的 jsp 代码遇到了一些问题。这是代码[Get_Values.java]:
public class Get_values {
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String msisdn = request.getParameter("msisdn");
temp = new Test().parseXml();
out.println("<b><font color='blue'>MSISDN :</font></b>" + "<b>" + temp[0] + "</b>" + "<br>");
}
}
这是 Test.java 的代码
public class Test {
public String [] temp= new String [50];
public String [] parseXml() {
SAXParser sp = factory.newSAXParser();
sp.parse("test.xml", handler);
DefaultHandler handler = new DefaultHandler() {
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if (qName.equalsIgnoreCase("location")) {
nodeName = attributes.getValue(qName);
}
}
public void endElement(String uri, String localName, String qName) throws SAXException {
if (qName.equalsIgnoreCase("x")) {
temp[0] = value;
} else if (qName.equalsIgnoreCase("y")) {
temp[1] = value;
} else if (qName.equalsIgnoreCase("z")) {
temp[2] = value;
}
}
public void characters(char ch[], int start, int length) throws SAXException {
value = new String(ch, start, length);
}
};
return temp;
}
}
但是当我执行“Get_values.war”文件时,temp 的值总是返回为空。但是当我执行java程序时,它工作正常。我认为当我执行war文件时“test.xml”没有被正确读取。可能是什么原因?我应该在我的 jsp 程序中明确包含该文件吗?
【问题讨论】:
-
您尝试过使用调试器吗?
-
我没有看到
temp在你的Test类中的任何地方声明 -
@abu 抱歉省略了温度。复制问题。
-
@home 我尝试使用调试器,但程序在此“sp.parse("test.xml", handler);" 处遇到断点线。我不知道该怎么做。是不是因为 test.xml 没有被读取?
-
@user1640534 :可能是因为 test.xml 的路径错误。尝试将您的 test.xml 放在根文件夹中