permaculture playing cards*
The moose likes HTML, CSS and JavaScript and the fly likes Calling servlet with jQuery Ajax Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Calling servlet with jQuery Ajax" Watch "Calling servlet with jQuery Ajax" New topic
Author

Calling servlet with jQuery Ajax

Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 618
I am working on servlet hibernate project
I want a functionality that as soon as the page is loaded, it should load the data. There is a single data for this page saved in db. So i am using Ajax n JQuery. Here is the code

Web.xml


myJsp.jsp


My "LoadNonWorkingMasterServlet" class should be called which call some other Java class and prints "In dao" (just for checking)
But on running, i found that the servlet is not executed.
Have also tried this in servlet


What is the reason behind this?
Have i made a wrong call to Servlet (as var url)?


kunal
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60048
    
  65

As with any other URL to a servlet, the URL must start with the context path of the web app.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 618
Thanks Bear
I tried this,



But, still no response
Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 618
I had written the name of project as "Smart Life", (with a space), so after running the tomcat, the url was being changed to "Smart%20Life". I renamed it as "SmartLife"

Though the name is not updated as the context path even after deployment, I appended the servlet access url directly to the browser address bar, and it ran successfully. So, now i guess, the problem is with path
I am now giving the path as "var url = /SmartLife/loadNonWorkingMaster.do"

Is that correct?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60048
    
  65

Does it work?

Regardless, you should not be hard-coding the context path. See the JspFaq for how to obtain it programmatically.
Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 618
I think that is not being updated. Still it shows context path as "/Smart Life" (i am printing it in a console). And so, the JQuery code doesn't works. I restarted the IDE(myeclipse). But still, the same problem
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60048
    
  65

Then stop running it in an IDE. IDEs are not the real world. I never run web apps in an IDE because you waste time configuring the IDE rather than learning how to run web apps for real.
Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 618
You are right. But , we are instructed to work on myeclipse. And i guess its IDE problem as still it shows context path as "/Smart Life". I printed it in console via (request..getContextPath())

Or, there something more, i need to correct?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60048
    
  65

This is not the place to address IDE questions. If you are having problems getting changes to occur in your IDE post in the IDEs forum.
Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 618
Okay.
But, still the problem exists. context path is now changed and is correct


But, it doesn't runs directly. (instead runs when i append the servlet path in the browser)
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60048
    
  65

What works "in the browser"? Exactly. Don't make us guess.
Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 618
When i directly append the url pattern (/loadNonWorkingMaster) in the browser, my servlet is called, and it prints the values, but, it doesn't executes the servlet through Jquery code i.e

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60048
    
  65

Kunal Lakhani wrote:When i directly append the url pattern (/loadNonWorkingMaster) in the browser

You are still making us guess what you entered into the browser. If you want help you will need to put some more effort into your posts.
Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 618
I am not able to make it clear, i guess.

The url to access the servlet, which is not working inside JQuery (i.e var url = "./loadNonWorkingmaster"), when dierctly appended to the context path , runs the servlet, or rather access the servlet code, and gets me the output in console.

This means the var url in the jquery code is not able to call the servlet

Hope, i am able to made it clear
Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 618
Can anyone explain the reason of this?
Problem still occurs
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60048
    
  65

Still waiting for you to tell us exactly what you typed into the browser address bar when it worked. I grow tired of asking.
Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 618
Sorry

I typed "http://localhost/SmartLife/loadNonWorkingMaster" in the browser, and it called the servlet, and prints the response in the console.
But, its not called via Jquery code
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60048
    
  65

Then "/SmartLife/loadNonWorkingMaster" should work on the page. You are claiming it is not?
Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 618
Yes, That's strange. That's not working in the following code (in the page)


Shall i post the complete code of servlet,dao and html page?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60048
    
  65

Please explain exactly how it is "not working". What have you done to debug this? Have you opened the browser's debugger to see if the network activity is taking place at all? Is there a JavaScript error? Is a request being generated? If so, what's the request body look like? And so on...
Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 618
Frankly speaking, No

I don't have any idea about that. Downloaded Firebug, but didn't understood how to use it
Sorry
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60048
    
  65

Well, learning how to use the debugger is an essential skill for web apps. Now is the best time to learn how to use one. Besides, to answer any question that I'll be asking, you'll need to use the debugger to find out the answers.

I'd switch to Chrome or Safari for debugging this. The WebKit debugger is more fully featured then Firebug.

Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 618
Thanks. You are absolutely right. Its the best time to learn debugging.
It will be great if you post the link of Webkit Debugger for Google Chrome. I found WebInspector – WebKit.
Rob Crowther
author
Ranch Hand

Joined: Nov 06, 2012
Posts: 268
    
    4

It's built in to Chrome. Right click on the page and select 'Inspect element' or hit F12.


Read my books: Hello! HTML5 & CSS3 | HTML5 in Action | Read my blog
Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 618
Thanks Rob Crowther, Bear Bibeault for your reply

The debugger built in chrome, gives this error "Uncaught ReferenceError: $ is not defined " in the line "$.getJSON(url, function(data) { "

That means, i was missing something that could tel the browser that i am using JQuery.
Solved it. Thanks a lot

What are the other ways to debug apart from Browser's Debugger?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60048
    
  65

That's the primary means to debug client-side code.
Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 618
Thanks Bear

I guess log file is the way to debug Server side code
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Calling servlet with jQuery Ajax
 
Similar Threads
Calling DB onSelectRadio and populating another table in the same JSP page
help me.. problem with using android app, jquerymobile and html
Can you post data from textfields to a URL without having a <form>
Problem with ajax implementation using Jquery
Trying to use AJAX to call a servlet