在tomcat下的servlet Error问题(比较长)~~先谢谢大家了!
一、有这么一道题:
一个Java 类 User 的定义如下 :
public class User {
private String name;
private String emailAddress;
private boolean male;
public String getEmailAddress() {
return emailAddress;
}
public void setEmailAddress(String address) {
this.emailAddress = address;
}
public boolean isMale() {
return male;
}
public void setMale(boolean male) {
this.male = male;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public User( String name, String emailAddress, boolean male ) {
this.name = name;
this.emailAddress = emailAddress;
this.male = male;
}
}
假设在 Velocity 的 runtime context 中已经存在一个 Java 容器类 ArrayList 的实例变量 $user 。
$user 的初始化方法为:
java.util.List user = new java.util.ArrayList(5);
user.add( new User( "John ", "john@some.domain ", true ) );
user.add( new User( " Alice ", "alice@other.domain ", false ) );
user.add( new User( "Tom ", "tom@other.domain ", true ) );
user.add( new User( "Elisa ", "elisa@some.domain ", false ) );
user.add( new User( "Zine ", "zine@other.domain ", true ) );
请写一个 velocity 模版,这个模版可以生成一个 html 页面,输出 $user 中包含的动态用户信息,要求:
1. 采用 table 的形式,每个用户一行,每行包括用户名, email 地址,性别三列信息。
2. 性别需要转换成 “ 男 ” , “ 女 ” ,不要直接输出 boolean 值。
3. 男用户输出行背景为蓝色,女用户输出行背景为红色。
二、我是这么做的:
TestVelocity.java代码如下:
//package testVelocity;
import java.io.StringWriter;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
public class TestVelocity{
public static void main( String[] args )throws Exception{
VelocityEngine ve = new VelocityEngine();
ve.init();
Template t = ve.getTemplate( "userTable.vm " );
VelocityContext context = new VelocityContext();
context.put( "name ", "Eiffel Qiu ");
context.put( "site ", "http://www.j2medev.com ");
java.util.List user = new java.util.ArrayList(5);
user.add( new User( "John ", "john@some.domain ", true ) );
user.add( new User( " Alice ", "alice@other.domain ", false ) );
user.add( new User( "Tom ", "tom@other.domain ", true ) );
user.add( new User( "Elisa ", "elisa@some.domain ", false ) );
user.add( new User( "Zine ", "zine@other.domain ", true ) );
context.put( "user ", user);
StringWriter writer = new StringWriter();
t.merge( context, writer );
System.out.println( writer.toString() );
}
}
Uesr.java代码:
//package testVelocity;
public class User {
private String name;
private String emailAddress;
private boolean male;
public String getEmailAddress() {
return emailAddress;
}
public void setEmailAddress(String address) {
this.emailAddress = address;
}
public boolean isMale() {
return male;
}
public void setMale(boolean male) {
this.male = male;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public User( String name, String emailAddress, boolean male ) {
this.name = name;
this.emailAddress = emailAddress;
this.male = male;
}
}
userTable.vm代码:
<html>
<title> TestVelocity </title>
<body>
<h1> 采用 table 的形式显示: </h1> <br/>
<table border=1>
<tr>
<td> Name </td>
<td> EMailAddress </td>
<td> Male or Female </td>
</tr>
#foreach($userInstance in $user)
<tr>
<td> $userInstance.name </td>
<td> $userInstance.emailAddress </td>
#if($userInstance.male)
<td bgcolor= "blue "> 男 </td>
#else
<td bgcolor= "red "> 女 </td>
</tr>
#end
</table>
</body>
</html>
web.xml代码:
<?xml version= "1.0 " encoding= "UTF-8 "?>
<web-app id= "WebApp_ID " version= "2.4 " xmlns= "http://java.sun.com/xml/ns/j2ee " xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance " xsi:schemaLocation= "http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd ">
<display-name> TestVelocity </display-name>
<description> TestVelocity </description>
<welcome-file-list>
<welcome-file> index.html </welcome-file>
<welcome-file> index.htm </welcome-file>
<welcome-file> index.jsp </welcome-file>
<welcome-file> default.html </welcome-file>
<welcome-file> default.htm </welcome-file>
<welcome-file> default.jsp </welcome-file>
</welcome-file-list>
<servlet>
<servlet-name> test </servlet-name>
<servlet-class> cn.ac.ict.TestVelocity </servlet-class>
</servlet>
<servlet-mapping>
<servlet-name> test </servlet-name>
<url-pattern> /test </url-pattern>
</servlet-mapping>
</web-app>
三、用tomcat打开就这样的错误:
HTTP Status 500 -
--------------------------------------------
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
javax.servlet.ServletException: Error allocating a servlet instance
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:118)
org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:160)
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:799)
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:705)
org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:577)
org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)
java.lang.Thread.run(Thread.java:534)
root cause
java.lang.NoClassDefFoundError: cn/ac/ict/TestVelocity (wrong name: TestVelocity)
java.lang.ClassLoader.defineClass0(Native Method)
java.lang.ClassLoader.defineClass(ClassLoader.java:539)
java.security.SecureClassLoader.defineClass(SecureClassLoader.java:123)
org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:1634)
org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:860)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1307)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1189)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:118)
org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:160)
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:799)
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:705)
org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:577)
org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)
java.lang.Thread.run(Thread.java:534)
note The full stack trace of the root cause is available in the Apache Tomcat/5.0.28 logs.
--------------------------------------------
Apache Tomcat/5.0.28
四、我的classpath是
.;C:\j2sdk1.4.2_14\lib\dt.jar;C:\j2sdk1.4.2_14\lib\tools.jar;C:\j2sdk1.4.2_14\jre\lib\rt.jar;C:\Tomcat 5.0\common\lib\servlet-api.jar;C:\Tomcat 5.0\common\lib;C:\Tomcat 5.0\webapps\velocity\WEB-INF\lib\velocity-dep-1.5.jar;C:\Tomcat 5.0\webapps\velocity\WEB-INF\lib\velocity-1.5.jar;C:\Tomcat 5.0\webapps\velocity\WEB-INF\classes\cn\ac\ict\TestVelocity.class
五、我的j2dk1.4.2_14
tomcat5.0.28
j2re1.4.2_14
六、为什么不能正确显示出网页呢?
我在网上搜了一下,有的说*.java文件加上引入的包package就行了,但我前边两个java文件如果加了包就不能编译通过,去掉包就编译顺利通过,郁闷阿~~找高人指点!!
[解决办法]
加上package 然后编译时候 javac -d . 编译
[解决办法]
TestVelocity这个类定义不是Servlet。
就是说你在Web.xml里定义的那个cn.ac.ict.TestVelocity的类必须继承HttpServlet,然后写在main方法里的那些东西也要改在overload的doGet或者doPost或者service里边,还有就是输出也不是System.out而是写入HttpResponse的out里。
[解决办法]
<servlet>
<servlet-name> test </servlet-name>
<servlet-class> cn.ac.ict.TestVelocity </servlet-class>
</servlet>
TestVelocity.java代码如下:
//package testVelocity;
import java.io.StringWriter;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
public class TestVelocity{
在web.xml中声明的类为cn.ac.ict.TestVelocity,在包cn.ac.ict里,而你定义的类的包声明是package testVelocity;
还有要在web-inf/classes目录下建cn/ac/ict子目录放TestVelocity类
我的异常网推荐解决方案:The server encountered an internal error () that prevented it from fulfilling this request.,http://www.myexception.cn/java-web/317.html