• Post Reply Bookmark Topic Watch Topic
  • New Topic

Ajax post cannot find servlet  RSS feed

 
Anup Nadig Malathesha
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.
 
Saif Asif
Ranch Hand
Posts: 440
Eclipse IDE Hibernate 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.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66151
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • 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
.
This is the where that I kept my servlet and all other files in the server.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66151
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • 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
Author and ninkuma
Marshal
Posts: 66151
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • 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
Author and ninkuma
Marshal
Posts: 66151
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!