日期:2014-05-20  浏览次数:20921 次

servlet环境搭建问题
servlet环境搭建问题:
如下
Java code

package test;
import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

public class HelloWorld extends HttpServlet

{

public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException
,IOException

{
    response.setContentType("text/html"); 

PrintWriter out = response.getWriter();
    out.println("<html><head><title>");
    out.println("This is my first Servlet");
    out.println("</title></head><body>");
    out.println("<h1>Hello,World!</h1>");
    out.println("</body></html>");
   
   
  }
}


在DOS中用JAVAC编译报错:
error:C:\apache-tomcat-6.0.35\lib\servlet-api.jar<javax/servlet/http/HttpServlet.class>中的类文件格式无效。 wrong version:49,expected 45
HelloWorld.java:8:未找到 class test.HelloWorld 的父类 javax.servlet.http.HttpServlet。
2 个错误

C:\apache-tomcat-6.0.35\lib\servlet-api.jar与
C:\Program Files (x86)\Java\jre6\lib\ext\servlet-api.jar都是有的,环境变量classpath也加了C:\apache-tomcat-6.0.35\lib\servlet-api.jar

------解决方案--------------------
试过可以编译,可能是jar包的问题
------解决方案--------------------
要正确的配置servlet-api.jar(该jar包在Tomcat安装目录下有,如:C:\Tomcat 5.5\common\lib\servlet-api.jar)。可用把该jar包复制到虚拟目录/WEB-INF/classes/lib目录(如果没有此目录就手动创建,如:D:\webstudy\WEB-INF\lib\servlet-api.jar),然后再编译。
Java code

package org.xiong.servletdemo;
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;
public class ServletDemo extends HttpServlet
{
    public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException
    {
    response.setContentType("text/html"); 
        PrintWriter out = response.getWriter();
    out.println("<html><head><title>");
    out.println("This is my first Servlet");
    out.println("</title></head><body>");
    out.println("<h1>Hello,World!</h1>");
    out.println("</body></html>");   
  }
  public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException
    {
        this.doGet(request,response);
    }
}

------解决方案--------------------
javac -cp jar路径 *.java
------解决方案--------------------
tomcat似乎需要配置java_home。 配置了吗?