java - 在服务器上运行时,我不断收到错误404

标签 java eclipse jsp tomcat servlets

package com.davido.asuzor;

import java.io.IOException;
import java.io.PrintWriter;

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

@WebServlet("/SimpleServlet")
public class SimpleServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        System.out.println("Hello from Servlet GET method");
        response.setContentType("text/html");
        PrintWriter pw=response.getWriter();
        pw.println("<h3>Hello in html</h3>");
    }
}


我的tomcat成功连接,但是当我在服务器上运行代码时,出现错误404异常。

最佳答案

服务器运行时,可以通过多种方式调用它。我列出了其中一些:

一种。

假设上下文路径为http://localhost:8080/TestDynamicProject,则应在浏览器中输入URL http://localhost:8080/TestDynamicProject/SimpleServlet,然后按Enter。

B.

使用类似的Java程序

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

class Main {
    public static void main(String[] args) {
        System.out.println(getHTML("http://localhost:8080/TestDynamicProject/SimpleServlet"));
    }

    public static String getHTML(String urlToRead) {
        try {
            StringBuilder result = new StringBuilder();
            InputStream stream = null;
            URL url = new URL(urlToRead);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.connect();
            if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
                stream = conn.getInputStream();
            }

            BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String line;
            while ((line = rd.readLine()) != null) {
                result.append(line);
            }

            rd.close();
            return result.toString();
        } catch (Exception e) {
            return e.getMessage().toString();
        }
    }
}


输出:

<h3>Hello in html</h3>


C。

从HTML或JSP页面访问它:例如,我在testservlet.html文件夹下创建了WebContent并以http://localhost:8080/TestDynamicProject/testservlet.html的身份访问它,然后单击Submit按钮。文件内容如下:

<html>
<head>
<title>Insert title here</title>
</head>
<body>
    <form action="SimpleServlet">
        <input type="Submit"/>
    </form>
</body>
</html>

关于java - 在服务器上运行时,我不断收到错误404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59338235/

相关文章:

java - 如何使用 java 将数据从 CSV 文件加载到表中?

java - 如何使用 Mongo-Java API 3 在 MongoCursor 上设置取消超时

java - JAVA中比较XML,输出: changelog

java - 如何获取从 jsp 调用的 java 包中的工作目录?

java - 新手Spring webflow end-state查看报错

java - Spring Boot 使用 ConfigurationProperties 从 .properties 和 .yaml 注入(inject)映射值

java - 如何在自定义路径参数类中获取http headers?

java - 为什么 Eclipse 不导出图像?

eclipse - 使用 Eclipse 调试 Node.js 应用程序

java - JSP 下拉列表的选定值