sisisisi
sisisisi

Reputation: 621

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

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:

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

Upvotes: 0

Views: 764

Answers (1)

Ramanlfc
Ramanlfc

Reputation: 8354

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

Upvotes: 2

Related Questions