Stefan
Stefan

Reputation: 1059

HTTP Status 404 - Why server can't find my servlet I mapped to?

First of all, sorry if this question is a bit longer. I promise the code is super simple and fast to read.

I made a very simple app using Servlets. It has form where user picks beer color. It should take him to servlet I mapped to using XML. Servlet should just send a response back and print the color user picked.

Unfortunately, I have a bug somewhere and I can't understand where. Could someone help me, as I am really stuck?

There are my three files:

form.html:

<!DOCTYPE html>
<html>
<body>

    <form action="SelectBeer.do" method="POST">

        Color:
        <select name="color">
            <option>light</option>
            <option>amber</option>
            <option>brown</option>
            <option>dark</option>
        </select>
        
        <button>Submit</button>

    </form>

</body>
</html>

BeerSelect.java servlet:

package com.example.web;

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

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

public class BeerSelect extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        resp.setContentType("text/html");
        PrintWriter out = resp.getWriter();
        String color = req.getParameter("color");
        out.println("Selected: " + color);

    }
}

web.xml, that I used for mapping:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>MyBeerApp</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  <servlet>
  <servlet-name>myServlet</servlet-name>
  <servlet-class>com.example.web.BeerSelect</servlet-class>
  </servlet>
  
  <servlet-mapping>
  <servlet-name>myServlet</servlet-name>
  <url-pattern>/SelectBeer.do</url-pattern>
  </servlet-mapping>
  
</web-app>

Lastly, I will provide you with ss from my project structure. Once again, thanks in advance for anyone who tries to help. It would mean a lot to me!

enter image description here

P.S. Form runs fine, but when I submit it, I get 404 error.

Upvotes: 1

Views: 407

Answers (2)

Mark
Mark

Reputation: 84

@Stefan This is my project structure. I found it to be a bit different from you.

enter image description here

Upvotes: 2

Mark
Mark

Reputation: 84

I don't think your code wrong. I have tried run and result as your expect. enter image description here

enter image description here

Upvotes: 0

Related Questions