0

I am working on a web app, where a EXTJS makes an ajax call to a JSP, the JSP in turn calls a java class and then returns the value and updates something in the database.

now the question is, I somehow do not feel this is an effective way, I discovered about servets working and was wondering if should just make a Ajax call to the servlet instead of JSP. Are there any other ways which are better and optimized. Please explain.

Thanks, SixthString

oortcloud_domicile
  • 840
  • 6
  • 21
  • 41
  • 1
    Definitely use a servlet for this. Related question: http://stackoverflow.com/questions/4112686/update-current-page-with-a-servlet – BalusC Mar 06 '11 at 03:41

2 Answers2

2

You're right - a Servlet is better suited for situations like there where all you really need is data from a web end point. The purpose of JSP technology is presentation, there really shouldn't be any business logic in a JSP if you can help it. JSPs are usually employed as the V in MVC (Model-View-Controller) - a design pattern that advocates separating the presentation from the control and business logic. So in theory, if you did everything just right, you would be able to swap out one view for another with minimal work since the view just presents the information.

Do note that JSPs are eventually compiled into Servlets themselves so the issue isn't one of a technical limitation but more of a good design/programming practice.

no.good.at.coding
  • 20,221
  • 2
  • 60
  • 51
1

It depends on what you want returned form your Ajax call. JSP is used for generating formatted HTML. If that's what you're looking to get from the Ajax call, then JSP could be the right choice.

If you're looking for some raw data, then you might as well skip the JSP template and go straight to the servlet. I recommend using JSON in this case, because it's easy to work with in JavaScript.

Mike M. Lin
  • 9,992
  • 12
  • 53
  • 62
  • exactly i am just returning some JSON data. I think I will go straight to a servlet instead of a JSP. Although are there any other ways to achieve this? thanks. – oortcloud_domicile Mar 06 '11 at 03:50
  • 1
    @Sixth: JAX-WS or JAX-RS can also do that. And it's actually nicer :) JEE web containers (read: not Tomcat and like) supports it out the box. – BalusC Mar 06 '11 at 04:01