【问题标题】:Httpservlet error 404, not sure what I am doing wrong I have tried all the solutionsHttpservlet错误404,不确定我做错了什么我已经尝试了所有解决方案
【发布时间】:2019-10-19 01:24:08
【问题描述】:

所以我正在尝试创建这个使用 java 的 Web 应用程序并尝试通过 httpservlet 将 html 连接到 java 作为一个试验,我只是试图获取一些示例数据传递给和从 html 到 java,反之亦然。我遇到的问题是当我运行 html 表单时,它工作正常,我尝试在添加我想要的值后按下搜索按钮,它进入 404 Not Found 页面。我试图确保编译的 servlet 类文件位于 /WEB-INF/classes 文件夹中的包结构中。我也有一个包。我正在使用 tomcat 构建和编译应用程序,但我得到“源服务器没有找到目标资源的当前表示或不愿意透露存在的表示。”当我尝试运行“http://localhost:8080/”时。我尝试在 html 中使用 @WebServlet、“action”并添加一个 jsp 文件。我已经包含了文件夹结构、网页和错误的图像。

我不知道我做错了什么,真的很想得到一些帮助##


##

Folder Structure

Error

Web page

Java

@WebServlet("/servlet")
public class page extends HttpServlet {

@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) 
throws IOException {

String accountid = request.getParameter("Accountid");
String marketid = request.getParameter("Marketid");

response.setContentType("text/html");
PrintWriter writer = response.getWriter();

writer.println("<html>");
writer.println("<head>");
writer.println("<title> Incidents search result </title>");
writer.println("<head>");
writer.println("<body>");
writer.println("<h1>" + accountid + marketid + "</h1>");
writer.println("</body>");
writer.println("</html>");
}

HTML

<form name="getInput" action="${com.example.page.java}/servlet" method="post">
    <p>Account id: <input type="text" name="Accountid"></p>
    <p>Market id: <input type="text" name="Marketid"></p>
    <input type="submit" value="Submit">
 </form>

web.xml

 <?xml version="1.0" encoding="UTF-8"?>

 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns="http://xmlns.jcp.org/xml/ns/javaee"
     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
 http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
     id="WebApp_ID" version="3.1"
 >

 <!-- Invoke 'Generate' action to add tags or functions -->

 <servlet>
    <servlet-name>Servlet</servlet-name>
    <servlet-class>com.example.page</servlet-class>
 </servlet>
 <servlet-mapping>
    <servlet-name>Servlet</servlet-name>
    <url-pattern>/servlet</url-pattern>
 </servlet-mapping>
 <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>getInputs.html</welcome-file>
 </welcome-file-list>


 </web-app>

jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Same as the html

编辑:我尝试过使用Servlet returns “HTTP Status 404 The requested resource (/servlet) is not available”,但它似乎不起作用,正如我在代码和文件夹结构中显示的那样,并且我也在使用 tomcat plus 尝试了所有不同的编写方式 'action=.. .' 在 html 中。

编辑:这是我得到的新错误 New Error

【问题讨论】:

  • @Gatusko 已尝试使用 Servlet 返回“HTTP 状态 404 请求的资源 (/servlet) 不可用”但它似乎不起作用,正如我在代码和文件夹结构中所示我也在使用 tomcat plus 尝试了在 html 中编写 'action=...' 的所有不同方式。
  • 通过查看您的错误图像,我们可以得出您正在使用 doGet 方法,因为参数在 url 中传递,但您的代码包含 doPost 方法。您是否使用正确的方法进行状态转移?

标签: servlets intellij-idea web-applications error-handling http-status-code-404


【解决方案1】:

我认为问题在于 HTML 中提供的操作路径删除 ${com.incidents.cashout.CurlExecutor.java} 并尝试它可能有效。通过 sysouts 找出路径。

【讨论】:

  • 那行不通。 “通过 sysouts 查找路径”是什么意思?
  • onclick 尝试在脚本中使用警报或做一件事手动检查哪个路径不会导致 404 错误。
猜你喜欢
  • 2021-03-16
  • 2020-05-28
  • 1970-01-01
  • 2022-09-27
  • 2022-07-14
  • 1970-01-01
  • 2021-12-26
  • 2020-10-23
  • 2021-04-23
相关资源
最近更新 更多