aspose file tools*
The moose likes Tomcat and the fly likes problem with first servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Products » Tomcat
Bookmark "problem with first servlet" Watch "problem with first servlet" New topic
Author

problem with first servlet

trupti nigam
Ranch Hand

Joined: Jun 21, 2001
Posts: 613
Hello,
I am using Tomcat4.1. I was able to start the server sucsessfully and the default page too got displayed.
But when i wrote my first servlet HelloWorld.java & compiled it, put the class file under install-dir/webApps/examples/WEB-INF/classes directory and tried to access it through a browser(IE) It shows me the error 404.
Also while accessing the servlet do we have to always specify the port no 8080 i mean..
http://localhost:8080/servlet/HelloWorld??
thanks
Trupti
Tim Berett
Ranch Hand

Joined: Jun 14, 2002
Posts: 35
Place your java and class files in root to run
Yes, you must always specify the port no when accessing tomcat, unless you configure it as standard port 80.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15662
    
  15

404 means "Page not found". So your servlet URL in the web.xml file isn't mapping properly. CHeck the Tomcat logs and you may find out where it's looking when you want it to be looking somewhere else. Don't forget - EVEN UNDER WINDOWS the classname must be precisely capitalized, otherwise the Java classloader won't match, since it does case-senstive lookups. Windows is case-insenstitive, but Java is not!
As far as ports go: If you do not explicitly specify a port in a url, port 80 is assumed by the browser. Thus, http://www.javaranch.com and http://www.javaranch.com:80 are identical.
EVERY http request must specify the port, since there is no such thing as a session in a browser - sessions are artifacts created by servers. Thus, there is no ongoing client-side context to remember the port. Cookies don't count - the browser merely remembers them, it doesn't act directly on their contents.
Also, it is perfectly permissible (and often very useful) to have a server serving requests from more than one port. You can see this very dramatically in a situation where the same machine is using apache on port 80 and Tomcat on port 8080.
The one exception here is if you use a relative URL on a web page.


Customer surveys are for companies who didn't pay proper attention to begin with.
trupti nigam
Ranch Hand

Joined: Jun 21, 2001
Posts: 613
I am following the instuctions given in the marty halls book..according to which i have to put the class file in the
install-dir\webapps\examples\WEB-INF\classes
directory.
I have a different directory "myServlets" where i put the .java files.
I have put the url mapping and thus have registered my servlet in the web.xml which is under the install-dir\webapps\examples\WEB-INF directory.
please let me know what is wrong..
thanks
Trupti
Louise Haydu
Ranch Hand

Joined: Aug 17, 2000
Posts: 51
Did you shut down and restart the server after you put your servlet class in the classes directory?
trupti nigam
Ranch Hand

Joined: Jun 21, 2001
Posts: 613
I have created a separate directory named "myServlets" under install-dir/webapps/examples/web-inf/classess dirctory and have stored the .java & .class files in it.
in the web.xml file i have registered the servlet in the following manner.
<servlet>
<servlet-name>HelloWorld</servlet-name>
<servlet-class>myServlets.HelloWorld</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorld</servlet-name>
<url-pattern>/HelloWorld</url-pattern>
</servlet-mapping>
now i am getting th following error..

HTTP Status 500 - No Context configured to process this request
--------------------------------------------------------------------------------
type Status report
message No Context configured to process this request
description The server encountered an internal error (No Context configured to process this request) that prevented it from fulfilling this request.
please let me know what i am doing wrong..
thanks in advance.
trupti
Rajeev Nair
Ranch Hand

Joined: Mar 11, 2002
Posts: 51
trupti,
Just like you I just followed what Marty Hall's book said and copied the example HelloServlet and it worked fine for me.
At begining i had compilation problem because i had put the classpath in autoexec.bat file and somehow it didnt recognize. Then i set it up on the dos prompt and it worked.
As told by book, I created a directory
c:\servletdevel and had my servlet HelloServlet under it.
I had set the classpath like
set CLASSPATH=%CLASSPATH%;c:\servletdevel;c:\tomcat\common\lib\servlet.jar.
I compiled the program and copied the .class onto
install_dir\webapps\root\WEB-INF\classes from
c:\servletdevel\
On IE browser i gave the url as
http:\\localhost\servlet\HelloServlet and it worked. I have changed the 8080 port to 80.
I dont know whether this will help you,but still...
Thanks


Raj<br />Sun Certified Java Programmer
trupti nigam
Ranch Hand

Joined: Jun 21, 2001
Posts: 613
hi rajeev,
that means you didn't register your servlet in the web.xml??
also under install-dir/webapps/WEB-INF directory there is no class directory by default. that means we have to create it and hen put the .class file in it?
thanks
trupti
Rajeev Nair
Ranch Hand

Joined: Mar 11, 2002
Posts: 51
Trupti,
I didnt register in web.xml. Yes we need to create a directory classes under WEB-INF and put the class file there. It worked for me and i was able to run the rest of the examples without any problem.
trupti nigam
Ranch Hand

Joined: Jun 21, 2001
Posts: 613
thanks everyone for answering my queries..
the problem got solved ..i had set the classpath wrong..i mean while setting the path for servlet.jar i missed the c:
thanks
Trupti
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

Hi
Just for the record!
If you put your class files in 'install-dir\webapps\examples\WEB-INF\classes' then the URL will be: 'http://localhost:8080/examples/servlet/HelloWorld'
and if you put them in 'install-dir\webapps\WEB-INF\classes' the URL will be 'http://localhost:8080/servlet/HelloWorld'
/Rene


Regards, Rene Larsen
Dropbox Invite
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem with first servlet
 
Similar Threads
SERVLET IS NOT RUNNING PLZ HELP ME
how can I post my work online?
tomcat
Tomcat 404 Error - Still not working
tomcat- set JAVA_HOME point to jdk..