gpt4 book ai didi

html - 尝试在 Intellij IDEA 中运行带有 html 页面的 Web 应用程序,但出现 404 错误

转载 作者:行者123 更新时间:2023-11-28 23:28:53 25 4
gpt4 key购买 nike

我想做一件简单的事情:让用户输入用户名和密码,然后在第二页显示。我使用 tomcat 8 作为服务器。以下是关键代码:

登录.html:

    <!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html" charset="UTF-8">
<title>login</title>
</head>
<body>
<div align="center"><h1>login in</h1>

<form method="post" action="GetPostData" name="login form" align="center">
<table align="center" width="232" border="2">
<tr>
<td>Username</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>Password</td>
<td><input type="text" name="password"></td>
</tr>
</table>
<input type="reset" value="Reset" name="Reset">
<input type="submit" value="Submit" name="Submit">
</form>

</div>

</body>
</html>

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>GetPostData</servlet-name>
<servlet-class>com.ywwynm.GetPostData</servlet-class>
</servlet>

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

</web-app>

GetPostData.java:

package com.ywwynm;

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 = "GetPostData")
public class GetPostData extends HttpServlet {

public void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println(
"<BODY BGCOLOR=\"#FDF5E6\">\n"+
"<H1 ALIGN=CENTER>" +"GetPostData"+"</H1><br>"+
"<UL>\n"+
"<L1><B>username:"+request.getParameter("username")+"<br>"+
"<L1><B>password:"+request.getParameter("password")+"<br>"+
"</UL>"+"\n"+
"</BODY>"+
"</HTML>"
);
out.close();
}

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
processRequest(req, resp);
}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
processRequest(req, resp);
}
}

但是当我点击运行login.html时,它只显示一个静态网页login.html。我输入信息点击提交后,404错误会跳转到页面。

我已经将编译后的类目录设置为web/WEB-INF/classes。

一些线索:

  1. 运行 login.html 的 url 不是以 localhost:8080 开头,而是另一个数字。
  2. Intellij 提醒我无法解析 login.html 中的文件“GetPostData”(在 action="GetPostData")。
  3. 我在 login.html 上单击了运行,但没有找到运行整个模块的方法。

我在谷歌上搜索了如何使用 Intellij 开发 Web 应用程序,但几乎所有的搜索结果都以自动生成的 index.jsp 为例。结果,我不知道我是否应该做其他准备工作来完成所有工作。请帮忙。提前致谢!

最佳答案

我发现我的 login.html 不在文件夹 web 中。我将它移到 web 中,一切正常。

关于html - 尝试在 Intellij IDEA 中运行带有 html 页面的 Web 应用程序,但出现 404 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33447166/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com