• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem regarding Ajax in JSP  RSS feed

 
VarunS Singh
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • 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
 
Campbell Ritchie
Sheriff
Posts: 55351
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That isn't Java® code; is it JS?
Moving to the JSP and HTML/JS fora.
 
Stephan van Hulst
Saloon Keeper
Posts: 7737
142
  • Mark post as helpful
  • send pies
  • 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.
 
Tim Moores
Saloon Keeper
Posts: 3834
80
  • Mark post as helpful
  • send pies
  • 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: 7737
142
  • Mark post as helpful
  • send pies
  • 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: 3834
80
  • Mark post as helpful
  • send pies
  • 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: 7737
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I disagreed with the "outdated" part. :)
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66158
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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: 7737
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, I was not aware of browser differences.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66158
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!