0
<form action="../SelectBeer.do" method="POST">
    <p>Select Beer Characteristics</p>
    <p>Color</p>
    <select name="color" size="1">
        <option>light</option>
        <option>amber</option>
        <option>brown</option>
        <option>dark</option>
    </select>
    <br><br>
    <center>
    <input type="submit">
    </center>
</form>

Here is the code for my form.html and its path is /Library/Tomcat/webapps/Project/form.html

package com.example.web;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
@WebServlet("/SelectBeer.do")
class BeerSelect extends HttpServlet
{   

public void doPost(HttpServletRequest request,HttpServletResponse reponse) throws ServletException,IOException
{   
    reponse.setContentType("text/html");
    PrintWriter out=reponse.getWriter();
    out.println("Beer Selection Advice<br>");
    String c=request.getParameter("color");
    out.println("<br>Got Beer Color "+ c);
}
}

And this is my BeerSelect.java file and its path is /Users/MyName/Desktop/ServletProjects/BeerAdvisor/src/com/example/web/BeerSelect.java

<?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">
<servlet>
    <servlet-name>Beer</servlet-name>
    <servlet-class>com.example.web.BeerSelect</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>Beer</servlet-name>
    <url-pattern>/SelectBeer.do</url-pattern>
</servlet-mapping>

When I submit the form the page shows Error404 Not Found, The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.Can someone please help.

  • Under what URL you are getting that error? – Pshemo Nov 12 '17 at 18:27
  • http://localhost:8080/SelectBeer.do – akshansh narula Nov 12 '17 at 18:28
  • tried all those solutions, still not working – akshansh narula Nov 12 '17 at 18:38
  • What is the URL of page holding forms? What does `..` represent in `action` attribute? – Pshemo Nov 12 '17 at 18:42
  • localhost:8080/BeerAdvisor/form.html – akshansh narula Nov 12 '17 at 18:43
  • .. was for the parent folder, I tried action="/SelectBeer.do", yet it didn't work – akshansh narula Nov 12 '17 at 18:44
  • So as you see URL looks something like `server/projectName/location`. When you specify location of servlet in web.xml it describes only `/location` part, but `server/projectName` is still mandatory (at least on server you are using). If you use `..` you are moving to `server/` which means `localhost:8080/SelectBeer.do` instead of `localhost:8080/BeerAdvisor/SelectBeer.do`. Remove that `..` and if you are still getting that error try republishing your application/restarting server. – Pshemo Nov 12 '17 at 18:49
  • So do i need to change my url pattern from /SelectBeer.do to /BeerAdvisor/SelectBeer.do?? – akshansh narula Nov 12 '17 at 18:57
  • If you are talking about web.xml then NO. Such change would mean that URL to servlet would be `localhost:8080/BeerAdvisor/BeerAdvisor/SelectBeer.do`. Like I said earlier you need to remove `..` from `action` attribute. You may also need to save your code, republish it on server and possibly restart server. – Pshemo Nov 12 '17 at 19:00
  • I already did that and i also restarted the server yet it is not working – akshansh narula Nov 12 '17 at 19:01
  • Oh, you may also need to make you servlet public (I am not so sure about that and can't check it now). Try adding `public` before `class BeerSelect extends HttpServlet`. – Pshemo Nov 12 '17 at 19:03
  • OK, I just checked duplicate question where it is explicitly mentioned that `public class YourServlet extends HttpServlet { // Must be public and extend HttpServlet.` – Pshemo Nov 12 '17 at 19:04
  • Made the class public, still not working. After submitting the form the url coming is : http://localhost:8080/SelectBeer.do, is it ok?? – akshansh narula Nov 12 '17 at 19:08
  • In that case your `action` is probably `action="/SelectBeer.do"`. I didn't tell you to remove `/` so you probably left it there. If you start href, action, src (or other) attributes from `/` like `/foo` it will treat `/` as root directory, not *current* directory. So `/SelectBeer.do` will represent `server/SelectBeer.do` not `server/projectName/SelectBeer.do`. To solve this problem you can either remove `/` or add `.` before it to represent location related to directory which holds current resource (form.htm). So try with `action="SelectBeer.do"` or `action="./SelectBeer.do"`. – Pshemo Nov 12 '17 at 19:23

0 Answers0