wood burning stoves*
The moose likes Servlets and the fly likes Problem starting servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Problem starting servlet" Watch "Problem starting servlet" New topic
Author

Problem starting servlet

andree surya
Greenhorn

Joined: Jun 13, 2009
Posts: 29
Hi all, I'm entirely new to servlet and I'm having a problem here.

I installed tomcat as my web container and everything works fine, my JSP file can be parsed successfully.
And then I try to test the servlet. I've set up the classpath, compile my HelloServlet.java, put it into webapps/lab/WEB-INF/classes/.
When I browse the URL http://localhost:8080/lab/HelloServlet, it gives me an 404 error. The requested resource (/lab/HelloServlet) is not available, it says.

Can anyone tell me what am I missing here?

Thanks,
Ang
dhiraj sardana
Greenhorn

Joined: Jan 21, 2009
Posts: 5
You need to add mapping of your servlet to web.xml file as well like
<servlet>
<servlet-name>test</servlet-name>
<servlet-class>com.nagarro.com.TestServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>test</servlet-name>
<url-pattern>/test</url-pattern>
</servlet-mapping>
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60055
    
  65

andree surya wrote:And then I try to test the servlet. I've set up the classpath

Tomcat completely ignores the classpath.

Did you place your servlet in a package other than the default? That might not be the only issue, but after setting up the mapping, it's the first thing to get out of the way.

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
andree surya
Greenhorn

Joined: Jun 13, 2009
Posts: 29
Hi all, thanks for the reply.

The web.xml is not working. I have to put it under the WEB-INF directory, right?

My directory structure is like this:



My HelloServlet.java :


By the way, I'm using ubuntu linux. The servlet jar library is put under /usr/share/java. Does my tomcat server need to know about this?
Anirvan Majumdar
Ranch Hand

Joined: Feb 22, 2005
Posts: 261
Like Bear mentioned, Tomcat doesn't need to depend on any JARs or classes other than those provided by the JDK installation. And this behaviour is independent of the OS you're using.
Your directory structure looks fine, but can you confirm that the directory structure under Tomcat's deployment folder [webapps] is the same? Particularly, can you verify whether the HelloServlet.class file is present at the expected location in the deployment folder.
andree surya
Greenhorn

Joined: Jun 13, 2009
Posts: 29
But the built-in hello world example works.
The example is structured like this:

The servlets-example.xml looks like this:

Do i need this?
andree surya
Greenhorn

Joined: Jun 13, 2009
Posts: 29
Anirvan Majumdar wrote:Like Bear mentioned, Tomcat doesn't need to depend on any JARs or classes other than those provided by the JDK installation. And this behaviour is independent of the OS you're using.
Your directory structure looks fine, but can you confirm that the directory structure under Tomcat's deployment folder [webapps] is the same? Particularly, can you verify whether the HelloServlet.class file is present at the expected location in the deployment folder.


I intend to use tomcat's deployment folder as my development folder (just for testing). Under the webapps folder, I only have the lab subfolder. So the HelloServlet.class is located at webapps/lab/WEB-INF/classes/
andree surya
Greenhorn

Joined: Jun 13, 2009
Posts: 29
Anirvan Majumdar wrote:Like Bear mentioned, Tomcat doesn't need to depend on any JARs or classes other than those provided by the JDK installation. And this behaviour is independent of the OS you're using.
Your directory structure looks fine, but can you confirm that the directory structure under Tomcat's deployment folder [webapps] is the same? Particularly, can you verify whether the HelloServlet.class file is present at the expected location in the deployment folder.


I intend to use tomcat's deployment folder as my development folder (just for testing). Under the webapps folder, I only have the lab subfolder. So the HelloServlet.class is located at webapps/lab/WEB-INF/classes/
Anirvan Majumdar
Ranch Hand

Joined: Feb 22, 2005
Posts: 261
Woah!
I understand that there are no strong directives instructing you NOT to use Tomcat's deployment folder as your development folder, but if you do so, errors like you're facing are just the tip of the problem-iceberg!
While I can't clearly pin-point why Tomcat expects a web archive to be deployed and not real code [even though the resources are structured similarly], I've got a strong feeling it's not correct. If you Google around a bit, I'm sure you'll know of the precise reason[s].
For starters, I'd suggest you move your development code to a different location. Then when you need to test it on the server, build a WAR and place it in Tomcat's webapp folder. Thereafter, if you still encounter a 404 error, we can work to discover the core issue. But I've got a feeling that things should function properly then.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Do you have the servlet mapped or not?
andree surya
Greenhorn

Joined: Jun 13, 2009
Posts: 29
Hey, it's working now but I'm not sure what do I did to fix this. Maybe because I'm restarting the tomcat server.
Vinnibabu singu
Ranch Hand

Joined: Aug 19, 2009
Posts: 30
I always face the same problem.
The Tomcat server just displays the error some time and sometime after that may be when i restart my system once again it works fine
If one application works other application which is almost similar is displayed as error :404 .
I couldnt get a precise reason why it is doin so.
can anyone suggest what standerd procedure is to follow for avoiding that error?
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12678
    
    5


ALL classes used in servlets MUST be in packages - the reason being that if no package is defined, the JVM looks in the "current" directory which you have no control over.

ALL class files MUST be in a directory structure which reflects the package structure - thats the only way the JVM can find them.

Bill


Java Resources at www.wbrogden.com
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

There's no possible way to tell you how to avoid the error if we have no idea what's causing it in the first place.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem starting servlet
 
Similar Threads
Tomcat 4.0.1 - Servlet not found
running a servlet from a new web application
Apache/Tomcat servlet problem
deploying prb ..
Error 404- not found