简体   繁体   English

使用Eclipse Java EE运行Servlet

[英]Running a Servlet with Eclipse Java EE

I'm trying to run a servlet using Apache Tomcat v7.0 (i also tried 8) using Dynamic web module version 3.0. 我正在尝试使用动态Web模块版本3.0使用Apache Tomcat v7.0(我也尝试过8)运行servlet。

码

I've also tried with the xml file (even though it should work without it because its 3.0) and it doesn't work aswell... What the hell am I doing wrong? 我还尝试了xml文件(即使没有它也可以工作,因为它的3.0版本),它也不能正常工作...我到底在做什么错呢?

The error is The requested resource is not available. 错误是请求的资源不可用。

EDIT: Ok, so I made a XML file again so you can maybe tell me what i'm doing wrong in it... 编辑:好的,所以我又做了一个XML文件,所以你也许可以告诉我我在做错什么...

<?xml version="1.0" encoding="UTF-8"?>
<web-app>
 <servlet>
    <servlet-name>HelloServlet</servlet-name>
    <servlet-class>code.HelloServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>HelloServlet</servlet-name>
    <url-pattern>/hello</url-pattern>
  </servlet-mapping>
</web-app>

code is the name of the package inside src. code是src中软件包的名称。

I'm using the url: http://localhost:8080/hello , still not working 我正在使用网址: http:// localhost:8080 / hello ,仍然无法正常工作

The package is wrong, you defined in the web.xml: 您在web.xml中定义的包是错误的:

  <servlet-class>code.HelloServlet</servlet-class>

and in the class 在课堂上

package net.codejava;

So this should fix the problem: 因此,这应该可以解决问题:

<servlet-class>net.codejava.HelloServlet</servlet-class

It's quite unclear from the question, either you want to create Servlet using @WebServlet or through web.xml . 从这个问题还不清楚,您是想使用@WebServlet还是通过web.xml创建Servlet

To create Servlet using @WebServlet you do not need web.xml configuration at all. 要使用@WebServlet创建Servlet您根本不需要配置web.xml

Just create a class annotated with @WebServlet and call the url as below: 只需创建一个用@WebServlet注释的类并按如下所示调用url:

import java.io.IOException;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/HelloServlet")
public class MyServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        response.getWriter().println("Hello");
    }
}

Then call url: http://host:port/context-root[/url-pattern] , for me it's http://localhost:8989/TestApplication/HelloServlet 然后调用url: http://host:port/context-root[/url-pattern] ,对我来说是http://localhost:8989/TestApplication/HelloServlet

Or if you want to create through web.xml then load the servlet on startup as below: 或者,如果您要通过web.xml创建,则在启动时加载servlet,如下所示:

<servlet>
        <display-name>Hello Servlet</display-name>
        <servlet-name>HelloServlet</servlet-name>
        <servlet-class>com.package.name.HelloServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>HelloServlet</servlet-name>
        <url-pattern>/HelloServlet/*</url-pattern>
    </servlet-mapping>

Again you can call like : http://host:port/context-root[/url-pattern] , for me it's http://localhost:8989/TestApplication/HelloServlet 同样,您可以这样调用: http://host:port/context-root[/url-pattern] ,对我来说,它是http://localhost:8989/TestApplication/HelloServlet

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM