首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web开发 >

大家帮忙看看这个应用 一老报java.lang.ClassNotFoundException: org.dom4j.DocumentHelper

2012-02-27 
大家帮忙看看这个应用 一老报java.lang.ClassNotFoundException: org.dom4j.DocumentHelper - Web 开发 /

大家帮忙看看这个应用 一老报java.lang.ClassNotFoundException: org.dom4j.DocumentHelper - Web 开发 / 其他
这是开始的那个jsp程序 
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  <base href="<%=basePath%>">
   
  <title>My JSP 'xml.jsp' starting page</title>
   
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">  
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript" src="scripts/jquery-1.6.2.js"></script>
<script type="text/javascript">
$(function() {
  $("#button1").click(function() {
  $.ajax({
  type:"POST",
  url:"XMLServlet",
  dataType:"XML",
  data:{"name":$("#name").val()},
  success:function(returnedData) {
  var id = $(returnedData).find("id").text();
  var name = $(returnedData).find("name").text();
  var age = $(returnedData).find("age").text();
  var address = $(returnData).find("address").text();
  alert(id);
  }
  });
  });

});
</script>


  </head>
  
  <body>
  <select id="name">
  <option value="zhangsan">zhangsan</option>
  <option value="lisi">lisi</option>
  </select>
  <input type="button" value="Get content from server" id="button1"/>
  </body>
</html>



下面是转到的servlet代码

package com.xtu.ajax;



import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

import com.xtu.ajax.model.Person;

public class XMLServlet extends HttpServlet {
  protected void doGet(HttpServletRequest req, HttpServletResponse resp)
  throws ServletException, IOException {
  String name = req.getParameter("name");
  Person person = new Person();
  if(name.equals("zhangsan")) {
  person.setName("zhangsan");
  person.setAge(30);
  person.setAddress("beijing");
  person.setId(551218);
  }


  else if(name.equals("lisi")) {
  person.setName("lisi");
  person.setAge(20);
  person.setAddress("shanghai");
  person.setId(551219);
  }
 
  /*Document document = DocumentHelper.createDocument();
  Element rootElement = document.addElement("users");
  rootElement.addComment("this is a comment!!");
  Element userElement = rootElement.addElement("user");
 
  Element idElement = userElement.addElement("id");
  Element nameElement = userElement.addElement("name");
  Element ageElement = userElement.addElement("age");
  Element addressElement = userElement.addElement("address");
 
  idElement.setText(person.getId() + "");
  nameElement.setText(person.getName());
  ageElement.setText(person.getAge() + "");
  addressElement.setText(person.getAddress());
 
  PrintWriter out = resp.getWriter();
  OutputFormat format = OutputFormat.createPrettyPrint();
  format.setEncoding("utf-8");
  format.setIndent(" ");
  XMLWriter xmlWriter = new XMLWriter(out,format);
  xmlWriter.write(document);
  out.flush();*/
 
 
  Document document = DocumentHelper.createDocument();

Element rootElement = document.addElement("users");

rootElement.addComment("This is a comment!");

Element userElement = rootElement.addElement("user");

Element idElement = userElement.addElement("id");
Element nameElement = userElement.addElement("name");
Element ageElement = userElement.addElement("age");
Element addressElement = userElement.addElement("address");

idElement.setText(person.getId() + "");
nameElement.setText(person.getName());
ageElement.setText(person.getAge() + "");
addressElement.setText(person.getAddress());

resp.setContentType("text/xml; charset=utf-8");
resp.setHeader("pragma", "no-cache");
resp.setHeader("cache-control", "no-cache");

PrintWriter out = resp.getWriter();

OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("utf-8");

XMLWriter xmlWriter = new XMLWriter(out, format);

xmlWriter.write(document);

out.flush();
  }
   
  @Override
  protected void doPost(HttpServletRequest req, HttpServletResponse resp)
  throws ServletException, IOException {
  this.doGet(req,resp);
  }
}

在myeclipse下运行一老报错 

严重: Servlet.service() for servlet XMLServlet threw exception
java.lang.ClassNotFoundException: org.dom4j.DocumentHelper
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1680)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1526)
at com.xtu.ajax.XMLServlet.doGet(XMLServlet.java:62)
at com.xtu.ajax.XMLServlet.doPost(XMLServlet.java:99)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)


at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:662)


[解决办法]
还是自己解决了 希望有遇到这个情况的人 能够参考参考 
搞了2个小时才发现的
因为运行时没有找到第三方类库的包,所以报错,将dom4j的包复制到javahome\Java\jre6\lib\ext下,运行程序,OK! 所以对于jdk安装目录下的两个文件夹,jdk和jre,jdk用于开发环境,应该包括程序的编译,如果程序中包含第三方类库,可直接将第三方jar包复制到扩展目录下。而jre用于运行环境,如果程序中包含第三方类库,要运行程序,将第三方jar包复制至扩展目录下,在类加载时,会自动加载该目录下的类库。

热点排行