如何导入javax。我的Eclipse项目中的servlet API ?

时间:2021-10-22 11:00:24

I want to develop with Servlets in Eclipse, but it says that the package javax.servlet cannot be resolved. How can I add javax.servlet package to my Eclipse project?

我希望在Eclipse中使用servlet进行开发,但是它说包javax。servlet无法解决。我如何添加javax。servlet包到我的Eclipse项目?

14 个解决方案

#1


373  

Ensure you've the right Eclipse and Server

Ensure that you're using at least Eclipse IDE for Java EE developers (with the EE). It contains development tools to create dynamic web projects and easily integrate servletcontainers (those tools are part of Web Tools Platform, WTP). In case you already had Eclipse IDE for Java (without EE), and manually installed some EE related plugins, then chances are that it wasn't done properly. You'd best trash it and grab the real Eclipse IDE for Java EE one.

确保您正在使用至少Eclipse IDE为Java EE开发人员(使用EE)。它包含开发工具来创建动态的web项目,并轻松地集成servletcontainer(这些工具是web工具平台WTP的一部分)。如果您已经为Java(没有EE)提供了Eclipse IDE,并且手动安装了一些与EE相关的插件,那么很有可能它做得不够好。您最好将它丢弃,并为Java EE one获取真正的Eclipse IDE。

You also need to ensure that you already have a servletcontainer installed on your machine which implements at least the same Servlet API version as the servletcontainer in the production environment, for example Apache Tomcat, Oracle GlassFish, JBoss AS/WildFly, etc. Usually, just downloading the ZIP file and extracting it is sufficient. In case of Tomcat, do not download the EXE format, that's only for Windows based production environments. See also a.o. Several ports (8005, 8080, 8009) required by Tomcat Server at localhost are already in use.

您还需要确保你在你的机器上已经安装了一个servletcontainer实现至少相同的Servlet API版本servletcontainer在生产环境中,例如Apache Tomcat,Oracle GlassFish,JBoss / WildFly,等等。通常,只是下载ZIP文件和提取是充分的。在Tomcat的情况下,不要下载EXE格式,这只适用于基于Windows的生产环境。在本地主机上,Tomcat服务器需要的几个端口(8005,8080,8009)已经在使用。

A servletcontainer is a concrete implementation of the Servlet API. Note that the Java EE SDK download at Oracle.com basically contains GlassFish. So if you happen to already have downloaded Java EE SDK, then you basically already have GlassFish. Also note that for example GlassFish and JBoss AS/WildFly are more than just a servletcontainer, they also supports JSF, EJB, JPA and all other Java EE fanciness. See also a.o. What exactly is Java EE?

servletcontainer是Servlet API的具体实现。请注意,Oracle.com上的Java EE SDK基本包含了GlassFish。如果你已经下载了Java EE SDK,那么你基本上已经有了GlassFish。还要注意,例如GlassFish和JBoss作为/WildFly不仅仅是一个servletcontainer,它们还支持JSF、EJB、JPA和其他所有Java EE fanciness。Java EE到底是什么?


Integrate Server in Eclipse and associate it with Project

Once having installed both Eclipse for Java EE and a servletcontainer on your machine, do the following steps in Eclipse:

一旦在您的机器上安装了Java EE和servletcontainer的Eclipse,请在Eclipse中执行以下步骤:

  1. Integrate servletcontainer in Eclipse

    在Eclipse中集成servletcontainer

    a. Via Servers view

    通过服务器视图

    • Open the Servers view in the bottom box.
    • 打开底部框中的Servers视图。
    • Rightclick there and choose New > Server.
    • 右击并选择新的>服务器。
    • Pick the appropriate servletcontainer make and version and walk through the wizard.

      选择合适的servletcontainer制作和版本,并通过向导。

      如何导入javax。我的Eclipse项目中的servlet API ?

    b. Or, via Eclipse preferences

    或者,通过Eclipse首选项。

    • Open Window > Preferences > Server > Runtime Environments.
    • 打开窗口>偏好>服务器>运行时环境。
    • You can Add, Edit and Remove servers here.

      您可以在这里添加、编辑和删除服务器。

      如何导入javax。我的Eclipse项目中的servlet API ?

  2. Associate server with project

    把服务器和项目

    a. In new project

    新项目。

    • Open the Project Navigator/Explorer on the left hand side.
    • 在左侧打开项目导航器/资源管理器。
    • Rightclick there and choose New > Project and then in menu Web > Dynamic Web Project.
    • 右击并选择新的>项目,然后在菜单Web >动态Web项目中。
    • In the wizard, set the Target Runtime to the integrated server.

      在向导中,将目标运行时设置为集成服务器。

      如何导入javax。我的Eclipse项目中的servlet API ?

    b. Or, in existing project

    b.或在现有项目中。

    • Rightclick project and choose Properties.
    • 右击项目并选择属性。
    • In Targeted Runtimes section, select the integrated server.

      在目标运行时部分,选择集成服务器。

      如何导入javax。我的Eclipse项目中的servlet API ?

    Either way, Eclipse will then automatically take the servletcontainer's libraries in the build path. This way you'll be able to import and use the Servlet API.

    无论哪种方式,Eclipse都会自动在构建路径中获取servletcontainer的库。这样您就可以导入和使用Servlet API了。


Never carry around loose server-specific JAR files

You should in any case not have the need to fiddle around in the Build Path property of the project. You should above all never manually copy/download/move/include the individual servletcontainer-specific libraries like servlet-api.jar, jsp-api.jar, el-api.jar, j2ee.jar, javaee.jar, etc. It would only lead to future portability, compatibility, classpath and maintainability troubles, because your webapp would not work when it's deployed to a servletcontainer of a different make/version than where those libraries are originally obtained from.

您应该在任何情况下都不需要在项目的构建路径属性中进行修改。首先,您不应该手动复制/下载/移动/包含像servlet-api这样的单独的servlet特定的库。jsp-api jar。el-api jar。jar,j2ee。javaee jar。它只会导致未来的可移植性、兼容性、类路径和可维护性问题,因为当它被部署到一个不同的make/版本的servletcontainer时,你的webapp就不会起作用了,而这些库最初是从这些库中获取的。

In case you're using Maven, you need to make absolutely sure that servletcontainer-specific libraries which are already provided by the target runtime are marked as <scope>provided</scope>.

如果您使用的是Maven,那么您需要确保目标运行时已经提供的servletcontainer特定库被标记为 提供的

Here are some typical exceptions which you can get when you litter the /WEB-INF/lib or even /JRE/lib, /JRE/lib/ext, etc with servletcontainer-specific libraries in a careless attempt to fix the compilation errors:

下面是一些典型的例外情况,当你在一个粗心的尝试去修复编译错误时,你可以把/WEB-INF/lib,甚至/JRE/lib/ JRE/lib/ext,等等。

#2


209  

如何导入javax。我的Eclipse项目中的servlet API ?

STEP 1

Go to properties of your project ( with Alt+Enter or righ-click )

转到项目属性(Alt+Enter或右击)

STEP 2

check on Apache Tomcat v7.0 under Targeted Runtime and it works.

在目标运行时检查Apache Tomcat v7.0,它可以工作。

#3


70  

Little bit difference from Hari:

与Hari有一点不同:

Right click on project ---> Properties ---> Java Build Path ---> Add Library... ---> Server Runtime ---> Apache Tomcat ----> Finish.

右键单击项目——>属性——> Java构建路径——>添加库…--->服务器运行时---> Apache Tomcat --->结束。

#4


10  

Quick Fix- This worked in Eclipse - Right Click on project -> Properties -> Java Build Path (Tab) -> Add External JARs -> locate the servlet api jar implementation (if Tomcat - its named servlet-api.jar) -> click OK. That's it !!

快速修复-这在Eclipse -右键单击项目->属性-> Java Build Path (Tab) ->添加外部jar ->定位servlet api jar实现(如果Tomcat -它的命名为servlet-api - jar) ->点击OK。就是这样! !

#5


10  

Include servlet-api.jar from your server lib folder.如何导入javax。我的Eclipse项目中的servlet API ?

包括servlet api。从服务器lib文件夹中获取jar。

Do this step

做这一步

如何导入javax。我的Eclipse项目中的servlet API ?

#6


4  

I know this is an old post. However, I observed another instance where in the project already has Tomcat added but we still get this error. Did this to resolve that:
Alt + Enter
Project Facets
On the right, next to details, is another tab "Runtimes". The installed tomcat server will be listed there. Select it.
Save the configuration and DONE!

我知道这是一个旧帖子。但是,我观察到另一个实例,在项目中已经添加了Tomcat,但是我们仍然会得到这个错误。这样做是为了解决这个问题:Alt +在右边的项目方面,在细节旁边,是另一个标签“运行时间”。安装的tomcat服务器将在那里列出。选择它。保存配置并完成!

Hope this helps someone.

希望这可以帮助别人。

#7


3  

you can simply copy the servlet-api.jar and copy that jar files into lib folder, which is in WEB-INF. then just clean and built your project, your errors will be solved.

您可以简单地复制servlet-api。将jar文件复制到lib文件夹中,该文件夹位于WEB-INF中。然后清理并建立你的项目,你的错误将被解决。

  **OR**

you can directly add jar files to library by using following steps.

您可以通过以下步骤直接将jar文件添加到库中。

  1. Right click on project.
  2. 右键单击项目。
  3. Go To Properties.
  4. 去的属性。
  5. Go to Java Build Path.
  6. 转到Java构建路径。
  7. Select Add Library option from tabs.
  8. 从选项卡中选择Add Library选项。
  9. Add Jar Files
  10. 添加Jar文件
  11. give path of your servlet-api.jar file.
  12. 提供您的servlet-api的路径。jar文件。
  13. Clean and build your project.
  14. 清洁并建立你的项目。

#8


3  

Add javax.servlet dependency in pom.xml. Your problem will be resolved.

添加javax。在pom . xml中servlet的依赖。你的问题将得到解决。

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.0.1</version>
    <scope>provided</scope>
</dependency>

#9


2  

From wikipedia.

从*。

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 HelloWorld extends HttpServlet {
  public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    PrintWriter out = response.getWriter();
    out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
                "Transitional//EN\">\n" +
                "<html>\n" +
                "<head><title>Hello WWW</title></head>\n" +
                "<body>\n" +
                "<h1>Hello WWW</h1>\n" +
                "</body></html>");
  }
}

This, of course, works only if you have added the servlet-api.jar to Eclipse build path. Typically your application server (e.g Tomcat) will have the right jar file.

当然,这只有在您添加了servlet-api之后才有效。jar到Eclipse构建路径。通常是应用服务器(e)。将有正确的jar文件。

#10


1  

I was getting a null pointer exception during project creation related to "Dynamic Web Module".

在与“动态Web模块”相关的项目创建过程中,我得到了一个空指针异常。

To get the project to compile (that is, to javax.servlet to import successfully) I had to go to project's Properties, pick Project Facets in the sidebar, tick Dynamic Web Module and click Apply.

让项目编译(即,javax)。要成功导入servlet)我必须访问项目的属性,在侧边栏中选择项目方面,在动态Web模块中进行标记,然后单击Apply。

Surprisingly, this time "Dynamic Web Module" facet installed correctly, and import started to work.

令人惊讶的是,这一次“动态Web模块”安装正确,导入开始工作。

#11


0  

You should above all never manually copy/download/move/include the individual servletcontainer-specific libraries like servlet-api.jar

首先,不要手动复制/下载/移动/包含像servlet-api.jar这样的单独的servlet特定库。

@BalusC,

@BalusC,

I would prefer to use the exact classes that my application is going to use rather than one provided by Eclipse (when I am feeling like a paranoid developer).

我更愿意使用我的应用程序将要使用的确切类,而不是Eclipse提供的类(当我感觉自己像一个偏执的开发人员时)。

Another solution would be to use Eclipse "Configure Build Path" > Libraries > Add External Jars, and add servlet api of whatever Container one chooses to use.

另一个解决方案是使用Eclipse“配置构建路径”>库>添加外部jar,并添加任意选择使用的容器的servlet api。

And follow @kaustav datta's solution when using ant to build - have a property like tomcat.home or weblogic.home. However it introduces another constraint that the developer must install Weblogic on his/her local machine if weblogic is being used ! Any other cleaner solution?

并在使用ant构建时遵循@kaustav datta的解决方案——拥有tomcat这样的属性。家里或weblogic.home。然而,它引入了另一个限制,即如果Weblogic被使用,开发人员必须在他/她的本地机器上安装Weblogic !其他清洁解决方案吗?

#12


0  

In my case, when I went to the Targetted Runtimes, screen, Tomcat 7 was not listed (disabled) despite being installed.

在我的例子中,当我到目标运行时,屏幕,Tomcat 7没有被列出(禁用),尽管已经安装了。

To fix, I had to go to Preferences->Server->Runtime Environments then uninstall and reinstall Tomcat 7.

为了解决这个问题,我必须到Preferences->服务器->运行时环境,然后卸载和重新安装Tomcat 7。

#13


0  

Many of us develop in Eclipse via a Maven project. If so, you can include Tomcat dependencies in Maven via the tomcat-servlet-api and tomcat-jsp-api jars. One exists for each version of Tomcat. Usually adding these with scope provided to your POM is sufficient. This will keep your build more portable.

我们中的许多人通过Maven项目开发Eclipse。如果是这样,您可以通过tomcat-servlet-api和tomcat-jsp-api jar将Tomcat的依赖项包括在Maven中。一个存在于每个版本的Tomcat。通常,在您的POM中添加这些内容是足够的。这将使您的构建更具可移植性。

If you upgrade Tomcat in the future, you simply update the version of these jars as well.

如果将来升级Tomcat,那么只需更新这些jar的版本。

#14


0  

For maven projects add following dependancy :

对于maven项目,添加以下依赖项:

<!-- https://mvnrepository.com/artifact/javax.servlet/servlet-api -->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.0.1</version>
    <scope>provided</scope>
</dependency>

Reference

参考

For gradle projects:

gradle项目:

dependencies {
providedCompile group: 'javax.servlet', name: 'javax.servlet-api', version: '3.0.1'
}

or download javax.servlet.jar and add to your project.

或javax.servlet下载。jar并添加到项目中。

#1


373  

Ensure you've the right Eclipse and Server

Ensure that you're using at least Eclipse IDE for Java EE developers (with the EE). It contains development tools to create dynamic web projects and easily integrate servletcontainers (those tools are part of Web Tools Platform, WTP). In case you already had Eclipse IDE for Java (without EE), and manually installed some EE related plugins, then chances are that it wasn't done properly. You'd best trash it and grab the real Eclipse IDE for Java EE one.

确保您正在使用至少Eclipse IDE为Java EE开发人员(使用EE)。它包含开发工具来创建动态的web项目,并轻松地集成servletcontainer(这些工具是web工具平台WTP的一部分)。如果您已经为Java(没有EE)提供了Eclipse IDE,并且手动安装了一些与EE相关的插件,那么很有可能它做得不够好。您最好将它丢弃,并为Java EE one获取真正的Eclipse IDE。

You also need to ensure that you already have a servletcontainer installed on your machine which implements at least the same Servlet API version as the servletcontainer in the production environment, for example Apache Tomcat, Oracle GlassFish, JBoss AS/WildFly, etc. Usually, just downloading the ZIP file and extracting it is sufficient. In case of Tomcat, do not download the EXE format, that's only for Windows based production environments. See also a.o. Several ports (8005, 8080, 8009) required by Tomcat Server at localhost are already in use.

您还需要确保你在你的机器上已经安装了一个servletcontainer实现至少相同的Servlet API版本servletcontainer在生产环境中,例如Apache Tomcat,Oracle GlassFish,JBoss / WildFly,等等。通常,只是下载ZIP文件和提取是充分的。在Tomcat的情况下,不要下载EXE格式,这只适用于基于Windows的生产环境。在本地主机上,Tomcat服务器需要的几个端口(8005,8080,8009)已经在使用。

A servletcontainer is a concrete implementation of the Servlet API. Note that the Java EE SDK download at Oracle.com basically contains GlassFish. So if you happen to already have downloaded Java EE SDK, then you basically already have GlassFish. Also note that for example GlassFish and JBoss AS/WildFly are more than just a servletcontainer, they also supports JSF, EJB, JPA and all other Java EE fanciness. See also a.o. What exactly is Java EE?

servletcontainer是Servlet API的具体实现。请注意,Oracle.com上的Java EE SDK基本包含了GlassFish。如果你已经下载了Java EE SDK,那么你基本上已经有了GlassFish。还要注意,例如GlassFish和JBoss作为/WildFly不仅仅是一个servletcontainer,它们还支持JSF、EJB、JPA和其他所有Java EE fanciness。Java EE到底是什么?


Integrate Server in Eclipse and associate it with Project

Once having installed both Eclipse for Java EE and a servletcontainer on your machine, do the following steps in Eclipse:

一旦在您的机器上安装了Java EE和servletcontainer的Eclipse,请在Eclipse中执行以下步骤:

  1. Integrate servletcontainer in Eclipse

    在Eclipse中集成servletcontainer

    a. Via Servers view

    通过服务器视图

    • Open the Servers view in the bottom box.
    • 打开底部框中的Servers视图。
    • Rightclick there and choose New > Server.
    • 右击并选择新的>服务器。
    • Pick the appropriate servletcontainer make and version and walk through the wizard.

      选择合适的servletcontainer制作和版本,并通过向导。

      如何导入javax。我的Eclipse项目中的servlet API ?

    b. Or, via Eclipse preferences

    或者,通过Eclipse首选项。

    • Open Window > Preferences > Server > Runtime Environments.
    • 打开窗口>偏好>服务器>运行时环境。
    • You can Add, Edit and Remove servers here.

      您可以在这里添加、编辑和删除服务器。

      如何导入javax。我的Eclipse项目中的servlet API ?

  2. Associate server with project

    把服务器和项目

    a. In new project

    新项目。

    • Open the Project Navigator/Explorer on the left hand side.
    • 在左侧打开项目导航器/资源管理器。
    • Rightclick there and choose New > Project and then in menu Web > Dynamic Web Project.
    • 右击并选择新的>项目,然后在菜单Web >动态Web项目中。
    • In the wizard, set the Target Runtime to the integrated server.

      在向导中,将目标运行时设置为集成服务器。

      如何导入javax。我的Eclipse项目中的servlet API ?

    b. Or, in existing project

    b.或在现有项目中。

    • Rightclick project and choose Properties.
    • 右击项目并选择属性。
    • In Targeted Runtimes section, select the integrated server.

      在目标运行时部分,选择集成服务器。

      如何导入javax。我的Eclipse项目中的servlet API ?

    Either way, Eclipse will then automatically take the servletcontainer's libraries in the build path. This way you'll be able to import and use the Servlet API.

    无论哪种方式,Eclipse都会自动在构建路径中获取servletcontainer的库。这样您就可以导入和使用Servlet API了。


Never carry around loose server-specific JAR files

You should in any case not have the need to fiddle around in the Build Path property of the project. You should above all never manually copy/download/move/include the individual servletcontainer-specific libraries like servlet-api.jar, jsp-api.jar, el-api.jar, j2ee.jar, javaee.jar, etc. It would only lead to future portability, compatibility, classpath and maintainability troubles, because your webapp would not work when it's deployed to a servletcontainer of a different make/version than where those libraries are originally obtained from.

您应该在任何情况下都不需要在项目的构建路径属性中进行修改。首先,您不应该手动复制/下载/移动/包含像servlet-api这样的单独的servlet特定的库。jsp-api jar。el-api jar。jar,j2ee。javaee jar。它只会导致未来的可移植性、兼容性、类路径和可维护性问题,因为当它被部署到一个不同的make/版本的servletcontainer时,你的webapp就不会起作用了,而这些库最初是从这些库中获取的。

In case you're using Maven, you need to make absolutely sure that servletcontainer-specific libraries which are already provided by the target runtime are marked as <scope>provided</scope>.

如果您使用的是Maven,那么您需要确保目标运行时已经提供的servletcontainer特定库被标记为 提供的

Here are some typical exceptions which you can get when you litter the /WEB-INF/lib or even /JRE/lib, /JRE/lib/ext, etc with servletcontainer-specific libraries in a careless attempt to fix the compilation errors:

下面是一些典型的例外情况,当你在一个粗心的尝试去修复编译错误时,你可以把/WEB-INF/lib,甚至/JRE/lib/ JRE/lib/ext,等等。

#2


209  

如何导入javax。我的Eclipse项目中的servlet API ?

STEP 1

Go to properties of your project ( with Alt+Enter or righ-click )

转到项目属性(Alt+Enter或右击)

STEP 2

check on Apache Tomcat v7.0 under Targeted Runtime and it works.

在目标运行时检查Apache Tomcat v7.0,它可以工作。

#3


70  

Little bit difference from Hari:

与Hari有一点不同:

Right click on project ---> Properties ---> Java Build Path ---> Add Library... ---> Server Runtime ---> Apache Tomcat ----> Finish.

右键单击项目——>属性——> Java构建路径——>添加库…--->服务器运行时---> Apache Tomcat --->结束。

#4


10  

Quick Fix- This worked in Eclipse - Right Click on project -> Properties -> Java Build Path (Tab) -> Add External JARs -> locate the servlet api jar implementation (if Tomcat - its named servlet-api.jar) -> click OK. That's it !!

快速修复-这在Eclipse -右键单击项目->属性-> Java Build Path (Tab) ->添加外部jar ->定位servlet api jar实现(如果Tomcat -它的命名为servlet-api - jar) ->点击OK。就是这样! !

#5


10  

Include servlet-api.jar from your server lib folder.如何导入javax。我的Eclipse项目中的servlet API ?

包括servlet api。从服务器lib文件夹中获取jar。

Do this step

做这一步

如何导入javax。我的Eclipse项目中的servlet API ?

#6


4  

I know this is an old post. However, I observed another instance where in the project already has Tomcat added but we still get this error. Did this to resolve that:
Alt + Enter
Project Facets
On the right, next to details, is another tab "Runtimes". The installed tomcat server will be listed there. Select it.
Save the configuration and DONE!

我知道这是一个旧帖子。但是,我观察到另一个实例,在项目中已经添加了Tomcat,但是我们仍然会得到这个错误。这样做是为了解决这个问题:Alt +在右边的项目方面,在细节旁边,是另一个标签“运行时间”。安装的tomcat服务器将在那里列出。选择它。保存配置并完成!

Hope this helps someone.

希望这可以帮助别人。

#7


3  

you can simply copy the servlet-api.jar and copy that jar files into lib folder, which is in WEB-INF. then just clean and built your project, your errors will be solved.

您可以简单地复制servlet-api。将jar文件复制到lib文件夹中,该文件夹位于WEB-INF中。然后清理并建立你的项目,你的错误将被解决。

  **OR**

you can directly add jar files to library by using following steps.

您可以通过以下步骤直接将jar文件添加到库中。

  1. Right click on project.
  2. 右键单击项目。
  3. Go To Properties.
  4. 去的属性。
  5. Go to Java Build Path.
  6. 转到Java构建路径。
  7. Select Add Library option from tabs.
  8. 从选项卡中选择Add Library选项。
  9. Add Jar Files
  10. 添加Jar文件
  11. give path of your servlet-api.jar file.
  12. 提供您的servlet-api的路径。jar文件。
  13. Clean and build your project.
  14. 清洁并建立你的项目。

#8


3  

Add javax.servlet dependency in pom.xml. Your problem will be resolved.

添加javax。在pom . xml中servlet的依赖。你的问题将得到解决。

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.0.1</version>
    <scope>provided</scope>
</dependency>

#9


2  

From wikipedia.

从*。

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 HelloWorld extends HttpServlet {
  public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    PrintWriter out = response.getWriter();
    out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
                "Transitional//EN\">\n" +
                "<html>\n" +
                "<head><title>Hello WWW</title></head>\n" +
                "<body>\n" +
                "<h1>Hello WWW</h1>\n" +
                "</body></html>");
  }
}

This, of course, works only if you have added the servlet-api.jar to Eclipse build path. Typically your application server (e.g Tomcat) will have the right jar file.

当然,这只有在您添加了servlet-api之后才有效。jar到Eclipse构建路径。通常是应用服务器(e)。将有正确的jar文件。

#10


1  

I was getting a null pointer exception during project creation related to "Dynamic Web Module".

在与“动态Web模块”相关的项目创建过程中,我得到了一个空指针异常。

To get the project to compile (that is, to javax.servlet to import successfully) I had to go to project's Properties, pick Project Facets in the sidebar, tick Dynamic Web Module and click Apply.

让项目编译(即,javax)。要成功导入servlet)我必须访问项目的属性,在侧边栏中选择项目方面,在动态Web模块中进行标记,然后单击Apply。

Surprisingly, this time "Dynamic Web Module" facet installed correctly, and import started to work.

令人惊讶的是,这一次“动态Web模块”安装正确,导入开始工作。

#11


0  

You should above all never manually copy/download/move/include the individual servletcontainer-specific libraries like servlet-api.jar

首先,不要手动复制/下载/移动/包含像servlet-api.jar这样的单独的servlet特定库。

@BalusC,

@BalusC,

I would prefer to use the exact classes that my application is going to use rather than one provided by Eclipse (when I am feeling like a paranoid developer).

我更愿意使用我的应用程序将要使用的确切类,而不是Eclipse提供的类(当我感觉自己像一个偏执的开发人员时)。

Another solution would be to use Eclipse "Configure Build Path" > Libraries > Add External Jars, and add servlet api of whatever Container one chooses to use.

另一个解决方案是使用Eclipse“配置构建路径”>库>添加外部jar,并添加任意选择使用的容器的servlet api。

And follow @kaustav datta's solution when using ant to build - have a property like tomcat.home or weblogic.home. However it introduces another constraint that the developer must install Weblogic on his/her local machine if weblogic is being used ! Any other cleaner solution?

并在使用ant构建时遵循@kaustav datta的解决方案——拥有tomcat这样的属性。家里或weblogic.home。然而,它引入了另一个限制,即如果Weblogic被使用,开发人员必须在他/她的本地机器上安装Weblogic !其他清洁解决方案吗?

#12


0  

In my case, when I went to the Targetted Runtimes, screen, Tomcat 7 was not listed (disabled) despite being installed.

在我的例子中,当我到目标运行时,屏幕,Tomcat 7没有被列出(禁用),尽管已经安装了。

To fix, I had to go to Preferences->Server->Runtime Environments then uninstall and reinstall Tomcat 7.

为了解决这个问题,我必须到Preferences->服务器->运行时环境,然后卸载和重新安装Tomcat 7。

#13


0  

Many of us develop in Eclipse via a Maven project. If so, you can include Tomcat dependencies in Maven via the tomcat-servlet-api and tomcat-jsp-api jars. One exists for each version of Tomcat. Usually adding these with scope provided to your POM is sufficient. This will keep your build more portable.

我们中的许多人通过Maven项目开发Eclipse。如果是这样,您可以通过tomcat-servlet-api和tomcat-jsp-api jar将Tomcat的依赖项包括在Maven中。一个存在于每个版本的Tomcat。通常,在您的POM中添加这些内容是足够的。这将使您的构建更具可移植性。

If you upgrade Tomcat in the future, you simply update the version of these jars as well.

如果将来升级Tomcat,那么只需更新这些jar的版本。

#14


0  

For maven projects add following dependancy :

对于maven项目,添加以下依赖项:

<!-- https://mvnrepository.com/artifact/javax.servlet/servlet-api -->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.0.1</version>
    <scope>provided</scope>
</dependency>

Reference

参考

For gradle projects:

gradle项目:

dependencies {
providedCompile group: 'javax.servlet', name: 'javax.servlet-api', version: '3.0.1'
}

or download javax.servlet.jar and add to your project.

或javax.servlet下载。jar并添加到项目中。