如何在我的Eclipse项目中导入javax.servlet API? [英] How do I import the javax.servlet API in my Eclipse project?

查看:1002
本文介绍了如何在我的Eclipse项目中导入javax.servlet API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Eclipse中的Servlets进行开发,但是它表示包 javax.servlet 无法解析。如何在我的Eclipse项目中添加 javax.servlet package

解决方案

确保你有正确的Eclipse和服务器



确保你至少使用




b。 或通过Eclipse首选项




  • 打开窗口>首选项>服务器>运行时环境

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



  • 将服务器与项目关联



    a。 新项目




    • 打开左侧的 Project Navigator / Explorer

    • 右键单击,然后选择新建>项目,然后在菜单 Web>动态Web项目中。

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






    b。 或现有项目




    • 右键单击项目并选择属性
    • 。 >
    • 在目标运行时间部分中,选择集成服务器。






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









  • 在任何情况下,您都不需要在构建路径项目财产。您应该首先从不手动复制/下载/移动/包含各个servlet特定库,如 servlet-api.jar jsp-api.jar el-api.jar j2ee.jar javaee.jar 等。它只会导致未来的可移植性,兼容性,类路径和可维护性的麻烦,因为当您的webapp部署到不同的servlet容器时,您的webapp将无法正常工作make / version比这些库最初从哪里获得。



    如果你使用Maven,你需要确保已经提供的servletcontainer特定的库由目标运行时标记为< scope>提供< / scope>



    这是一些典型的例外当您丢弃 / WEB-INF / lib 甚至 / JRE / lib / JRE / lib / ext 等,其中servletcontainer特定的库在粗心的att无法修复编译错误:




    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?

    解决方案

    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.

    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.

    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?


    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:

    1. Integrate servletcontainer in Eclipse

      a. Via Servers view

      • Open the Servers view in the bottom box.
      • Rightclick there and choose New > Server.
      • Pick the appropriate servletcontainer make and version and walk through the wizard.

      b. Or, via Eclipse preferences

      • Open Window > Preferences > Server > Runtime Environments.
      • You can Add, Edit and Remove servers here.

    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.
      • In the wizard, set the Target Runtime to the integrated server.

      b. Or, in existing project

      • Rightclick project and choose Properties.
      • In Targeted Runtimes section, select the integrated server.

      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.


    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.

    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>.

    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:

    这篇关于如何在我的Eclipse项目中导入javax.servlet API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆