Solved

Can't get my servlets to run and PHP on Tomcat

Posted on 2004-10-22
338 Views
Last Modified: 2013-11-24
I downloaded and installed Apache Tomcat Server 5.0.28 and all is working fine, the JSP and servlet examples that came with the server work fine as well. I've writen a small example servlet and I can't the the damn thing to run, I don't know where to place it and what must be changed in which .xml file. I did follow all the documentation on how to do it but I guess that was writen by a non-user-friendly person because I can't follow most of it...

I´ve also heard that Tomcat can do PHP ( http://wiki.apache.org/jakarta-tomcat/UsingPhp ) but those it seems to me those instructions are for Linux and I can't really figure out how to get it done in Windows...

I hope to get some input and a nice example would be much appreciated...

Thanx

;)
0
Question by:stefanaichholzer
    14 Comments
     
    LVL 8

    Expert Comment

    by:kiranhk
    0
     
    LVL 8

    Expert Comment

    by:kiranhk
    you need to place your compiled java class file in the
    c:\jakarta-tomcat-5.5.2\webapps\servlets-examples\WEB-INF\classes  folder (assuming you have ur tomcat installed in c drive) Then u need to change the web.xml located in the WEB-INF folder in the same path just above the classes folder. then u need to restart the tomcat and access by the URL. the above link which i have given will give u a fair idea
    0
     
    LVL 13

    Expert Comment

    by:petmagdy
    dear sir their are two approches to deploy ur servlets, either ur create ur own web module and deploy it or add ur servlet to an existing deployed module over tomcat

    if u will do this as a separate web module do the following assuming that ur servlet is (mypackage.myservlet),

    1- create a web.xml file contains the following:

    <?xml version = '1.0' encoding = 'windows-1252'?>
    <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
    <web-app>
      <description>Empty web.xml file for Web Application</description>
      <servlet>
        <servlet-name>myservlet</servlet-name>
        <servlet-class>mypackage.myservlet</servlet-class>
      </servlet>
      <servlet-mapping>
          <servlet-name>myservlet</servlet-name>
          <url-pattern>/myservlet</url-pattern>
      </servlet-mapping>
      <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>index.html</welcome-file>
      </welcome-file-list>
    </web-app>

    3- compile ur servlet
    2- using a jar or even zip utitly create a directory tree of ur module as following:

    root
    |----->WEB-INF/web.xml
    |----->WEB-INF/classes/mypackage/myservlet.class

    and name it for example myweb.war

    4- using tomcat admin deploy ur war and specify a context root like myweb

    5 - call http://host:port/myweb/myservlet


    0
     
    LVL 13

    Expert Comment

    by:petmagdy
    in case u want to deploy on an existing Web module do this:

    1- compile ur servlet and put it as following
    {tomcat-home}\webapps\servlets-examples\WEB-INF\classes\ mypackage/myservlet.class
     
    open web.xml in {tomcat-home}\webapps\servlets-examples\WEB-INF

    and add this section:

      <servlet>
        <servlet-name>myservlet</servlet-name>
        <servlet-class>mypackage.myservlet</servlet-class>
      </servlet>
      <servlet-mapping>
         <servlet-name>myservlet</servlet-name>
         <url-pattern>/myservlet</url-pattern>
      </servlet-mapping>

    3- restart tomcat and try http://host:port/{examples-context-root}/myservlet

    good luck
    0
     
    LVL 9

    Author Comment

    by:stefanaichholzer
    petmagdy,

    I´ll try your suggestions, but tell me one more thing, what if I don't want to place my .class file in the /servlet-examples/ folder in Tomcat?, can't I just place my .class files in the ROOT folder or create a new folder for my servlets??

    Thank you for your time, much appreciated...

    ;)
    0
     
    LVL 13

    Expert Comment

    by:petmagdy
    No because assembling applications in J2EE is due to specific standards, but if u r going to create ur own web module tomcat will deploy ur classes in a separate directory, please also that if u packaged ur code (mycode.jar) into a jar file u can place it into a specific place for jar libraries that is:

    {tomcat-home}\webapps\servlets-examples\WEB-INF\lib\mycode.jar

     that will do it also
    0
     
    LVL 9

    Author Comment

    by:stefanaichholzer
    petmagdy,

    I didn't quite follow your last post but I'll try to get it all done in the /servlet-examples/ folder.

    Now my point is; the name says it´s an example folder ( /servlet-examples/ ), so there must be a way to create a folder, say /servlets/ and have all the servlets in there, right? and just configure Tomcat to run the servlets from that folder...

    I hate it when things are so complicated as this...

    Could you answer the second part of my question about Tomcat doing PHP?

    Thanx

    ;)
    0
     
    LVL 8

    Accepted Solution

    by:
    0
     
    LVL 8

    Expert Comment

    by:kiranhk
    0
     
    LVL 8

    Expert Comment

    by:kiranhk
    0
     
    LVL 9

    Author Comment

    by:stefanaichholzer
    petmagdy,

    This is what I mean:

    In its default configuration, Tomcat expects you to place compiled Servlets in the webapps\ROOT\WEB-INF\classes subdirectory of the Tomcat installation directory to deploy them. Place your compiled MyServlet.class file in that directory, then (assuming Tomcat is running on your local computer) load http://localhost:8080/servlet/MyServlet.

    There I don't need to place my .class files into the /servlet-examples/ folder, just what I want, still I don't know if I will get the baby to run...

    Read on: http://www.sitepoint.com/article/java-servlets-1/4

    kiranhk,

    Thanx for the valuable links...

    ;)
    0
     
    LVL 8

    Expert Comment

    by:kiranhk
    yes, you can place your class file in webapps\ROOT\WEB-INF\classes . since ROOT is also a web application.
    If you have gone thro' the article fully they also specify why u need to add an entry in the web.xml.
    If you dont add an entry in the web.xml you will have to access ur servlet using the class name of ur servlet. pl go thro' the article fully and u will be able to get ur baby to run..... and  i mean run real fast........ :)
    0
     
    LVL 9

    Author Comment

    by:stefanaichholzer
    kiranhk,

    I went through the article and the thing is now running, was actually pretty simple once I knew what to do...

    Thank you for your time, you have been so much more the kind...

    :)
    0
     
    LVL 8

    Expert Comment

    by:kiranhk
    you r always Welcome :)
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Suggested Solutions

    For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
    Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
    Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
    This video teaches viewers about errors in exception handling.

    884 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now