简体   繁体   English

Java servlet不起作用,URL映射问题

[英]Java servlet does not work, url mapping issue

If I made a stupid mistake and asking a stupid question then my apologize. 如果我犯了一个愚蠢的错误并提出了一个愚蠢的问题,那么我道歉。 I have a servlet called HelloWorld - it is a simple servlet that implements the Servlet interface that is part of "1stapp" project. 我有一个名为HelloWorld的servlet-它是一个简单的servlet,它实现了Servlet接口,该接口是“ 1stapp”项目的一部分。 Its code is the following: 其代码如下:

 import java.io.IOException;
 import java.io.PrintWriter;
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import javax.servlet.*;
 public class helloworld implements Servlet
 {
     private ServletConfig config;
     public void init(ServletConfig config) 
        throws ServletException {
         this.config=config;
     }
     public void destroy(){}
     public ServletConfig getServletConfig() {
         return config;
     }
     public String getServletInfo() {
         return "this is simple hello World Servlet";
     }
     public void service(ServletRequest request, ServletResponse response)
             throws ServletException, IOException {
         response.setContentType("text/html");
         PrintWriter out=response.getWriter();
         out.println("<html><head>");
         out.println("<title>Simple Servlet</title>");
         out.println("</head>");
         out.println("<body>");
         out.println("<h1>Hello, World</h1>");
         out.println("</body></html>");
         out.close();
      }
  }

The HelloWorld servlet was successfully compiled into classes directory within WEB-INF . HelloWorld Servlet已成功编译到WEB-INF classes目录中。 The deployment descriptor is the following: 部署描述符如下:

  <?xml version="1.0" encoding="ISO-8859-1"?>
  <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" 
          "http://java.sun.com/j2ee/dtds/web-app_2.2.dtd">
  <web-app>
        <servlet>
             <servlet-name>hello</servlet-name>
             <servlet-class>helloworld</servlet-class>
        </servlet>

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

Now, when I type localhost:8080/1stapp/hello firefox geves me problem loading page message. 现在,当我键入localhost:8080/1stapp/hello firefox使我在加载页面消息时遇到问题。 Can anybody say what i did wrong? 谁能说我做错了吗?

There seem to be least three issues 似乎至少有三个问题

Classes are searched in WEB-INF/classes rather than in WEB-INF WEB-INF/classes而不是在WEB-INF中搜索WEB-INF/classes

Use the HttpServlet instead of the generic Servlet 使用HttpServlet代替通用Servlet

Overwrite the doXXX methods of the HttpServlet and not the generic service method. 覆盖HttpServletdoXXX方法,而不是通用service方法。 The service method dispatches to the corresponding doXXX method service方法调度到相应的doXXX方法

This is not the way to map Servlet in web.xml 这不是在web.xml映射Servlet的方法

First of all you have to declare your Servlet in web.xml as below 首先,您必须在web.xml中声明您的Servlet,如下所示

<servlet>
        <servlet-name>hello</servlet-name>
        <servlet-class>HelloWorld</servlet-class>
</servlet>

Then map your URL pattern to the Servlet as 然后将您的URL模式映射为Servlet,如下所示:

<servlet-mapping>
        <servlet-name>hello</servlet-name>
        <url-pattern>/hello</url-pattern>
</servlet-mapping>

And you better extend HttpServlet rather than Servlet. 而且,您最好扩展HttpServlet而不是Servlet。 HttpServlet is the convenient class to work with HTTP protocol. HttpServlet是使用HTTP协议的便捷类。

Change your Servlet class as below. 如下更改您的Servlet类。

public class HelloWorld extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<html><head>");
        out.println("<title>Simple Servlet</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>Hello, World</h1>");
        out.println("</body></html>");
        out.close();
    }
}

In addition your Servlet class name should be HelloWorld instead of helloWorld. 另外,您的Servlet类名称应为HelloWorld而不是helloWorld。

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

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