HTTP status 404 error running TimeForm servlet project in NetBeans

bastille77

I'm trying to test the following servlet in a web project created with NetBeans. It constitutes a simple form which allows users to select locale and timezone from a list and display relevant information accordingly.

Here is the servlet code:

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
  }
}

And this is the 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>

When I run the project through NetBeans, the code seems to compile fine and I get to select the locale and timezone. But when I click Submit, I get the following error:

enter image description here

Could someone explain to me why this is happening and how to go about fixing it?

enter image description here enter image description hereenter image description here

bastille77

After fiddling with this for another couple hours, I finally figured out that the problem lies in the fact that I wrote the wrong path in the form tag in the servlet's doGet method.

Instead of

out.println("<form method=\"post\" action=" +
      "/localhost:8080/TimeForm>");

It should be

out.println("<form method=\"post\" action=" +
      "/TestTimeForm/TimeForm>");

Now when I run the servlet and click the submit button, there is no more HTTP status 404.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

HTTP Status 404 - Servlet not found

Servlet: HTTP Status 404 - Not Found

JBoss error report: HTTP Status 404 - Servlet is not available

HTTP Server Status 404 Error in Spring MVC Project

404 error in tomcat, Java Servlet, Apache Netbeans

Java Servlet HTTP Status Code 404

HTTP Status 404 - Not Found - Servlet and JSP

Maven web project HTTP Status [404] – [Not Found]

Why do I always get Whitelabel Error Page with status “404” while running Spring Boot Project

HTTP Status 500 Error instantiating servlet class

Error instantiating servlet class - HTTP Status 500

HTTP ERROR 404 accessing "name" Not Found (Servlet)

Java Servlet, http status 404, The requested resource is not available

Node express 404 error HTTP status code

Eclipse & Tomcat Error: HTTP Status 404 – Not Found:

Servlet error : HTTP Status 405 - HTTP method GET is not supported by this URL

HTTP Status 404 - /com.demo/rest/hello - with Rest project

HTTP Status 500 - Error instantiating servlet class servlets.LoginServlet

servlet-HTTP Status 500 Internal Server Error

unknown Servlet exception throw error of HTTP Status 500

HTTP Status 500 - Error instantiating servlet in eclipse maven

Java Servlets - HTTP Status 500 Error instantiating servlet class

Java - Servlet 404 error

Error 404 with JSP and Servlet

404 Error (Servlet)

HTTP Status 404 - /LoginAuth

HTTP Status 404 -

Struts2 confgured in netbeans 8 returning HTTP Status 404 - Not Found

Scrapy Error: Ignoring response <404 ...> : HTTP status code is not handled or not allowed