Link to home
Start Free TrialLog in
Avatar of jaggernat
jaggernat

asked on

servlet programmer beginner

hi ,

I am new to servlet programming. I have downloaded latest versions of Apache and Tomcat on my Windows NT machine.

I have written my first servlet program. Now i am confused where to save it. Do i compile it using javac "filename" and run it using java "filename" (like we do for usal java programs)

Any advice greatly appreciated.

This is my simple servlet program :

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class Servlet1 extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponce responce) throws ServletException, IOException
{
PrintWriter out = responce.getWriter();
out.println("Helloooooooooo World");
}
}

thanks,
J
Avatar of kiranhk
kiranhk

http://jakarta.apache.org/tomcat/tomcat-4.0-doc/appdev/deployment.html

The above link gives you information on how to deploy your application into Tomcat.

You need to compile above program using javac and put your class file under the folder /WEB-INF/classes/ .

you can use the existing directory $CATALINA_HOME/webapps/ROOT/WEB-INF/classes to put your servlet classes. In this case you can access your servlet by invoking http://hostname:8080/servlet/Servlet1 

Good practise is to make a dirctory under webapps and put all your servlets under WEB-INF/classes underneath that directory
eg : $CATALINA_HOME/webapps/myroot/WEB-INF/classes

In this case you can access your classes by : http://hostname:8080/myrrot/servlet/Servlet1 

Also read:  http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/Servlets.html

Hope this helps.
Avatar of jaggernat

ASKER

For some reason, i cant start the server .
 
Detailed Description:

My Tomcat is stored in this following directory:
c:\program files\Apache Group\Tomcat 4.1

Under tomcat 4.1  directory  , i made a directory  called  "ap1"(which i created to store applications) ,under "ap1" directory i created  "Web-inf" directory , and under "Web-inf directory" i created  the "classes" directory.

I have stored my first java program "servlet1.java" in the "classes" directory .

In the  environment variables , i have set some thing like this
set JAVA_HOME=c:\j2sdk1.4.2_06;  set CATALINA_HOME=c:\Program Files\Apache Group\Tomcat 4.1

when i run the command "startup" from c:\program files\Apache Group\Tomcat 4.1\bin
the server starts , but terminates in between and throws an error message (like file not found).

Also , i cant run the javac command from the command prompt.

...........This is getting real frustrating..

Any advice greatly appreciated.

thanks,
J

you need to create "api" directory under c:\program files\Apache Group\Tomcat 4.1\webapps and not under c:\program files\Apache Group\Tomcat 4.1  and WEB-INF is case sensitive.

You need to solve the problem with tomcat startup. Try to find more details of the error. Since you cannot even compile java program from command prompt, that could be the root of the problem. Try to figure out why you are not able to run javac. Try running "echo %JAVA_HOME%" to see if the env variable is set correctly. You need to set PATH variable as well.

set PATH = %PATH%;%JAVA_HOME%\bin
ok when i run "startup" it says "Catalina_Home is not defined correctly. This environment variable is needed to run this  program .

Do i need to define "CATALINA_HOME" in environment variable??

thanks,
J
Yes, you need to set CATALINA_HOME, JAVA_HOME and PATH in the environment variable.
ok ..JAVA_HOME and PATH environment variables are set..  How do i set CATALINE_HOME variable ?

thanks,
J

In the same way you can set CATALINE_HOME, give it the value :

Right click "My Computer" icon on the desktop and select "Properties" click "Advanced" tab and select "Environment Variables", click "new" button under user variables,
Give name as: CATALINE_HOME
value as: c:\Program Files\Apache Group\Tomcat 4.1

Don't set environment variable in the command prompt giving the set option, that's only temporary.

i dont know....i am still getting the same error.."Catalina_Home is not defined correctly"

couple of hours back i deleted one line of code from startup.bat or catalina.bat (dont remember exactly). I think thats the problem.

will i have to uninstall and reinstall Tomcat.

thanks,
sorry for the trouble.

this question s is definetely worth more than 150 points :)


Given below is the contents of startup.bat, may be from that you can figure out which line you deleted.
=======================================================
@echo off
if "%OS%" == "Windows_NT" setlocal
rem ---------------------------------------------------------------------------
rem Start script for the CATALINA Server
rem
rem $Id: startup.bat,v 1.4 2002/08/04 18:19:43 patrickl Exp $
rem ---------------------------------------------------------------------------

rem Guess CATALINA_HOME if not defined
if not "%CATALINA_HOME%" == "" goto gotHome
set CATALINA_HOME=.
if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHome
set CATALINA_HOME=..
:gotHome
if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHome
echo The CATALINA_HOME environment variable is not defined correctly
echo This environment variable is needed to run this program
goto end
:okHome

set EXECUTABLE=%CATALINA_HOME%\bin\catalina.bat

rem Check that target executable exists
if exist "%EXECUTABLE%" goto okExec
echo Cannot find %EXECUTABLE%
echo This file is needed to run this program
goto end
:okExec

rem Get remaining unshifted command line arguments and save them in the
set CMD_LINE_ARGS=
:setArgs
if ""%1""=="""" goto doneSetArgs
set CMD_LINE_ARGS=%CMD_LINE_ARGS% %1
shift
goto setArgs
:doneSetArgs

call "%EXECUTABLE%" start %CMD_LINE_ARGS%

:end
=========================================================
can you paste catalina.bat too. it may have been from that file.

thanks,
J
@echo off
if "%OS%" == "Windows_NT" setlocal
rem ---------------------------------------------------------------------------
rem Start/Stop Script for the CATALINA Server
rem
rem Environment Variable Prequisites
rem
rem   CATALINA_HOME   May point at your Catalina "build" directory.
rem
rem   CATALINA_BASE   (Optional) Base directory for resolving dynamic portions
rem                   of a Catalina installation.  If not present, resolves to
rem                   the same directory that CATALINA_HOME points to.
rem
rem   CATALINA_OPTS   (Optional) Java runtime options used when the "start",
rem                   "stop", or "run" command is executed.
rem
rem   CATALINA_TMPDIR (Optional) Directory path location of temporary directory
rem                   the JVM should use (java.io.tmpdir).  Defaults to
rem                   %CATALINA_BASE%\temp.
rem
rem   JAVA_HOME       Must point at your Java Development Kit installation.
rem
rem   JAVA_OPTS       (Optional) Java runtime options used when the "start",
rem                   "stop", or "run" command is executed.
rem
rem   JSSE_HOME       (Optional) May point at your Java Secure Sockets Extension
rem                   (JSSE) installation, whose JAR files will be added to the
rem                   system class path used to start Tomcat.
rem
rem   JPDA_TRANSPORT  (Optional) JPDA transport used when the "jpda start"
rem                   command is executed. The default is "dt_shmem".
rem
rem   JPDA_ADDRESS    (Optional) Java runtime options used when the "jpda start"
rem                   command is executed. The default is "jdbconn".
rem
rem $Id: catalina.bat,v 1.7 2003/11/05 11:40:18 remm Exp $
rem ---------------------------------------------------------------------------

rem Guess CATALINA_HOME if not defined
if not "%CATALINA_HOME%" == "" goto gotHome
set CATALINA_HOME=.
if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHome
set CATALINA_HOME=..
:gotHome
if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHome
echo The CATALINA_HOME environment variable is not defined correctly
echo This environment variable is needed to run this program
goto end
:okHome

rem Get standard environment variables
if exist "%CATALINA_HOME%\bin\setenv.bat" call "%CATALINA_HOME%\bin\setenv.bat"

rem Get standard Java environment variables
if exist "%CATALINA_HOME%\bin\setclasspath.bat" goto okSetclasspath
echo Cannot find %CATALINA_HOME%\bin\setclasspath.bat
echo This file is needed to run this program
goto end
:okSetclasspath
set BASEDIR=%CATALINA_HOME%
call "%CATALINA_HOME%\bin\setclasspath.bat"

rem Add on extra jar files to CLASSPATH
if "%JSSE_HOME%" == "" goto noJsse
set CLASSPATH=%CLASSPATH%;%JSSE_HOME%\lib\jcert.jar;%JSSE_HOME%\lib\jnet.jar;%JSSE_HOME%\lib\jsse.jar
:noJsse
set CLASSPATH=%CLASSPATH%;%CATALINA_HOME%\bin\bootstrap.jar

if not "%CATALINA_BASE%" == "" goto gotBase
set CATALINA_BASE=%CATALINA_HOME%
:gotBase

if not "%CATALINA_TMPDIR%" == "" goto gotTmpdir
set CATALINA_TMPDIR=%CATALINA_BASE%\temp
:gotTmpdir

rem ----- Execute The Requested Command ---------------------------------------

echo Using CATALINA_BASE:   %CATALINA_BASE%
echo Using CATALINA_HOME:   %CATALINA_HOME%
echo Using CATALINA_TMPDIR: %CATALINA_TMPDIR%
echo Using JAVA_HOME:       %JAVA_HOME%

set _EXECJAVA=%_RUNJAVA%
set MAINCLASS=org.apache.catalina.startup.Bootstrap
set ACTION=start
set SECURITY_POLICY_FILE=
set DEBUG_OPTS=
set JPDA=

if not ""%1"" == ""jpda"" goto noJpda
set JPDA=jpda
if not "%JPDA_TRANSPORT%" == "" goto gotJpdaTransport
set JPDA_TRANSPORT=dt_shmem
:gotJpdaTransport
if not "%JPDA_ADDRESS%" == "" goto gotJpdaAddress
set JPDA_ADDRESS=jdbconn
:gotJpdaAddress
shift
:noJpda

if ""%1"" == ""debug"" goto doDebug
if ""%1"" == ""run"" goto doRun
if ""%1"" == ""start"" goto doStart
if ""%1"" == ""stop"" goto doStop

echo Usage:  catalina ( commands ... )
echo commands:
echo   debug             Start Catalina in a debugger
echo   debug -security   Debug Catalina with a security manager
echo   jpda start        Start Catalina under JPDA debugger
echo   run               Start Catalina in the current window
echo   run -security     Start in the current window with security manager
echo   start             Start Catalina in a separate window
echo   start -security   Start in a separate window with security manager
echo   stop              Stop Catalina
goto end

:doDebug
shift
set _EXECJAVA=%_RUNJDB%
set DEBUG_OPTS=-sourcepath "%CATALINA_HOME%\..\..\jakarta-tomcat-catalina\catalina\src\share"
if not ""%1"" == ""-security"" goto execCmd
shift
echo Using Security Manager
set SECURITY_POLICY_FILE=%CATALINA_BASE%\conf\catalina.policy
goto execCmd

:doRun
shift
if not ""%1"" == ""-security"" goto execCmd
shift
echo Using Security Manager
set SECURITY_POLICY_FILE=%CATALINA_BASE%\conf\catalina.policy
goto execCmd

:doStart
shift
if not "%OS%" == "Windows_NT" goto noTitle
set _EXECJAVA=start "Tomcat" %_RUNJAVA%
goto gotTitle
:noTitle
set _EXECJAVA=start %_RUNJAVA%
:gotTitle
if not ""%1"" == ""-security"" goto execCmd
shift
echo Using Security Manager
set SECURITY_POLICY_FILE=%CATALINA_BASE%\conf\catalina.policy
goto execCmd

:doStop
shift
set ACTION=stop
goto execCmd

:execCmd
rem Get remaining unshifted command line arguments and save them in the
set CMD_LINE_ARGS=
:setArgs
if ""%1""=="""" goto doneSetArgs
set CMD_LINE_ARGS=%CMD_LINE_ARGS% %1
shift
goto setArgs
:doneSetArgs

rem Execute Java with the applicable properties
if not "%JPDA%" == "" goto doJpda
if not "%SECURITY_POLICY_FILE%" == "" goto doSecurity
%_EXECJAVA% %JAVA_OPTS% %CATALINA_OPTS% %DEBUG_OPTS% -Djava.endorsed.dirs="%JAVA_ENDORSED_DIRS%" -classpath "%CLASSPATH%" -Dcatalina.base="%CATALINA_BASE%" -Dcatalina.home="%CATALINA_HOME%" -Djava.io.tmpdir="%CATALINA_TMPDIR%" %MAINCLASS% %CMD_LINE_ARGS% %ACTION%
goto end
:doSecurity
%_EXECJAVA% %JAVA_OPTS% %CATALINA_OPTS% %DEBUG_OPTS% -Djava.endorsed.dirs="%JAVA_ENDORSED_DIRS%" -classpath "%CLASSPATH%" -Djava.security.manager -Djava.security.policy=="%SECURITY_POLICY_FILE%" -Dcatalina.base="%CATALINA_BASE%" -Dcatalina.home="%CATALINA_HOME%" -Djava.io.tmpdir="%CATALINA_TMPDIR%" %MAINCLASS% %CMD_LINE_ARGS% %ACTION%
goto end
:doJpda
if not "%SECURITY_POLICY_FILE%" == "" goto doSecurityJpda
%_EXECJAVA% %JAVA_OPTS% %CATALINA_OPTS% -Xdebug -Xrunjdwp:transport=%JPDA_TRANSPORT%,address=%JPDA_ADDRESS%,server=y,suspend=n %DEBUG_OPTS% -Djava.endorsed.dirs="%JAVA_ENDORSED_DIRS%" -classpath "%CLASSPATH%" -Dcatalina.base="%CATALINA_BASE%" -Dcatalina.home="%CATALINA_HOME%" -Djava.io.tmpdir="%CATALINA_TMPDIR%" %MAINCLASS% %CMD_LINE_ARGS% %ACTION%
goto end
:doSecurityJpda
%_EXECJAVA% %JAVA_OPTS% %CATALINA_OPTS% -Xrunjdwp:transport=%JPDA_TRANSPORT%,address=%JPDA_ADDRESS%,server=y,suspend=n %DEBUG_OPTS% -Djava.endorsed.dirs="%JAVA_ENDORSED_DIRS%" -classpath "%CLASSPATH%" -Djava.security.manager -Djava.security.policy=="%SECURITY_POLICY_FILE%" -Dcatalina.base="%CATALINA_BASE%" -Dcatalina.home="%CATALINA_HOME%" -Djava.io.tmpdir="%CATALINA_TMPDIR%" %MAINCLASS% %CMD_LINE_ARGS% %ACTION%
goto end

:end
ok ..  here is the problem.

when the server starts , it pops up the error message ' Windows cannot find : "-Dsun.io.canoncaches=false" ' .

i searched for "-Dsun.io.canoncaches=false" , and found that it is a line of code in "catalina.bat".

These are all the environment variables i have :

variable                                           value

CATALINA_HOME                       set CATALINA_HOME=C:\Program Files\Apache Group\Tomcat 4.1\bin
JAVA_HOME                              set JAVA_HOME=C:\j2sdk1.4.2_06\bin
PATH                                        PATH=%PATH%c:\j2sdk1.4.2_06\bin;%CATALINA_HOME%\bin

could you please help me out..

thanks,
J
your environment variables should be

CATALINA_HOME                       set CATALINA_HOME=C:\Program Files\Apache Group\Tomcat 4.1
JAVA_HOME                              set JAVA_HOME=C:\j2sdk1.4.2_06

dont add the bin directory in your CATALINA_HOME and JAVA_HOME variables
it  still   says "catalina_home " variable is not defined correctly .


thanks,
J

try this
set CATALINA_HOME=C:\Progra~1\Apache Group\Tomcat 4.1
try this
set CATALINA_HOME=C:\Progra~1\Apache~1\Tomcat 4.1

Avatar of CEHJ
>>it  still   says "catalina_home " variable is not defined correctly .

It will, as it should not contain spaces. Use

set CATALINA_HOME="C:\Program Files\Apache Group\Tomcat 4.1"


no difference ,,doesnt work  

when set to :
set CATALINA_HOME=C:\Progra~1\Apache~1\Tomcat 4.1

it says --.>"catalina_home " variable is not defined correctly

and

when set to:
set CATALINA_HOME="C:\Program Files\Apache Group\Tomcat 4.1"

it says --> File\Apache  was unexpected at this time.


thanks,
J



Add these lines in your startup.bat after this line rem Guess CATALINA_HOME if not defined
set JAVA_HOME=C:\j2sdk1.4.2_06
set CATALINA_HOME=C:\Progra~1\Apache~1\Tomcat 4.1
Personally i'd set those environment variables through Windows itself. Go

Start|Help

and look up 'environment variable' for your version of Windows
ok ..i did the following steps :
<<<<<
Add these lines in your startup.bat after this line rem Guess CATALINA_HOME if not defined
set JAVA_HOME=C:\j2sdk1.4.2_06
set CATALINA_HOME=C:\Progra~1\Apache~1\Tomcat 4.1
>>>>>>

now i dont get any error messages ...Now it opens an other window with the label Tomcat and quickly dissapears...

looks like we are getting close..

thanks,
J


ok, good.
now go to startup.bat and change this line
call "%EXECUTABLE%" start %CMD_LINE_ARGS%

to
call "%EXECUTABLE%" run %CMD_LINE_ARGS%


then you can post whatever error you r getting here
ther are a whole bunch of errors ..it starts with

java.net.bind exception :address already in use JVM_BINDPOINT:80
at org.apache.tomcat.util.net.poolTCPendpoint.InitendPoint(poolTCPendpoint.java)

and goes on and on

 
that means your port 80 is already being used by a different address. you need to change this port number to different one or you need to shutdown the process which is using port 80 and then start your tomcat.

go to tomcat->conf folder and change the port number located in server.xml change the port number to 10110 or any number greater than 5000 and then restart your tomcat. otherwise you can post your server.xml here

    <!-- Define a non-SSL HTTP/1.1 Connector on port 8080 -->
    <Connector port="8080"
               maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
               enableLookups="false" redirectPort="8443" acceptCount="100"
               debug="0" connectionTimeout="20000"
               disableUploadTimeout="true" />
    <!-- Note : To disable connection timeouts, set connectionTimeout value
     to 0 -->
ok
Now, which port number should be changed with 10110.

 there is
 server port = 8005 shutdown=SHUTDOWN  debug=0

there is port 8009
there is port 8002 ..

ect..

thanks,
J


i have posted the lines where u need to change. dont change anything other than what i have posted in my previous comment. after you change the port while accessing the server you need to use http://localhost:10110

just go to the line  
   <!-- Define a non-SSL HTTP/1.1 Connector on port 8080 -->
<Connector port="8080"

in your server.xml and then change the port number as specified
ok ,,,,in my server.xml ,there is not port with the number "8080".. there is 8081, 8443, 8082,8083,,ect.

The only instance where 8080 is mentioned is this:
by default a non-ssl/1.1 connector is established on port 8080. You can also enable
non-ssl/1.1 connector on port 8443.........

thanks,
J


ok, fine. but do u have the lines which i am mentioning. just change whatever port number is there and restart.

just go to the line  
   <!-- Define a non-SSL HTTP/1.1 Connector on port 8080 -->
<Connector port="xxxxx"

in your server.xml and then change the port number as specified


or better still post your full server.xml here
ok ...is it possible for me to copy paste the server.xml code
I am looking at server.xml from the dos prompt->edit mode..
I guess copy -paste doesnt work..is there any other way..

thanks,
J
there is a menu option for copy. first select all the lines or the lines which i have mentioned and then go to Edit->Copy.
ok, atleast can you see and type the lines which i am telling
I DO NOT HAVE

 <!-- Define a non-SSL HTTP/1.1 Connector on port 8080 -->
<Connector port="xxxxx"

THERE IS

<define a non-ssl coyote HTTP/1.1 Connector on port 8081 >
connector class name = "org.apache.coyote.tomcat4.coyoteconnector" port ="80"
minprocessors=5...ect

and there is

 <!-- Define a non-SSL HTTP/1.1 Connector on port 8443 -->
<port="8443"

...everything but 8080 is missing ...



ok, good. now change the line
connector class name = "org.apache.coyote.tomcat4.coyoteconnector" port ="80"
to some other port number and start the tomcat. Access your server as i have told before using the port number which u give here

this should work now!!!!!!!!!!!!!!!!!!!!!!!!!
excellent ..thanks...
one last question...
where do i save the java servelt files ....and where should i run javac from....

thanks,
J
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
thanks kiranhk.