-1

Web.xml :

Hi , can't solve the error, spent over two days and the same issue. Please help ,do I have any mistake ?

<?xml version="1.0" encoding="ISO-8859-1"?>

<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"
metadata-complete="true">

<display-name>Welcome to Tomcat</display-name>
<description>
Welcome to Tomcat
</description>
<servlet>
<servlet-name>beer</servlet-name>
<servlet-class>com.example.web</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>beer</servlet-name>
<url-pattern>/SelectBeer.do</url-pattern>
</servlet-mapping>
</web-app>

Form.html :

<html><body>
<h1 align="center">Beer Selection Page</h1>
<form method="POST" action="SelectBeer.do">
Select beer characteristics<p>
Color:
<select name="color" size="1">
<option value="light"> light </option>
<option value="amber"> amber </option>
<option value="brown"> brown </option>
<option value="dark"> dark </option>
</select>
<br><br>
<center>
<input type="submit" value="ok"></center>

</form></body></html>

BeerSelect.java
package com.example.web;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class BeerSelect extends HttpServlet {

public void doPost(HttpServletRequest request,

HttpServletResponse response)

throws IOException, ServletException {

response.setContentType("text/html");

PrintWriter out = response.getWriter();

out.println("Beer Selection Advice<br>");

String c = request.getParameter("color");

out.println("<br>Got beer color " + c);
}
}

deployment is

C:\Program Files\tomcat\webapps\beer inside beer is form.html and WEB-INF

inside WEB_INF Web.xml and classes

inside classes is com /example/web/BeerSelect.java

I can log into locathost:8080/beer/form.html but when I chose the color and hit submit the error occur HTTP Status 404 - /beer/SelectBeer.do I have compiled BeerSelect.java

type Status report

message /beer/SelectBeer.do "

description The requested resource is not available. Apache Tomcat/8.0.22

Hazal
  • 1
  • 3

1 Answers1

0

I noticed that your servlet mapping in web.xml maps 'beer' to 'com.example.web' note this should be 'com.example.web.BeerSelect' according to my understanding.

The directory structure is important. You need to create your directory structure inside Tomcat/webapps/

Example Structure could be:
- Beer/Form.html
- Beer/WEB-INF/web.xml
- Beer/WEB-INF/com/example/web/BeerSelect.class

Note: there should be a folder named WEB-INF not WEB_INF etc this could result in a 404, cause tomcat looks for the mapping(DD) inside WEB-INF

The initial statement inside web.xml ' ...'could also cause a problem. Try to copy that statement form one of the example provided by your Tomcat version.

Hope that helps

shadow
  • 141
  • 1
  • 7