• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem regarding Ajax in JSP

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


The Jsp file related to the database is :


Here the problem is when i select the employee id from the list, the employee name is not fetched from the database?
Please suggest necessary changes .
Thanks in advance
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That isn't Java® code; is it JS?
Moving to the JSP and HTML/JS fora.
 
Saloon Keeper
Posts: 15490
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're making two huge mistakes:

1) You're performing processing in your JSP. The only thing JSP should do is display data that has been forwarded to it by a servlet. Move all your database access code to a servlet.

2) Your database queries are vulnerable to SQL injections. Use PreparedStatement for queries that depend on user input.
 
Saloon Keeper
Posts: 7582
176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the driver class of the MySQL JDBC driver is com.mysql.jdbc.Driver, not mysql.jdbc.Driver.

You should also consider using a library such as jQuery for the AJAX call, rather than calling XMLHttpRequest directly - that's very outdated. See http://api.jquery.com/category/ajax/shorthand-methods/ for what's on offer.

And finally, switch from XML to JSON as the data format. It makes the client-side handling much simpler.

If all this sounds like a lot, maybe try to find an example that's more up-to-date; the one you're following seems way obsolete. A quick search finds http://www.mysamplecode.com/2012/04/jquery-ajax-request-response-java.html and http://www.mysamplecode.com/2012/11/jquery-getjson-example-java-servlet.html. Those seem like better starting points.
 
Stephan van Hulst
Saloon Keeper
Posts: 15490
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Moores wrote:You should also consider using a library such as jQuery for the AJAX call, rather than calling XMLHttpRequest directly - that's very outdated.


Disagree. If all that a client does is perform some AJAX requests, loading all of jQuery seems like a bit of an overkill to me.
 
Tim Moores
Saloon Keeper
Posts: 7582
176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Engineering is all about choosing between different trade-offs, so I stand by my advice to consider different approaches. (Maybe you missed the "consider" part?)
 
Stephan van Hulst
Saloon Keeper
Posts: 15490
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, I disagreed with the "outdated" part. :)
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:

Tim Moores wrote:You should also consider using a library such as jQuery for the AJAX call, rather than calling XMLHttpRequest directly - that's very outdated.


Disagree. If all that a client does is perform some AJAX requests, loading all of jQuery seems like a bit of an overkill to me.



Disagree with the disagree. There's still way too much room for error and browser shenanigans when using XHR by hand.

Until fetch is supported across all supported browsers, jQuery is still the best bet for performing Ajax calls without writing a pageful of error-prone code.
 
Stephan van Hulst
Saloon Keeper
Posts: 15490
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah, I was not aware of browser differences.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Ah, I was not aware of browser differences.


Yeah, especially if still supporting older versions of IE (shudders).

If only supporting modern browsers, I'd skip XHR completely and go with fetch.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic