Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to implemen commons.digester in Servlet  RSS feed

 
k.harisudan kuppuswami
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am using Commons.Digester in java application with main().
It works fine.
but when i use commons.Digester in Servlet (because i need to send XML document as parameter to Servlet),
It throws an error of
"
Begin event threw exception
java.lang.ClassNotFoundException: No ClassLoaders found for"

i properly installed jar file.(It works in application).

I have taken example from

http://www.devx.com/Java/Article/21832

with modified XML document of


<?xml version='1.0'?>
<students>
<student>
<name>Java Boy</name>
<course>JSP</course>
</student>
<dept>E-Learning</dept>
<student>
<name>Java Girl</name>
<course>EJB</course>
</student>
<dept>E-Learning</dept>
</students>
 
Nathaniel Stoddard
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you need to put the commons-digester JAR file anywhere in your WAR so that your code can reference it?
 
k.harisudan kuppuswami
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hai
i included digester jar files in Project using Eclipse.
It works fine in Java Application with main();
but when i implement within servlet it displays the error of

java.lang.ClassNotFoundException: No ClassLoaders is thrown for java classes for CStudents,CStudent class needed for Parent & Child node.


I am giving all Java files (All are in same web module) .

Class files are:
------------------

1)CStudent.java 2)CStudents.java 3)StudentParser.java
the xml file is send as reader object.

public class CStudents {
//dept variable for dept property
private String dept;
//private variable for child node student
private CStudent student;
// get & set method for dept property
public String getDept()
{
return dept;
}
public void setDept(String dept)
{
this.dept = dept;
}
//get&set method for Child node property
public CStudent getStudent()
{
return student;
}
public void setStudent(CStudent student)
{
this.student = student;
}
}

public class CStudent {
//define two variable equivalent to no of properties to Student Node(child)
private String name="";
private String course="";
//get & set method for Name property
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
// get & set method for Course property
public String getCourse()
{
return course;
}
public void setCourse(String course)
{
this.course = course;
}
}
StudentParserNew.Java
----------------------

mport java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.digester.Digester;
public class StudentParserNew extends HttpServlet {
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
try
{
Digester digester = new Digester();
digester.setValidating( false );
digester.addObjectCreate("students",CStudents.class);
digester.addObjectCreate("students/student",CStudent.class);
digester.addBeanPropertySetter("students/student/name","name");
digester.addBeanPropertySetter("students/student/course","course");
digester.addSetNext( "students/student","setStudent");
digester.addBeanPropertySetter("students/dept","dept");
Reader reader = new StringReader(
"<?xml version='1.0'?><students><student><name>Java Boy</name><course>JSP</course>"+
"</student><dept>E-Learning</dept><student><name>Java Girl</name><course>EJB</course></student>"+
"<dept>E-Learning</dept></students>"
);
CStudents cstudents = (CStudents)digester.parse( reader );
System.out.println("Result is "+cstudents.toString() );
}
catch( Exception exc )
{
exc.printStackTrace();
}
}
}
 
k.harisudan kuppuswami
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hai
i included digester jar files in Project using Eclipse.
It works fine in Java Application with main();
but when i implement within servlet it displays the error of

java.lang.ClassNotFoundException: No ClassLoaders is thrown for java classes for CStudents,CStudent class needed for Parent & Child node.


I am giving all Java files (All are in same web module) .

Class files are:
------------------

1)CStudent.java 2)CStudents.java 3)StudentParser.java
the xml file is send as reader object.

public class CStudents {
//dept variable for dept property
private String dept;
//private variable for child node student
private CStudent student;
// get & set method for dept property
public String getDept()
{
return dept;
}
public void setDept(String dept)
{
this.dept = dept;
}
//get&set method for Child node property
public CStudent getStudent()
{
return student;
}
public void setStudent(CStudent student)
{
this.student = student;
}
}

public class CStudent {
//define two variable equivalent to no of properties to Student Node(child)
private String name="";
private String course="";
//get & set method for Name property
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
// get & set method for Course property
public String getCourse()
{
return course;
}
public void setCourse(String course)
{
this.course = course;
}
}
StudentParserNew.Java
----------------------

mport java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.digester.Digester;
public class StudentParserNew extends HttpServlet {
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
try
{
Digester digester = new Digester();
digester.setValidating( false );
digester.addObjectCreate("students",CStudents.class);
digester.addObjectCreate("students/student",CStudent.class);
digester.addBeanPropertySetter("students/student/name","name");
digester.addBeanPropertySetter("students/student/course","course");
digester.addSetNext( "students/student","setStudent");
digester.addBeanPropertySetter("students/dept","dept");
Reader reader = new StringReader(
"<?xml version='1.0'?><students><student><name>Java Boy</name><course>JSP</course>"+
"</student><dept>E-Learning</dept><student><name>Java Girl</name><course>EJB</course></student>"+
"<dept>E-Learning</dept></students>"
);
CStudents cstudents = (CStudents)digester.parse( reader );
System.out.println("Result is "+cstudents.toString() );
}
catch( Exception exc )
{
exc.printStackTrace();
}
}
}
 
k.harisudan kuppuswami
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I finally zeroed the error point

digester.addObjectCreate("students",CStudents.class);
the above line is throwing error.

how to create class object in webmodule.
I am placing all class files in webmodule using Eclipse in JBoss.

I am not placing java/class files in web-inf dir .

when i get classloader for class file it returns as

"/tmp/Jetty_0_0_0_0_8080__TCweb/webapp/WEB-INF/classes/"

please help me to know how to load classes in web module?

thank you
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!