0

There have been multiple questions like this on SO, but none of those solved my problem and I can't think of anything left that might cause this error.

I'm trying to write a simple servlet that just prints "Hello World":

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class PrintServlet extends HttpServlet {


     public void doGet(HttpServletRequest request,
              HttpServletResponse response) throws ServletException, IOException {
         PrintWriter out = response.getWriter();
         out.println("Hello World");
     }
}

What I did was compile the java file, move the compiled file into /usr/local/apache-tomcat-7.0.65/webapps/examples/WEB-INF/classes, then add these to the web.xml file located at /usr/local/apache-tomcat-7.0.65/webapps/examples/WEB-INF:

<servlet>
  <serlet-name>PrintServlet</servlet-name>
  <servlet-class>PrintServlet</servlet-name>
</servlet>

<servlet-mappinng>
<servlet-name>PrintServlet</servlet-name>
<url-pattern>/servlet/PrintServlet</url-pattern>
</servlet-mapping>

And tried to access the servlet using http://localhost:8080/servlet/PrintServlet.

And I get the error message: Status 404 The requested resource is not available.

I don't see where there could be a mistake, I'm simply following tutorials. Can anybody help me please?

EDIT:

log file at catalina.out (got this by calling tail -f catalina.out, not sure how else to open this file).

Dec 10, 2015 4:46:02 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory /usr/local/apache-tomcat-7.0.65/webapps/examples has finished in 130 ms
Dec 10, 2015 4:46:02 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /usr/local/apache-tomcat-7.0.65/webapps/host-manager
Dec 10, 2015 4:46:02 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory /usr/local/apache-tomcat-7.0.65/webapps/host-manager has finished in 80 ms
Dec 10, 2015 4:46:02 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /usr/local/apache-tomcat-7.0.65/webapps/manager
Dec 10, 2015 4:46:02 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory /usr/local/apache-tomcat-7.0.65/webapps/manager has finished in 96 ms
Dec 10, 2015 4:46:02 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /usr/local/apache-tomcat-7.0.65/webapps/ROOT
Dec 10, 2015 4:46:02 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory /usr/local/apache-tomcat-7.0.65/webapps/ROOT has finished in 62 ms
Dec 10, 2015 4:46:02 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
Dec 10, 2015 4:46:02 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
Dec 10, 2015 4:46:02 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 918 ms

Startup message when calling ./startup.sh:

Using CATALINA_BASE:   /usr/local/apache-tomcat-7.0.65
Using CATALINA_HOME:   /usr/local/apache-tomcat-7.0.65
Using CATALINA_TMPDIR: /usr/local/apache-tomcat-7.0.65/temp
Using JRE_HOME:        /Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/Home
Using CLASSPATH:       /usr/local/apache-tomcat-7.0.65/bin/bootstrap.jar:/usr/local/apache-tomcat-7.0.65/bin/tomcat-juli.jar
Tomcat started.
eager2learn
  • 1,447
  • 4
  • 24
  • 47

1 Answers1

2

In your web.xml you say you have this:

<servlet>
  <serlet-name>PrintServlet</servlet-name>
  <servlet-class>PrintServlet</servlet-name>
</servlet>

See the <servlet-class> PrintServlet </servlet-name> difference?

It should be like this:

<servlet>
  <serlet-name>test.PrintServlet</servlet-name>
  <servlet-class>PrintServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
</servlet>

Correct this, restart and report if it works ;-)

If that's not working:

I suppose you make the mapping even more simple:

<servlet-mappinng>
  <servlet-name>PrintServlet</servlet-name>
  <url-pattern>/print</url-pattern>
</servlet-mapping>

Which should resolve to http://localhost:8080/examples/print

And add some debug output to your servlet:

package test;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class PrintServlet extends HttpServlet {

     static {
       System.out.println("STACKOVERFLOW:> I've been loaded!");
     }

     public void doGet(HttpServletRequest request,
              HttpServletResponse response) throws ServletException, IOException {
         PrintWriter out = response.getWriter();
         out.println("Hello World");
     }
}

Recompile, put in examples/WEB-INF/classes, restart tomcat and watch for the line to show up. Or does log show error messages?

Jan
  • 13,738
  • 3
  • 30
  • 55
  • That's one hell of an eye man!!! :) or good IDE – Milan Dec 10 '15 at 14:32
  • Thanks for your help, but unfortunately even after correcting this I still get the same error message. Maybe the problem is in the specified url-pattern. Is it correct to write /servlet/PrintServlet and then calling "localhost:8080/examples/servlet/PrintServlet" in a browser? – eager2learn Dec 10 '15 at 14:40
  • see my edit - hopefully that helps – Jan Dec 10 '15 at 14:48
  • Please make sure your servlet resides in $TOMCAT_HOME/webapps/your-project-directory/WEB-INF/classes. Error 404 means your servlet is not there where it should be – Ghayel Dec 10 '15 at 15:13
  • @Jan I really appreciate your help, unfortunately it still doesn't work. I don't get the line you put into the servlet when trying to open http://localhost:8080/examples/print in a browser. This is the log message for that request: 0:0:0:0:0:0:0:1 - - [10/Dec/2015:16:14:30 +0100] "GET /examples/print HTTP/1.1" 404 979 – eager2learn Dec 10 '15 at 15:21
  • can you share your startup-log for your tomcat? The line is supposed to be shown *at startup* - not when servlet is called. – Jan Dec 10 '15 at 15:26
  • see edit - load-on-startup added to web.xml as well as moved the servlet to package test. You'd need to put class in WEB-INF/classes/test/* now. – Jan Dec 10 '15 at 15:28
  • What exactly do you mean by startup-log? Is that simply the message after calling startup.sh? – eager2learn Dec 10 '15 at 15:45
  • yes. or tomcat/logs/catalina.out – Jan Dec 10 '15 at 15:50
  • Ok I edited my question to post the logs. I opened tomcat after I did what you suggested in your last edit. – eager2learn Dec 10 '15 at 16:03
  • oh man sorry - not much more I can do. I *know* this works - but without actually checking your system we will not get much further. Maybe you start over and create a *new* webapp - a new folder in tomcat/webapps/yourapp. In there one folder WEB-INF/ with minimal web.xml (only your one servlet) and WEB-INF/classes/test/ with your compiled PrintServlet.class (if you put that in package test that is) – Jan Dec 10 '15 at 17:15
  • Ok I'll try that. Thanks so much for your help, really appreciate it. – eager2learn Dec 10 '15 at 17:53