Link to home
Start Free TrialLog in
Avatar of stefanaichholzer
stefanaichholzerFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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

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

;)
Avatar of kiranhk
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
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


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
Avatar of stefanaichholzer

ASKER

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

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

;)
ASKER CERTIFIED SOLUTION
Avatar of kiranhk
kiranhk

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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...

;)
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........ :)
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...

:)
you r always Welcome :)