This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Servlets and the fly likes Correct Servlet Address Returns 404. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Correct Servlet Address Returns 404. " Watch "Correct Servlet Address Returns 404. " New topic
Author

Correct Servlet Address Returns 404.

Gene Song
Greenhorn

Joined: Dec 08, 2012
Posts: 4
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.


Obessed with computers, mobile devices, linux, coding, scuba diving, boxing and coffee...
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60822
    
  65

It means you need to read the ServletsFaq!

This situation is covered there. (Hint: context path, cough, cough).


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Gene Song
Greenhorn

Joined: Dec 08, 2012
Posts: 4
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

Joined: Jan 10, 2002
Posts: 60822
    
  65

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

Joined: Dec 08, 2012
Posts: 4
It was a great help. Thanks alot.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Correct Servlet Address Returns 404.
 
Similar Threads
Tomcat Connector - high cpu usage
The requested resource (/servletTestApp/) is not available
Configuring tomcat in myeclipse Probelm
server not mapping JSF url-pattern
Where does stdout.log come from?