• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem starting servlet

 
andree surya
Greenhorn
Posts: 29
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64196
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
andree surya
Greenhorn
Posts: 29
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 261
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 29
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 29
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 29
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 261
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you have the servlet mapped or not?
 
andree surya
Greenhorn
Posts: 29
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13048
6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic