Win a copy of Modern JavaScript for the Impatient this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Ajax post cannot find servlet

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I used eclipse and developed a servlet application in which jQuery uses ajax and calls the doPost method of servlet by passing parameters.
The application was wokring perfectly in eclipse IDE on Apache Tomcat 7 server.
Now I need to run this on another server where I need to integrate this with other java files. So I copied all the file along with web.xml file and tried to run on the other server.
But now the ajax call is unable to reach the servlet doPost method and hence throwing 404 error.
Please anyone help me regarding this and let me know what is the reason for this problem.

Here is the html file that uses jQuery in Javascript file.



here is the Javascript file which uses ajax to call servlet doPost method.


the simpleServer is the context path of my servlet

here is my servlet code



and this is my web.xml file



Please help me regarding this.
Thank you very much.
 
Ranch Hand
Posts: 440
Hibernate Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you post the URLs here which are being hit by the ajax call ? Post both the URLs, from within Eclipse and also the one from the external server that you deployed the application to .
 
Anup Nadig Malathesha
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Saif : Thank you very much for your response. I am using same URL in both ajax calls. the servlet class name is simpleServer.java and its url mapping is simpleServer. Hence I am using simpleServer as url in both the ajax calls. Please let me know if you need any details.
 
Marshal
Posts: 67445
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just "simpleServer"? If so, it's wrong. That's a page-relative URL that is fragile even when you can get it to work.

All browser-initiated URLs in a web app should be server-relative, starting with the context path.
 
Anup Nadig Malathesha
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Bear :
Thanks Bear.
Are you saying that the required URL is this

http://omega.uta.edu/temp/simpleSerevr.java

.
This is the where that I kept my servlet and all other files in the server.
 
Bear Bibeault
Marshal
Posts: 67445
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, that's an absolute URL and should never be used for a resource in the same web app. I said:

All browser-initiated URLs in a web app should be server-relative, starting with the context path.

 
Anup Nadig Malathesha
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I need to use /temp/simpleServer right
 
Bear Bibeault
Marshal
Posts: 67445
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, but be sure not to hard-code the context path. Obtain in programmatically.

In a JSP, you'd use ${pageContext.request.contextPath}
 
Anup Nadig Malathesha
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much Bear.
So I heard that we need to depoly project in the server using war file. But I just copied and pasted these files in the server. won't it be problem. And also do I need to change my url mapping variable in web.xml file.
 
Bear Bibeault
Marshal
Posts: 67445
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Anup Nadig Malathesha wrote:So I heard that we need to depoly project in the server using war file. But I just copied and pasted these files in the server. won't it be problem.


Not if you don't hard-code the context path. This is precisely why you don't hard-code it.

And also do I need to change my url mapping variable in web.xml file.


No. Why would you? The context path is orthogonal to the servlet mappings.
 
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic