Java 生成时找不到符号请求。getServletContext()

Java 生成时找不到符号请求。getServletContext(),java,servlets,compiler-errors,ant,classpath,Java,Servlets,Compiler Errors,Ant,Classpath,我一直在编写一个脚本来构建一个项目,创建War文件并将其部署到Tomcat上。在执行脚本时,特别是在javac命令期间,我遇到了以下错误。 请注意,我试图在没有Eclipse的情况下实现它,因此我无法直接集成Tomcat7 <?xml version="1.0" encoding="UTF-8"?> <project xmlns:ant="antlib:net.sf.antcontrib" name="Build Script For Deploy" default="NN-b

我一直在编写一个脚本来构建一个项目,创建War文件并将其部署到Tomcat上。在执行脚本时,特别是在javac命令期间,我遇到了以下错误。 请注意,我试图在没有Eclipse的情况下实现它,因此我无法直接集成Tomcat7

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:ant="antlib:net.sf.antcontrib" name="Build Script For Deploy" default="NN-build" >
   
   <!-- This sript will read required proeprty values from below properties file -->
   <property file="upgrade.properties" />
   
   <target name="NN-build" depends="compile" description="NN VM build target with CDT update" /> 
   
   <!--Keeping all the required jar files to classpath. 
   All the ANT tasks used in the rest of the script will be executed from below jar files Hence they need to be on classpth. -->
   <path id="svnant.classpath">
       <fileset dir= "${lib.dir}" >
            <include name= "**/*.jar" />
        </fileset>
    </path>   

    <path id = "catalina-ant-classpath">
        <fileset dir="${appserver.lib}">
            <include name = "catalina-ant.jar" />
        </fileset>
    </path>

    <taskdef name="install" classname = "org.apache.catalina.ant.DeployTask">
        <classpath refid = "catalina-ant-classpath" />
    </taskdef>  

   <!-- Defining all the required ANT tasks which will be used in the rest of the script -->
    <typedef resource="org/tigris/subversion/svnant/svnantlib.xml" classpathref="svnant.classpath" />
    <taskdef resource="net/sf/antcontrib/antlib.xml">
        <classpath>
            <pathelement location="${lib.dir}/ant-contrib-1.0.jar"/>
        </classpath>
    </taskdef>
   

  <property name="src.dir" location="${dest.home}/src" />
  <property name="classes.dir" location="${dest.home}/build/classes"/>
  <property name="dist.dir" location="${dest.home}/WebContent/WarFile" />
  <path id="compile.classpath">
    <fileset dir="${dest.home}/WebContent/WEB-INF/lib">
        <include name="*.jar" />
    </fileset>
  </path>

    <!-- Target for deleting the existing directories-->
  <target name="clean">
    <delete dir="${classes.dir}" />
    <delete dir="${dist.dir}" />
  </target>
  
  <!-- Target for creating the new directories-->
  <echo>....Creating new directories.... </echo>
  <target name="makedir">
    <mkdir dir="${classes.dir}" />
    <mkdir dir="${dist.dir}" />
  </target>

    <!-- Target for compiling the java code-->
  <target name="compile" depends="clean, makedir">
    <javac destdir="${classes.dir}" debug="true" 
    deprecation="false" optimize = "false" failonerror = "true" >
    <src path="${src.dir}" />
    <classpath refid="compile.classpath" />
    </javac>
  </target>
  
</project>

java:43: error: cannot find symbol ServletContext context = request.getServletContext();    [javac]                                         ^
    
[javac]   symbol:   method getServletContext()
[javac]   location: variable request of type HttpServletRequest
[javac] Note: Some input files use unchecked or unsafe operations.
[javac] Note: Recompile with -Xlint:unchecked for details.
[javac] 1 error

BUILD FAILED
C:\task\task-upgrade.xml:97: Compile failed; see the compiler error output for
 details.

   
   
   
   
    
   
   
   
  
       
   
    
   
  
  
  
  
  
    
  
    
    
  
  
  
…正在创建新目录。。。。
  
    
    
  
  
    
  
  
java:43:错误:找不到符号ServletContext=request.getServletContext();[javac]^
    
[javac]符号:方法getServletContext()
[javac]位置:HttpServletRequest类型的变量请求
[javac]注意:某些输入文件使用未经检查或不安全的操作。
[javac]注意:使用-Xlint重新编译:未选中以获取详细信息。
[javac]1错误
构建失败
C:\task\task upgrade.xml:97:编译失败;有关详细信息,请参阅编译器错误输出
细节。

在Eclipse中导入项目时,我收到了相同的错误,但添加了服务器运行时,Tomcat7将其修复。

在构建J2EE应用程序时,用于编译的类路径需要与运行时使用的类路径不同。因为您的web应用程序使用的是J2EE API,所以需要针对它进行编译。但是一旦部署,这些API将由您选择的容器(这里是Tomcat)提供,因此它们不应该是web应用程序的一部分

因此,在您的具体示例中,您需要javaxservlet api。它的jar需要添加到编译类路径中,但不能放入
WEB-INF/lib
文件夹中

以下是可能适合您需要的罐子列表:


构建J2EE应用程序时,用于编译的类路径需要与运行时使用的类路径不同。因为您的web应用程序使用的是J2EE API,所以需要针对它进行编译。但是一旦部署,这些API将由您选择的容器(这里是Tomcat)提供,因此它们不应该是web应用程序的一部分

因此,在您的具体示例中,您需要javaxservlet api。它的jar需要添加到编译类路径中,但不能放入
WEB-INF/lib
文件夹中

以下是可能适合您需要的罐子列表:


您应该将相同的服务器运行时添加到属性
compile.classpath
。您应该将相同的服务器运行时添加到属性
compile.classpath