• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1652
  • Last Modified:

changing default directory for servlets in tomcat

Hi guys

I have a huge problem getting tomcat to serve servlets from another directory than the default.

Tomcat 5.5.17, apache 2.0.54 using mod_proxy

Currently it serves .jsp from the directory /home/lennart/www/blog/test without any problems
What I am trying to accomplish is to serve a servlet from /home/lennart/www/blog/test/WEB-INF/classes/HelloWorldExample

the testpage can be reached at megahooked.com/test/test.jsp

Code from the page <jsp:include page="HelloWorldExample" flush="true" />

One interesting thing is that if I uncommect the web.xml part below it will stot serving jsp files.

Relevant entries in the config files
httpd.conf
<VirtualHost *:80>
    ServerAdmin webmaster@dummy-host.example.com
    ServerName megahooked.com
    DocumentRoot /home/lennart/www/blog
    ErrorLog /home/lennart/www/error.log

#Reverse Proxy

ProxyRequests Off

<Proxy *>
Order deny,allow
Allow from all
</Proxy>

ProxyPass /jsp-examples http://localhost:8081/jsp-examples
ProxyPassReverse /jsp-examples http://localhost:8081/jsp-examples

ProxyPass /test http://localhost:8081/test
ProxyPassReverse /test http://localhost:8081/test

server.xml
<Connector port="8081"
            proxyName="megahooked.com"
            proxyPort="80" />

    <!--
    <Context path="/test" docBase="" debug="0"
            reloadable="true" />
    -->

web.xml
<!--
   <servlet>
        <servlet-name>HelloWorldExample</servlet-name>
        <servlet-class>HelloWorldExample</servlet-class>
         <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>HelloWorldExample</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    -->

Thanks
Joachim

0
joachimc
Asked:
joachimc
1 Solution
 
fargoCommented:
it is recommended to keep the servlet or any other java classes in under WEB-INF/classes with packages.

For ex: the class HelloWorldExample should go under some package say for ex: com.company.servlets.HelloWorldExample and do adjust the servlet-class in the web.xml with packages.

do check the url pattern..it should have the same name for your case.
<servlet-name>HelloWorldExample</servlet-name>
<url-pattern>/HelloWorldExample</url-pattern>


restart the tomcat and things should work.
0
 
sleep_furiouslyCommented:
First of all, the reason JSPs stop working when you uncomment the section of your application's web.xml is you have overriden the default servlet and sent everything to HelloWorldExample:

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

Which is saying for every request location beginning with "/" (in other words, all of them) send them to HelloWorldExample servlet.

What you are overriding is some settings in $TOMCAT/conf/web.xml:

    <!-- The mapping for the default servlet -->
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>/</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>

So, looking at your proxy config and jsp include, it looks like what you want in your application's web XML is probably something like:

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

Second, can you clarify "to serve a servlet from /home/lennart/www/blog/test/WEB-INF/classes/HelloWorldExample"

Is that last path segment a directory name?  In other words, do you have a class file like:
  /home/lennart/www/blog/test/WEB-INF/classes/HelloWorldExample/HelloWorldExample.class  ?

If so, then your servlet-class should reflect that.  In other words:

   <servlet>
        <servlet-name>HelloWorldExample</servlet-name>
        <servlet-class>HelloWorldExample.HelloWorldExample</servlet-class>
         <load-on-startup>1</load-on-startup>
    </servlet>




0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now