gpt4 book ai didi

tomcat - 在 NetBeans 中运行 TimeForm servlet 项目时出现 HTTP 状态 404 错误

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

<分区>

我正在尝试在使用 NetBeans 创建的 Web 项目中测试以下 servlet。它构成了一个简单的表单,允许用户从列表中选择区域设置和时区并相应地显示相关信息。

这是 servlet 代码:

public class TimeForm extends HttpServlet {
private static final String CONTENT_TYPE = "text/html";
private Locale[] allLocale = Locale.getAvailableLocales();
private String[] allTimeZone = TimeZone.getAvailableIDs();

/** Process the HTTP Get request */
public void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
out.println("<h3>Choose locale and time zone</h3>");
out.println("<form method=\"post\" action=" +
"/localhost:8080/TimeForm>");
out.println("Locale <select size=\"1\" name=\"locale\">");

// Fill in all locales
for (int i = 0; i < allLocale.length; i++) {
out.println("<option value=\"" + i +"\">" +
allLocale[i].getDisplayName() + "</option>");
}
out.println("</select>");

// Fill in all time zones
out.println("<p>Time Zone<select size=\"1\" name=\"timezone\">");
for (int i = 0; i < allTimeZone.length; i++) {
out.println("<option value=\"" + allTimeZone[i] +"\">" +
allTimeZone[i] + "</option>");
}
out.println("</select>");
out.println("<p><input type=\"submit\" value=\"Submit\" >");
out.println("<input type=\"reset\" value=\"Reset\"></p>");
out.println("</form>");
out.close(); // Close stream
}

/** Process the HTTP Post request */
public void doPost(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
out.println("<html>");
int localeIndex = Integer.parseInt(
request.getParameter("locale"));
String timeZoneID = request.getParameter("timezone");
out.println("<head><title>Current Time</title></head>");
out.println("<body>");
Calendar calendar =
new GregorianCalendar(allLocale[localeIndex]);
TimeZone timeZone = TimeZone.getTimeZone(timeZoneID);
DateFormat dateFormat = DateFormat.getDateTimeInstance(
DateFormat.FULL, DateFormat.FULL, allLocale[localeIndex]);
dateFormat.setTimeZone(timeZone);
out.println("Current time is " +
dateFormat.format(calendar.getTime()) + "</p>");
out.println("</body></html>");
out.close(); // Close stream
}
}

这是 web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" 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">
<servlet>
<servlet-name>TimeForm</servlet-name>
<servlet-class>hw5.TimeForm</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TimeForm</servlet-name>
<url-pattern>/TimeForm</url-pattern>
</servlet-mapping>
</web-app>

当我通过 NetBeans 运行项目时,代码似乎可以正常编译,我可以选择区域设置和时区。但是当我点击提交时,出现以下错误:

enter image description here

有人可以向我解释为什么会发生这种情况以及如何解决它吗?

enter image description here enter image description here enter image description here

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