Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Registering servlets

Status
Not open for further replies.

danglingpointers

Technical User
Jul 29, 2005
18
US
I have had Tomcat 4.1 up and running on my linux box for some time and now I am giving Tomcat 5.5 a try on windows and I seem to be missing some nuance.

I have a standard servlet program for which I have created the following directory structure under %CATALINA_HOME%\webapps\myProject\WEB-INF\classes:

\controllers
\utils
\daos

I reference one of my controllers in a jsp as /servlet/controllers.myServlet. But I got a 404. So I tried registering the servlets in my own web.xml file (located in %CATALINA_HOME%\webapps\myProject\WEB-INF).

Thus far I have the following

Code:
<servlet>
  <servlet-name>myServlet</servlet>
  <servlet-class>controllers.myServlet</servlet-class>
	<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
   <servlet-name>myServlet</servlet>
   <url-pattern>/servlet/controllers.myServlet</url-pattern>
</servlet-mapping>

But still I get a 404. There must be some minute detail that I am not seeing here.

All help is greatly appreciated.
 
Try :

Code:
<servlet>
  <servlet-name>myServlet</servlet>
  <servlet-class>controllers.myServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
   <servlet-name>myServlet</servlet>
   <url-pattern>/servlet/myServlet</url-pattern>
</servlet-mapping>

And access via
--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Try adding a welcome file :

<welcome-file-list>
<welcome-file>home.jsp</welcome-file>
<welcome-file>home.html</welcome-file>
</welcome-file-list>

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
I am using the default web.xml file from %CATALINA_HOME%\conf that I have copied into %CATALINA_HOME%\webapps\myProject\WEB-INF\ so that all of the <welcome-file-list> entries are the default ones. I also have an index.html file located in
%CATALINA_HOME%\webapps\myProject.

Moreover, whenever I try to access the index file directly through I get a 404. If I turn off the <servlet>myServlet</servlet> tags, then I can access the index file both directly and through But then I cannot access I also tried turning on the invoker servlet (I uncommented both the <servlet>invoker</servlet> and the <servlet-mapping>invoker</servlet-mapping> tags in my web.xml) but still the problem exists.

Tomcat seems to be having issues resolving anything but the root directory whenever I turn on a <servlet> tag besides the <servlet>default</servlet>.
 
Have you actually got an index.html file ?

Yes, I had listed its location in my previous post (%CATALINA_HOME%\webapps\myProject\index.html).

Here is my web.xml that I have running in %CATALINA_HOME%\conf. Another interesting behavior is that whenever I add a web.xml file to %CATALINA_HOME%\webapps\myProject\WEB-INF, I can no longer access even though I could before when the web.xml file was in the %CATALINA_HOME%\conf directory.



<!-- ================== Built In Servlet Definitions ==================== -->


<servlet>
<servlet-name>default</servlet-name>
<servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>0</param-value>
</init-param>
<init-param>
<param-name>listings</param-name>
<param-value>false</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>



<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>





<servlet>
<servlet-name>jsp</servlet-name>
<servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
<init-param>
<param-name>fork</param-name>
<param-value>false</param-value>
</init-param>
<init-param>
<param-name>xpoweredBy</param-name>
<param-value>false</param-value>
</init-param>
<load-on-startup>3</load-on-startup>
</servlet>




<!--
<servlet>
<servlet-name>ssi</servlet-name>
<servlet-class>
org.apache.catalina.ssi.SSIServlet
</servlet-class>
<init-param>
<param-name>buffered</param-name>
<param-value>1</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>0</param-value>
</init-param>
<init-param>
<param-name>expires</param-name>
<param-value>666</param-value>
</init-param>
<init-param>
<param-name>isVirtualWebappRelative</param-name>
<param-value>0</param-value>
</init-param>
<load-on-startup>4</load-on-startup>
</servlet>
-->


<!--
<servlet>
<servlet-name>cgi</servlet-name>
<servlet-class>org.apache.catalina.servlets.CGIServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>0</param-value>
</init-param>
<init-param>
<param-name>cgiPathPrefix</param-name>
<param-value>WEB-INF/cgi</param-value>
</init-param>
<load-on-startup>5</load-on-startup>
</servlet>
-->

<!------ USER-DEFINED SERVLETS ---------------->

<servlet>
<servlet-name>eventsController</servlet>
<servlet-class>controllers.eventsController</servlet-class>
<load-on-startup>4</load-on-startup>
</servlet>

<!-- ================ Built In Servlet Mappings ========================= -->


<!-- The servlet mappings for the built in servlets defined above. Note -->
<!-- that, by default, the CGI and SSI servlets are *not* mapped. You -->
<!-- must uncomment these mappings (or add them to your application's own -->
<!-- web.xml deployment descriptor) to enable these services -->

<!-- The mapping for the default servlet -->

<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

<servlet-mapping>
<servlet-name>eventsController</servlet>
<url-pattern>/servlet/eventsController</url-pattern>
</servlet-mapping>

<!-- The mapping for the invoker servlet -->


<servlet-mapping>
<servlet-name>invoker</servlet-name>
<url-pattern>/servlet/*</url-pattern>
</servlet-mapping>


<!-- The mapping for the JSP servlet -->
<servlet-mapping>
<servlet-name>jsp</servlet-name>
<url-pattern>*.jsp</url-pattern>
</servlet-mapping>

<servlet-mapping>
<servlet-name>jsp</servlet-name>
<url-pattern>*.jspx</url-pattern>
</servlet-mapping>

<!-- The mapping for the SSI servlet -->
<!--
<servlet-mapping>
<servlet-name>ssi</servlet-name>
<url-pattern>*.shtml</url-pattern>
</servlet-mapping>
-->

<!-- The mapping for the CGI Gateway servlet -->

<!--
<servlet-mapping>
<servlet-name>cgi</servlet-name>
<url-pattern>/cgi-bin/*</url-pattern>
</servlet-mapping>
-->

<!-- leaving out the mime definitions -->


<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

</web-app>

I have removed the comment sections to reduce the size.
 
Wow ! Thats a crazy web.xml.

1) Leave the TOMCAT_HOME/conf/web.xml alone - never touch it.

2) Your own webapp should define its own web.xml - which is NOT copied from conf !

I really don't think you need anything more than :

Code:
<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app
    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    "file:///usr/tomcat/web-app_2_2.dtd">

<web-app>
	<servlet>
	  <servlet-name>myServlet</servlet>
	  <servlet-class>controllers.myServlet</servlet-class>
	    <load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
	   <servlet-name>myServlet</servlet>
	   <url-pattern>/servlet/myServlet</url-pattern>
	</servlet-mapping>
	
	
	<welcome-file-list> 
		<welcome-file>somedefaultpage.jsp</welcome-file> 
	</welcome-file-list> 	
</web-app>



--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Ok, I uninstalled/reinstalled Tomcat 5.5.17, dumped the contents of my %CATALINA_HOME%\webapps\myProject directory and started fresh.

I added the ..\webapps\myProject contents to the new installation and then started the server and tested it. Again, when I navigate to Tomcat will open my index.html file but it will not access the controllers (gives me a 404 for /myProject/servlet/myServlet). So I created the above web.xml file and added it to the %CATALINA_HOME%\webapps\myProject\WEB-INF directory. I changed the necessary values to contain my servlet's name and the default welcome-page. Now Tomcat does not recognize or even (it gives me a 404 on both).


My directory structure for the %CATALINA_HOME%\webapps\myProject is as follows:

\myProject -- contains .html,.jsp,.css
\myProject\WEB-INF\ -- contains \classes and web.xml
\myProject\WEB-INF\classes
-- contains \controllers, \dao, \utils
-- the respective subdirectories all contain the appropriate class files which have been defined in a package structure which is reflected by the directory structure.

I cannot think of anything else that I am doing that would possibly cause such a problem.
 
I should make mention that it is only the controller servlets that are the problem. I have a jsp which accesses one of the \myProject\WEB-INF\classes\dao files and it works fine. So I have java and database connectivity, but I am unable to call the \myProject\WEB-INF\classes\controllers servlets.
 
After many arduous hours, I discovered that there was a typo in my web.xml file. The servlet-mapping tag had been improperly terminated and was throwing an error. After fixing the problem, all is working well.

Many thanks for your assistance.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top