servlet 中的 Http 错误 404:找不到该网址的网页

问题描述 投票:0回答:2

PrintNamesServlet.java

此 Servlet 打印输入的用户名称。

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "PrintNamesServlet")
public class PrintNamesServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("Ram Dhakal");
    }
}

CounterServlet.java
: 计算页面中的点击或访问次数

import javax.servlet.ServletException; import
javax.servlet.annotation.WebServlet; import
javax.servlet.http.HttpServlet; import
javax.servlet.http.HttpServletRequest; import
javax.servlet.http.HttpServletResponse; import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "CounterServlet") public class CounterServlet
extends HttpServlet {
    int totalHits;
    public void init() throws ServletException{
        totalHits = 0;
         }
         protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            PrintWriter out = response.getWriter();
        out.print("Total visit count: " + totalHits++);
    }  public void destroy(){
           }
           }

web.xml
:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <servlet>
        <servlet-name>PrintNamesServlet</servlet-name>
        <servlet-class>PrintNamesServlet</servlet-class>
    </servlet>

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

    <servlet>
        <servlet-name>CounterServlet</servlet-name>
        <servlet-class>CounterServlet</servlet-class>
    </servlet>

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

我收到错误:

No webpage was found for the web address: http://localhost:8080/

当我第一次尝试运行 servlet 时,我没有发现我的代码有什么问题。我在网址中输入了

http://localhost:8080/PrintNamesServlet

java servlets servlet-3.0
2个回答
0
投票

您已使用Servlet 3.0规范 让我们来看看吧

在Servlet 3.0中,可以使用@WebServlet指定servlet元数据

@WebServlet(name="mytest", 
 urlPatterns={"/myurl"}) 
public class TestServlet extends javax.servlet.http.HttpServlet { 
 .... 
}

这样就可以使用注释中指定的 url 模式来访问 servlet。

   @@WebServlet(name="mytest",urlPatterns={"/myurl"})

根据该 servlet 访问使用

  http://localhost:8080/myurl

在您只指定名称的情况下,您还必须指定 urlPatterns,这样您就可以正确调用 servlet。

  @WebServlet(name = "CounterServlet",urlPatterns={"/CounterServlet"}) public class CounterServlet extends HttpServlet {}

并且您不需要使用 web.xml 文件。


0
投票

当您使用这样的注释时

@WebServlet(name = "PrintNamesServlet")
web.xml
映射不会被使用。

您必须删除这些注释或向它们添加

urlMapping
属性。

© www.soinside.com 2019 - 2024. All rights reserved.