Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Correct Servlet Address Returns 404.

 
Gene Song
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can access the servlet through <jsp:include> tag. However, when I create a link to the same servlet using <a> tag or <form action="">, it returns 404 error.
The both ways work perfectly fine on my development computer and its Tomcat, but ocne it's deployed to a web server, it starts happening.

I have done following things:
1. Since the Tomcat version is 5.5 in the deployment server and the Tomcat version is 7.0 in my development server, I tried to lower my servlet API version to 2.4. It didn't fix the problem.
2. Even though my servlet only calls the methods on my model class and does not generate any output, I tried to set content type to html/text. It didn't fix the problem.

What could be the reason?
Please advice.
Thanks in advance.

EDIT: Grammar.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64990
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It means you need to read the ServletsFaq!

This situation is covered there. (Hint: context path, cough, cough).
 
Gene Song
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the response. I went through the FAQ.

Make sure that the URL that you are using starts with the context path of the web application, which should be followed by the servlet mapping. For example: "/context-path/servlet-path". Using page-relative addressing in an anchor tag, or as a form action, is almost guaranteed to cause issues. Even if you can get it to work, it is fragile and can break with the least little of changes.


Does this mean I cannot deploy the application on the root of my website?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64990
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Only if you deploy it as the ROOT application, in which case the context path is the empty string. See the Tomcat docs.

P.S. You should never hard-code the context path in a page, even if it's the root app. Always use ${pageContext.request.contextPath}. It will always resolve to the right thing.
 
Gene Song
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It was a great help. Thanks alot.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic