Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Calling servlet with jQuery Ajax

 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65115
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As with any other URL to a servlet, the URL must start with the context path of the web app.
 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Bear
I tried this,



But, still no response
 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65115
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65115
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65115
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65115
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What works "in the browser"? Exactly. Don't make us guess.
 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65115
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone explain the reason of this?
Problem still occurs
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65115
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65115
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then "/SmartLife/loadNonWorkingMaster" should work on the page. You are claiming it is not?
 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65115
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65115
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 297
5
Android Fedora Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's built in to Chrome. Right click on the page and select 'Inspect element' or hit F12.
 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65115
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's the primary means to debug client-side code.
 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Bear

I guess log file is the way to debug Server side code
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic