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
LVL 10
jaggernatAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

kiranhkCommented:
0
EmildaCommented:
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.
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

jaggernatAuthor Commented:
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

0
EmildaCommented:
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
0
jaggernatAuthor Commented:
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
0
EmildaCommented:
Yes, you need to set CATALINA_HOME, JAVA_HOME and PATH in the environment variable.
0
jaggernatAuthor Commented:
ok ..JAVA_HOME and PATH environment variables are set..  How do i set CATALINE_HOME variable ?

thanks,
J

0
EmildaCommented:
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.

0
jaggernatAuthor Commented:
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 :)


0
EmildaCommented:
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
=========================================================
0
jaggernatAuthor Commented:
can you paste catalina.bat too. it may have been from that file.

thanks,
J
0
EmildaCommented:
@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
0
jaggernatAuthor Commented:
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
0
kiranhkCommented:
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
0
jaggernatAuthor Commented:
it  still   says "catalina_home " variable is not defined correctly .


thanks,
J

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

0
CEHJCommented:
>>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"


0
jaggernatAuthor Commented:
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



0
kiranhkCommented:
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
0
CEHJCommented:
Personally i'd set those environment variables through Windows itself. Go

Start|Help

and look up 'environment variable' for your version of Windows
0
jaggernatAuthor Commented:
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


0
kiranhkCommented:
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
0
jaggernatAuthor Commented:
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

 
0
kiranhkCommented:
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 -->
0
jaggernatAuthor Commented:
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


0
kiranhkCommented:
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
0
jaggernatAuthor Commented:
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


0
kiranhkCommented:
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
0
jaggernatAuthor Commented:
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
0
kiranhkCommented:
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.
0
kiranhkCommented:
ok, atleast can you see and type the lines which i am telling
0
jaggernatAuthor Commented:
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 ...



0
kiranhkCommented:
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!!!!!!!!!!!!!!!!!!!!!!!!!
0
jaggernatAuthor Commented:
excellent ..thanks...
0
jaggernatAuthor Commented:
one last question...
where do i save the java servelt files ....and where should i run javac from....

thanks,
J
0
kiranhkCommented:
Please close the question in the Java Programming Topic area

Do a search for Javac.exe

If I do a search on MY COMPUTER I find this
C:\jdk1.3.1_02\bin\javac.exe

so I could
a) type C:\jdk1.3.1_02\bin\javac.exe to run javac.exe
or
b)set path=%path%;C:\jdk1.3.1_02\bin
then I can type javac in any directory C:\mycomputer c:\winnt etc.

as far as
set classpath=%classpath%;
Do a search for *.jar, whatever dir comes up, add to this variable as well.

Then run javac in whichever dir you .java files reside.

This can be done also by clicking START>CONTROL PANEL>SYSTEM>ADVANCED>ENV VARIABLE. This only needs to be done once here.


http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_21179277.html

http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_21177639.html
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jaggernatAuthor Commented:
thanks kiranhk.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java EE

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.