在 Apache Tomcat 中运行 Servlet

发布于 2024-11-07 19:45:38 字数 2427 浏览 0 评论 0原文

我正在学习 Java Ranch Cattle Drive 在线教程并开始着手 Servlet 项目。我想安装并运行 Apache 而不是 Orion,因为我想学习更主流的 HTTP 服务器。

在我的机器上启动并运行了 Apache(这是 Windows XP/Cygwin 环境,因此我使用最新版本 cygwin 附带的 Apache 软件包,当前 httpd 版本 1.3.33)

我 将浏览器定向到 http://localhost 并且服务器正确地在 C:\cygwin\ 获取欢迎页面 (index.html) var\www\htdocs。

我已经安装了 Java EE 并且能够编译以下 Servlet:

import java.io.* ;
import javax.servlet.http.* ;

public class BeeServlet extends HttpServlet
{

    public void doGet( HttpServletRequest request , HttpServletResponse response )
    {
        response.setContentType("text/html");
        try
        {
            PrintWriter out = response.getWriter();
            out.println( "a-buzz-buzz ..." );
            out.close();
        }
        catch( Exception e )
        {
            System.out.println( "cannot get writer: " + e );
        }
    }

}

这会编译成 .class 文件,没有错误。我的问题是,我在服务器中的哪里安装这个文件?

该文件名为 BeeServlet.class,教程要求将浏览器定向到 http://localhost/servlet/BeeServlet在 Web 服务器的相应目录中安装 BeeServlet.class 后。

(编辑:我已经成功安装了 Tomcat 并显示了基本的欢迎页面,如下面的步骤所述。我仍然不确定将 .class 文件放在哪里或如何访问它):

以下是步骤安装 Tomcat 并通过 Cygwin 运行它:

  1. 转到 http://tomcat.apache.org/ 并下载最新版本的 Tomcat(对于上述系统配置,选择 32 位/64 位 Windows Service Installer 方法,这将在 C:\Program Files\Apache Software Foundation\Tomcat 7.0 中创建 9 MB 的安装)。< /p>

  2. 将此路径添加到 Windows 系统环境变量“Path”

  3. 启动 Cygwin bash shell

  4. 输入“tomcat7”(设置路径后,它将在上面的路径中找到这个.exe)。这将启动 tomcat 服务器。

  5. 启动浏览器并将其定向到http://localhost:8080。这将显示 Tomcat 欢迎屏幕(这实际上是 Tomcat 正在读取文件:C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\ROOT\index.jsp)。

  6. 在 C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps 下创建名为 Examples\WEB-INF\classes 的新目录。

  7. 将 @WebServlet 注释添加到源代码文件(这将位于任何导入之后):@WebServlet(urlPatterns={"/servlet/BeeServlet"})。编译 BeeServlet.java 文件并将 .class 文件放置在 C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\examples\WEB-INF\classes\BeeServlet.class

  8. 将浏览器定向到 http://localhost:8080/examples/servlet/BeeServlet

I'm working through the Java Ranch Cattle Drive online tutorials and got up to the Servlets projects. I wanted to install and run Apache instead of Orion, because I wanted to learn a more mainstream HTTP server.

I got Apache up and running on my machine (this is a Windows XP/Cygwin environment, so I'm using the Apache package that comes with the latest version of cygwin, currently httpd version 1.3.33)

I'm to the point of directing a browser to http://localhost and the server is correctly fetching the welcome page (index.html) at C:\cygwin\var\www\htdocs.

I've installed Java EE and was able to compile the following Servlet:

import java.io.* ;
import javax.servlet.http.* ;

public class BeeServlet extends HttpServlet
{

    public void doGet( HttpServletRequest request , HttpServletResponse response )
    {
        response.setContentType("text/html");
        try
        {
            PrintWriter out = response.getWriter();
            out.println( "a-buzz-buzz ..." );
            out.close();
        }
        catch( Exception e )
        {
            System.out.println( "cannot get writer: " + e );
        }
    }

}

This compiles into a .class file without errors. My question is, where do I install this file in the server?

The file is called BeeServlet.class and the tutorial says to direct a browser to http://localhost/servlet/BeeServlet after installing the BeeServlet.class in the appropriate directory in the web server.

(EDIT: I've successfully installed Tomcat and have the basic welcome page showing, as explained in the steps below. I'm still not sure where to put the .class file though or how to access it):

Here's are the steps of installing Tomcat and running it through Cygwin:

  1. Go to http://tomcat.apache.org/ and download the latest version of Tomcat (for the above system configuration, select the 32-bit/64-bit Windows Service Installer method, which will create a 9 MB installation at C:\Program Files\Apache Software Foundation\Tomcat 7.0).

  2. Add this path to the Windows system environment variable 'Path'

  3. Start a Cygwin bash shell

  4. type 'tomcat7' (with Path set, it will find this .exe in the above path). This will start the tomcat server.

  5. Start a browser and direct it to http://localhost:8080. This will bring up the Tomcat welcome screen (which is really Tomcat reading the file: C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\ROOT\index.jsp).

  6. Create new directories under C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps named examples\WEB-INF\classes.

  7. Add a @WebServlet annotation to the source code file (this would be located after any imports): @WebServlet(urlPatterns={"/servlet/BeeServlet"}). Compile the BeeServlet.java file and place the .class file in C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\examples\WEB-INF\classes\BeeServlet.class

  8. Direct your browser to http://localhost:8080/examples/servlet/BeeServlet

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

海拔太高太耀眼 2024-11-14 19:45:38

您需要让 servlet 容器知道您有一个必须执行的 servlet。由于您已经使用 Tomcat 7.0,因此 @WebServlet 注释应该足够了。

@WebServlet(urlPatterns={"/servlet/BeeServlet"})
public class BeeServlet extends HttpServlet {
    // ...
}

或者使用旧方法(可能正如 JavaRanch 教程应该提到的那样),通过 web.xml 中的声明。

<servlet>
    <servlet-name>beeServlet</servlet-name>
    <servlet-class>BeeServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>beeServlet</servlet-name>
    <url-pattern>/servlet/BeeServlet</url-pattern>
</servlet-mapping>

请注意,将类放入默认包中是一种不好的做法。如果您希望包中的类可以看到它们,则应该将它们放置在包中。用纯 Java 编写的 servlet 容器也需要能够看到它们。现在,Tomcat 对此有一些技巧,但这仅适用于特定版本/配置。总是,总是将 servlet 类放入包中。

另请参阅:


与具体问题无关

我已经安装了 Java EE

请注意,从 Oracle.com 下载的 Java EE 基本上包含 Glassfish 应用服务器以及一些文档。当您只想在 Tomcat 上运行 servlet 时,您不需要它。

另请参阅:

You need to let the servletcontainer know that you've a servlet which it has to execute. Since you're already on Tomcat 7.0, a @WebServlet annotation should suffice.

@WebServlet(urlPatterns={"/servlet/BeeServlet"})
public class BeeServlet extends HttpServlet {
    // ...
}

Or the old way (probably as the JavaRanch tutorial should have mentioned), by a declaration in web.xml.

<servlet>
    <servlet-name>beeServlet</servlet-name>
    <servlet-class>BeeServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>beeServlet</servlet-name>
    <url-pattern>/servlet/BeeServlet</url-pattern>
</servlet-mapping>

Please note that putting classes in a default package is a bad practice. You should be placing classes in a package if you want them to be visible to classes inside a package. The servletcontainer, written in pure Java, needs to be able to see them as well. Now, Tomcat has hacks for this, but this works in specific versions/configurations only. Always, always put servlet classes in a package.

See also:


Unrelated to the concrete problem:

I've installed Java EE

Please note that the Java EE download from Oracle.com contains basically the Glassfish application server along with some documentation. You don't need it when all you want is just running servlets on Tomcat.

See also:

悲欢浪云 2024-11-14 19:45:38

Apache 是一个 Web 服务器,而不是 servlet/JSP 引擎。 Tomcat是一个servlet/JSP引擎;杰蒂也是如此。您需要在 Tomcat 上部署您的 servlet/JSP,并告诉 Apache 将您的 servlet/JSP 的请求转发到 Tomcat。

Apache is a web server, not a servlet/JSP engine. Tomcat is a servlet/JSP engine; so is Jetty. You'll need to deploy your servlets/JSPs on Tomcat and tell Apache to forward requests to your servlets/JSPs to Tomcat.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文