【问题标题】:JSP program always returning null valueJSP 程序总是返回空值
【发布时间】: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 放在根文件夹中

标签: java xml jsp null


【解决方案1】:

终于成功了。那是因为路径问题。我必须给出完整的路径。

C:\\xampp\\tomcat\\webapps\\Location_API\\WEB-INF\\classes\\location\\test.xml

它就像魅力一样。感谢大家宝贵的时间。 :)

【讨论】:

  • 您可以使用/ 而不是\\。正斜杠 / 独立于操作系统。
  • @Eng.Fouad 感谢您的提示。我认为“/”取决于操作系统。
猜你喜欢
  • 1970-01-01
  • 2016-07-11
  • 1970-01-01
  • 1970-01-01
  • 2013-11-17
  • 1970-01-01
  • 1970-01-01
  • 2014-06-24
  • 1970-01-01
相关资源
最近更新 更多