0

I created a dynamic web project in Eclipse with a HTML page (in the WebContent folder of the project) that is supposed to send some input data from the user to a servlet (with the name "Tee"). I try to locate the servlet with

form method="get" action="../../src/Tee"

it does not find it. No, its not a Status 404 error message. But simply "the page cannot be displayed."

I tried this:

form method="get" action="/Tee"

as well, does not work either.

The Tomcat server is started and the project is deployed on the server. If i start the servlet itself, it runs on the server without problem (with all the data set to null, as these are supposed to come from the html page).

Yes, there are similary questions out there but those gave no real solution to me.

enter image description here

bebe böbe
  • 11
  • 5
  • What error do you get? 404? – dan1st Aug 16 '20 at 14:25
  • Kein Zugriff auf Seite = "Site can't be reached" – bebe böbe Aug 16 '20 at 14:33
  • You try to access the file, not the site (at least in eclipse) – dan1st Aug 16 '20 at 14:34
  • @bebeböbe - Rename `doPost` to `doGet`. Then right-click the HTML file in Eclipse and click **Run As > Run on server**. Note that it should be `action="Tee"` and not `"/Tee"` or `"../../src/Tee"` in the HTML form. If you were using Spring MVC, any of `"Tee"` and `"/Tee"` would have been fine. – Arvind Kumar Avinash Aug 16 '20 at 14:41
  • if I try "Run on server", I get a message "The origin server did not find a current representation for the target resource or is not willing to disclose that one exists" – bebe böbe Aug 16 '20 at 14:45
  • @bebeböbe - Stop the server and Then right-click the HTML file in Eclipse and click **Run As > Run on server**. – Arvind Kumar Avinash Aug 16 '20 at 14:47
  • thanks but on "run on server " I get the error message "The origin server did not find a current representation for the target resource or is not willing to disclose that one exists." – bebe böbe Aug 16 '20 at 14:59
  • no, its not the same question, its a different error message – bebe böbe Aug 16 '20 at 18:51
  • It may not be exactly the same question, but the core problem and the answer is definitely the same. There's no need to copypaste the answer here. – BalusC Aug 16 '20 at 20:55

1 Answers1

0

You need to write a servlet class something like:

@WebServlet("/Tee")
public class Tee extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/plain");
        PrintWriter out = response.getWriter();
        out.write("Hello World!");
        //...
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //...
    }
}

After writing the servlet, right-click its name in Eclipse and click Run As > Run on server

If it runs successfully in the Eclipse browser, it will be accessible in your HTML with its name e.g.

<form method="GET" action="Tee">
    <input type="submit">
</form>

Update

After you posted the servlet code, I can see a problem there. You have written method="GET" in the HTML form but in the servlet code, you have just doPost. You should have doGet in the servlet for method="GET" in the HTML form to be acted on. Add the method, doGet in addition to doPost in the servlet.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110