错误 404:请求的资源在使用 HelloWorld servlet 时不可用 [英] Error 404: The requested resource is not available using HelloWorld servlet

查看:65
本文介绍了错误 404:请求的资源在使用 HelloWorld servlet 时不可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个 Java Servlet,我正在努力让一个简单的 HelloWorld 示例正常工作.

I am writing a Java Servlet, and I am struggling to get a simple HelloWorld example to work properly.

HelloWorld.java 类是:

package crunch;

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

public class HelloWorld extends HttpServlet {
  public void doGet(HttpServletRequest request,
                    HttpServletResponse response)
      throws ServletException, IOException {
    PrintWriter out = response.getWriter();
    out.println("Hello World");
  }
}

我正在运行 Tomcat v7.0,并且已经阅读了类似的问题,其中的回答指的是更改 web.xml 中的 invoker servlet-mapping 部分.这个部分在我的里面其实是没有的,我添加的时候还是出现同样的问题.

I am running Tomcat v7.0, and have already read similar questions, with responses referring to changing the invoker servlet-mapping section in web.xml. This section actually doesn't exist in mine, and when I added it the same problem still occurred.

推荐答案

试试这个(如果是 Java EE V6)

Try this (if the Java EE V6)

package crunch;

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

@WebServlet(name="hello",urlPatterns={"/hello"}) // added this line

public class HelloWorld extends HttpServlet {
  public void doGet(HttpServletRequest request,
                    HttpServletResponse response)
      throws ServletException, IOException {
    PrintWriter out = response.getWriter();
    out.println("Hello World");
  }
}

现在通过 http://127.0.0.1:8080/yourapp/hello

其中 8080 是默认的 Tomcat 端口,yourapp 是应用程序的上下文名称

where 8080 is default Tomcat port, and yourapp is the context name of your applciation

这篇关于错误 404:请求的资源在使用 HelloWorld servlet 时不可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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