Wednesday, September 9, 2009

When a Java Servlet / JSP is used?

If you are a hard core programmer then please skip this blog. As this is to enlighten the new programmers.

One common doubt which is when I should use Java Servlet and JSP? For all you know, Servlet is used when you have more Java codes to be used and less HTML tags. On the other hand, JSP is used when you have more number of HTML tags and less JAVA code.

The next question should  is Why??? Is there a reason behind this?

Yes, the following are the advantages of each other:

Servlet: Advantages

1. A java IDE like Eclipse, NetBeans can be used to create these servlets with ease and can be complied using javac whithout any problem as all you know, Servlet is just another class which extends HttpServlet class.

So, use Servlets where Java code is required more and less HTML.

JSP: Advantages

1. The easiest thing for a programmer is writing HTML codes – no indentation – no hard and fast rules – Just write anything and you can see a HTML page :)

2. Also, the point to be noted is that a new servlet will be created whenever you modify the JSP.

So, use JSP where there is a requirement of more HTML and less Java code.

1 comments:

Santosh Jayamurugan said...

IMHO...

According to MVC2 architecture... JSP is best used as a presentation technology and servlet as a place where u put all ur business logics...

JSP becomes heavy if u have ur java scriplets(business logic) embedded to it... JSP engine generates a servlet for every JSP created. So the engine has to translate and update the JSP's servlet every time u modify the JSP... This is costly and takes time to load...

And Servlets become hard to maintain if u put a lot of presentation centric codes(html)....

So servlet does the job and pushes the result to JSP which presents it to the user..JSP cant be powerfull without their JSP elements and directives... So it does contain Java scriplets.. but they are presentation centric without IO,DB or other resource interactions..

So its not like we can choose one over the other... They are Complimentary to one another....