在Apache Tomcat上运行的Servlet [英] Running Servlets in Apache Tomcat

查看:190
本文介绍了在Apache Tomcat上运行的Servlet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过Java牛牧场工作驱动器在线教程和起身到Servlet的项目。我想安装和运行Apache,而不是猎户座,因为我想学习更加主流的HTTP服务器。

我的Apache和我的机器上运行(这是Windows XP / Cygwin环境,所以我用自带的最新版本的cygwin,目前的httpd版本1.3.33 Apache的包)

我来指挥浏览器 HTTP点://本地主机和服务器正确获取欢迎页面(的index.html)在C:\\ cygwin的\\无功\\ WWW \\ htdocs目录

我已经安装了Java EE和能够编译下面的Servlet:

 进口java.io. *;
进口javax.servlet.http包*。公共类BeeServlet延伸的HttpServlet
{    公共无效的doGet(HttpServletRequest的请求,HttpServletResponse的响应)
    {
        response.setContentType(text / html的);
        尝试
        {
            PrintWriter的OUT = response.getWriter();
            通过out.println(A-时髦的嗡嗡声......);
            out.close();
        }
        赶上(例外五)
        {
            的System.out.println(不能让作家:+ E);
        }
    }}

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

该文件称为BeeServlet.class和教程说直接在浏览器中的http://本地主机/的servlet / BeeServlet 在Web服务器中的相应目录中安装BeeServlet.class后。

(编辑:我已经成功地安装Tomcat和具备基本的欢迎页面显示,如下面的步骤解释我仍然不知道放在哪里.class文件,虽然还是如何访问它):

下面的是安装Tomcat和通过Cygwin的运行它的步骤:


  1. 转到 http://tomcat.apache.org/ 并下载最新版本的Tomcat的(针对上述系统配置,选择32位/ 64位的Windows服务安装的方法,这将创建位于C 9 MB的安装:\\ Program Files文件\\ Apache软件基金会\\ Tomcat的7.0)。


  2. 此路径添加到Windows系统环境变量路径


  3. 启动Cygwin的bash shell的


  4. 键入'tomcat7'(与路径集,会发现这个.exe文件在上面的路径)。这将启动Tomcat服务器。


  5. 启动浏览器并直接到的http://本地主机:8080 。这将打开Tomcat的欢迎屏幕。(这是真的Tomcat的读取文件:C:\\ Program Files文件\\ Apache软件基金会\\ Tomcat的7.0 \\的webapps \\ ROOT \\的index.jsp)


  6. 创建在C新的目录:\\ Program Files文件\\ Apache软件基金会\\ Tomcat的7.0 \\ web应用实例名为\\ WEB-INF \\类。


  7. 添加@WebServlet标注到源$ C ​​$ C文件(这将设任何进口后):@WebServlet(URL模式= {/ servlet的/ BeeServlet})。编译BeeServlet.java文件并将其放置在C .class文件:\\ Program Files文件\\ Apache软件基金会\\ Tomcat的7.0 \\的webapps \\例子\\ WEB-INF \\类\\ BeeServlet.class


  8. 直接在浏览器中的http://本地主机:8080 /例子/的servlet / BeeServlet



解决方案

您需要让servletcontainer知道你由它来执行一个servlet。既然你已经在Tomcat 7.0,一个 @WebServlet 注释就足够了。

  @WebServlet(URL模式= {/ servlet的/ BeeServlet})
公共类BeeServlet延伸的HttpServlet {
    // ...
}

还是在老办法(可能为JavaRanch的教程应该提到),由一个声明的web.xml

 <&servlet的GT;
    < servlet的名称>&beeServlet LT; / servlet的名称>
    <的servlet类> BeeServlet< / servlet的类>
< / servlet的>
< Servlet映射>
    < servlet的名称>&beeServlet LT; / servlet的名称>
    < URL模式> /的servlet / BeeServlet< / URL模式>
< / Servlet映射>

请注意,把类默认包是一个不好的做法。如果你希望他们是在包的内部类可见,你应该把类的包。该servletcontainer,用纯Java编写,需要能够看到它们。现在,Tomcat拥有这个黑客,但这部作品在特定版本/只配置。一直以来,的总是的把servlet类在一个包。

参见:


无关以具体的问题:


  

我已经安装了Java EE


请注意,从Oracle.com了Java EE下载基本上包含GlassFish应用服务器与一些文档一起。当你想要的只是Tomcat上运行的servlet,你不需要它。

参见:

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

解决方案

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:

这篇关于在Apache Tomcat上运行的Servlet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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