why is it so difficult to test the simplest servlet on Tomcat 6.0?
I can't seem to get even the simplest servlet up and running on Apache Tomcat 6.0 and I'm starting to get really frustrated. Can someone please help me? Where are you supposed to put compiled servlets and how can you access them through your browser without having to modify the configuration files?
[308 byte] By [
@modia] at [2007-11-26 22:49:13]

# 1
Hi Modi,put your all servlet classes in /WEB-INF/classes folder.
# 2
I compiled this servlet from a file called Test.java and put it in my $CATALINA_HOME/webapps/Test/WEB-INF/classes directory:
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
//Java SE packages
import java.io.IOException;
import java.io.PrintWriter;
public class Test
extends HttpServlet
{
public static final int MAX_DEREFERENCES = 10;
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
final String pageName = request.getParameter("page");
final int dereferences = Integer.parseInt(request.getParameter("depth"));
//response.setContentType("text/html");
PrintWriter page = response.getWriter();
page.println("out");
}
}
I got HTTP 404 errors when I tried all three of the following URLs:
http://localhost/Test/
http://localhost/servlet/Test/
http://localhost/servlets/Test/
I tried the same putting Test.class in $CATALINA_HOME/webapps/ROOT/WEB-INF/classes and got the same result.
@modia at 2007-7-10 12:09:32 >

# 3
Edit your $CATALINA_HOME/conf/web.xml - check that the "invoker" servlet is not being commented out:
<servlet>
<servlet-name>invoker</servlet-name>
<servlet-class>
org.apache.catalina.servlets.InvokerServlet
</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>0</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
The above is my experience with Tomcat 5. I believe it should still be valid with Tomcat 6.
# 4
I tried uncommenting the following lines from $CATALINA_HOME/conf/web.xml
<servlet>
<servlet-name>invoker</servlet-name>
<servlet-class>
org.apache.catalina.servlets.InvokerServlet
</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>0</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
and
<servlet-mapping>
<servlet-name>invoker</servlet-name>
<url-pattern>/servlet/*</url-pattern>
</servlet-mapping>
and then tried
http://localhost/Test/
http://localhost/servlet/Test/
http://localhost/servlets/Test/
These URLs now all return blank pages instead of 404 errors.
@modia at 2007-7-10 12:09:32 >

# 5
I just tried http://localhost/ and now that returns a blank page as well. Anybody know why this is happening?
@modia at 2007-7-10 12:09:32 >

# 6
Hi,Did you manage to solve your problem with Tomcat 6.0 running a simple servlet?I tried all the mentioned steps already, but could not solve it yet.Thanks,Mahua
Mahuaa at 2007-7-10 12:09:32 >

# 7
Hi,
First thing, u don't need to configure for Apache Tomcat 6.0. if u type localhost:8080, it will give u a default page. if u scroll it down, u can see the options to access the helloworld.java servlet file. if u click that , it will takes u to that location. On that location, u can put ur compiled class files and run it through the browser.
# 8
Ok, I tried that, but now I'm getting a HTTP Status 500 error (the HelloWorldExample that comes with Tomcat works fine though). Why is this happening? The exception its throwing isjava.lang.ClassNotFoundException: HelloWorldExample
@modia at 2007-7-10 12:09:32 >

# 9
I figured out why I was having problems with my Tomcat installation. I didn't set the default context to privileged. For anyone who has the same problem in the future and reads this thread, this is a description of how to fix it (for Tomcat 6.0). Also, if you are experiencing any other problems with Tomcat 6.0 installation, a good step by step description of Tomcat configuration is online at http://www.coreservlets.com/Apache-Tomcat-Tutorial/
Edit the following line in <tomcat installation directory>/conf/context.xml (it should be on the second line):
<Context>
and change it to:
<Context privileged="true">
or, optionally, if you want to enable servlet reloading (which allows you edit and update your servlets without having to restart the server), change it to:
<Context reloadable="true" privileged="true">
Also, thanks to everyone that posted here for trying to help me fix it.
@modia at 2007-7-10 12:09:32 >
