Apache tomcat and servlet (beginner) - 404 error, maybe classes are not found?












0















I'm a beginner in servlets and for a "hello world style" servlet I installed apache tomcat and created the following under it's webapps directory:



servletdir
|- index.xhtml
|- WEB-INF
|- web.xml
|- classes
|- mypackage
|- ServletClass.class


Initialized everything so I could access my servlet on localhost:



web.xml



<?xml version="1.0" encoding="utf-8"?>
<web-app>

<servlet>
<servlet-name>myservlet</servlet-name>
<servlet-class>mypackage.ServletClass</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>myservlet</servlet-name>
<url-pattern>/servleturl/*</url-pattern>
</servlet-mapping>

</web-app>


and the index.xhtml contains a form that has action="/servleturl" method="get"



In theory, when I start up tomcat, I should be able to access both the servlet and index.xhtml file now through localhost:8080/servletdir/index.xhtml and submitting the form there. Reaching index is no problem, but when I submit the form, I get a 404 error for the servlet, saying that it "The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.".



My servlet class should not be the problem, it extends HttpServlet and overrides doGet to output a simple html via the ServletResponse parameter's getWriter().println(). Can an error in the servlet class cause a 404? Is my directory structure or web.xml wrong?



I have tried multiple directory structures and solutions, including:




  • flat dir structure in WEB-INF and class not being in package (no "classes" directory)

  • no WEB-INF, everything in servlet dir directly

  • wrap WEB-INF and index.xhtml in a directory named "WebContent"

  • keep "classes" directory but use no package, put class files directly into classes*


I have followed multiple resources including some tutorials and many questions here which are not directly related to my problem, but contain some info, but as I'm a complete newbie to this technology I can't deduce which resource is good and which isn't, that't why I tried almost everything I found.



The directory structure described at the top is directly on the disk as this structure, not in a war file, but as I can access some parts, I guess this isn't the problem.



*: note that I recompiled the java files with the according "package" at the file's top line every time the directory structure representing the package changed, that's not the problem










share|improve this question



























    0















    I'm a beginner in servlets and for a "hello world style" servlet I installed apache tomcat and created the following under it's webapps directory:



    servletdir
    |- index.xhtml
    |- WEB-INF
    |- web.xml
    |- classes
    |- mypackage
    |- ServletClass.class


    Initialized everything so I could access my servlet on localhost:



    web.xml



    <?xml version="1.0" encoding="utf-8"?>
    <web-app>

    <servlet>
    <servlet-name>myservlet</servlet-name>
    <servlet-class>mypackage.ServletClass</servlet-class>
    </servlet>

    <servlet-mapping>
    <servlet-name>myservlet</servlet-name>
    <url-pattern>/servleturl/*</url-pattern>
    </servlet-mapping>

    </web-app>


    and the index.xhtml contains a form that has action="/servleturl" method="get"



    In theory, when I start up tomcat, I should be able to access both the servlet and index.xhtml file now through localhost:8080/servletdir/index.xhtml and submitting the form there. Reaching index is no problem, but when I submit the form, I get a 404 error for the servlet, saying that it "The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.".



    My servlet class should not be the problem, it extends HttpServlet and overrides doGet to output a simple html via the ServletResponse parameter's getWriter().println(). Can an error in the servlet class cause a 404? Is my directory structure or web.xml wrong?



    I have tried multiple directory structures and solutions, including:




    • flat dir structure in WEB-INF and class not being in package (no "classes" directory)

    • no WEB-INF, everything in servlet dir directly

    • wrap WEB-INF and index.xhtml in a directory named "WebContent"

    • keep "classes" directory but use no package, put class files directly into classes*


    I have followed multiple resources including some tutorials and many questions here which are not directly related to my problem, but contain some info, but as I'm a complete newbie to this technology I can't deduce which resource is good and which isn't, that't why I tried almost everything I found.



    The directory structure described at the top is directly on the disk as this structure, not in a war file, but as I can access some parts, I guess this isn't the problem.



    *: note that I recompiled the java files with the according "package" at the file's top line every time the directory structure representing the package changed, that's not the problem










    share|improve this question

























      0












      0








      0








      I'm a beginner in servlets and for a "hello world style" servlet I installed apache tomcat and created the following under it's webapps directory:



      servletdir
      |- index.xhtml
      |- WEB-INF
      |- web.xml
      |- classes
      |- mypackage
      |- ServletClass.class


      Initialized everything so I could access my servlet on localhost:



      web.xml



      <?xml version="1.0" encoding="utf-8"?>
      <web-app>

      <servlet>
      <servlet-name>myservlet</servlet-name>
      <servlet-class>mypackage.ServletClass</servlet-class>
      </servlet>

      <servlet-mapping>
      <servlet-name>myservlet</servlet-name>
      <url-pattern>/servleturl/*</url-pattern>
      </servlet-mapping>

      </web-app>


      and the index.xhtml contains a form that has action="/servleturl" method="get"



      In theory, when I start up tomcat, I should be able to access both the servlet and index.xhtml file now through localhost:8080/servletdir/index.xhtml and submitting the form there. Reaching index is no problem, but when I submit the form, I get a 404 error for the servlet, saying that it "The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.".



      My servlet class should not be the problem, it extends HttpServlet and overrides doGet to output a simple html via the ServletResponse parameter's getWriter().println(). Can an error in the servlet class cause a 404? Is my directory structure or web.xml wrong?



      I have tried multiple directory structures and solutions, including:




      • flat dir structure in WEB-INF and class not being in package (no "classes" directory)

      • no WEB-INF, everything in servlet dir directly

      • wrap WEB-INF and index.xhtml in a directory named "WebContent"

      • keep "classes" directory but use no package, put class files directly into classes*


      I have followed multiple resources including some tutorials and many questions here which are not directly related to my problem, but contain some info, but as I'm a complete newbie to this technology I can't deduce which resource is good and which isn't, that't why I tried almost everything I found.



      The directory structure described at the top is directly on the disk as this structure, not in a war file, but as I can access some parts, I guess this isn't the problem.



      *: note that I recompiled the java files with the according "package" at the file's top line every time the directory structure representing the package changed, that's not the problem










      share|improve this question














      I'm a beginner in servlets and for a "hello world style" servlet I installed apache tomcat and created the following under it's webapps directory:



      servletdir
      |- index.xhtml
      |- WEB-INF
      |- web.xml
      |- classes
      |- mypackage
      |- ServletClass.class


      Initialized everything so I could access my servlet on localhost:



      web.xml



      <?xml version="1.0" encoding="utf-8"?>
      <web-app>

      <servlet>
      <servlet-name>myservlet</servlet-name>
      <servlet-class>mypackage.ServletClass</servlet-class>
      </servlet>

      <servlet-mapping>
      <servlet-name>myservlet</servlet-name>
      <url-pattern>/servleturl/*</url-pattern>
      </servlet-mapping>

      </web-app>


      and the index.xhtml contains a form that has action="/servleturl" method="get"



      In theory, when I start up tomcat, I should be able to access both the servlet and index.xhtml file now through localhost:8080/servletdir/index.xhtml and submitting the form there. Reaching index is no problem, but when I submit the form, I get a 404 error for the servlet, saying that it "The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.".



      My servlet class should not be the problem, it extends HttpServlet and overrides doGet to output a simple html via the ServletResponse parameter's getWriter().println(). Can an error in the servlet class cause a 404? Is my directory structure or web.xml wrong?



      I have tried multiple directory structures and solutions, including:




      • flat dir structure in WEB-INF and class not being in package (no "classes" directory)

      • no WEB-INF, everything in servlet dir directly

      • wrap WEB-INF and index.xhtml in a directory named "WebContent"

      • keep "classes" directory but use no package, put class files directly into classes*


      I have followed multiple resources including some tutorials and many questions here which are not directly related to my problem, but contain some info, but as I'm a complete newbie to this technology I can't deduce which resource is good and which isn't, that't why I tried almost everything I found.



      The directory structure described at the top is directly on the disk as this structure, not in a war file, but as I can access some parts, I guess this isn't the problem.



      *: note that I recompiled the java files with the according "package" at the file's top line every time the directory structure representing the package changed, that's not the problem







      java tomcat servlets http-status-code-404






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 28 '18 at 14:02









      sisisisisisisisi

      8428




      8428
























          1 Answer
          1






          active

          oldest

          votes


















          2














          change action="/servleturl" method="get" to action="/servletdir/servleturl" method="get" or action="servleturl" method="get".



          with action="/servleturl",you are trying to access a root relative path but you are missing the context path servletdir.



          If you use the action="servleturl", you don't need to put the context path in the action as it will get it from the current url






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53521212%2fapache-tomcat-and-servlet-beginner-404-error-maybe-classes-are-not-found%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            2














            change action="/servleturl" method="get" to action="/servletdir/servleturl" method="get" or action="servleturl" method="get".



            with action="/servleturl",you are trying to access a root relative path but you are missing the context path servletdir.



            If you use the action="servleturl", you don't need to put the context path in the action as it will get it from the current url






            share|improve this answer




























              2














              change action="/servleturl" method="get" to action="/servletdir/servleturl" method="get" or action="servleturl" method="get".



              with action="/servleturl",you are trying to access a root relative path but you are missing the context path servletdir.



              If you use the action="servleturl", you don't need to put the context path in the action as it will get it from the current url






              share|improve this answer


























                2












                2








                2







                change action="/servleturl" method="get" to action="/servletdir/servleturl" method="get" or action="servleturl" method="get".



                with action="/servleturl",you are trying to access a root relative path but you are missing the context path servletdir.



                If you use the action="servleturl", you don't need to put the context path in the action as it will get it from the current url






                share|improve this answer













                change action="/servleturl" method="get" to action="/servletdir/servleturl" method="get" or action="servleturl" method="get".



                with action="/servleturl",you are trying to access a root relative path but you are missing the context path servletdir.



                If you use the action="servleturl", you don't need to put the context path in the action as it will get it from the current url







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 28 '18 at 14:17









                RamanlfcRamanlfc

                7,14311022




                7,14311022
































                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53521212%2fapache-tomcat-and-servlet-beginner-404-error-maybe-classes-are-not-found%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Lallio

                    Unable to find Lightning Node

                    Futebolista