JSP, Servlet – Two very different things


People who are in the process of getting familiar with programming for the web using Java would have come across these two terms: JSP and servlet. And I have seen my peers getting quite perplexed with what the two meant and why there were two seemingly similar ways of doing the same thing.

Well, I havent been able to verbalise the differences between the two exactly, but still a small attempt on my part –

Servlet
Servlet is a Java object. It extends or adds to the capabilities of a HTTP server. It is a program, which means you type out your servlet, compile it using javac and load the byte code class file into the server’s webapp folder. Next time you change a feature in your servlet, you recompile it and reload the class file into the server. The HTTP server provides an environment for a web application to run, and servlets add to the HTTP server’s capability in providing the right environment, and right response to requests.

One can generate output from within a servlet, only by putting the output in the System.out.println, which will be sent to the browser as a response. And when we get output in a browser, it is instinctive to add HTML tags to the output and beautify it.

Picture this: I have a webapplication written using servlet with a well formatted output. But after a while, I want to change the way the output is displayed in the browser without making any changes to the way the application works behind the scenes. To achieve this, I have to filter through all the Java code, pick all the S.O.Ps and rewrite them. The recompile and reload. What if there was an easier way to do this? What if I dint have to wade through application logic statements to change the front end? This is what JSP was created for.

JSP
JSP is Java in a scripting language format.

One has to compile and generate an executable from a program to see its output. A script on the other hand is compiled on the fly. So we write a script and upload, no need to compile it.

JSP can be embedded in HTML. So there is a kind of division of labour. JSP handles the dynamism required in the presentation, while application logic is handled by somebody else(typically a Java Bean).

To quote San Shing of www.javapassion.com fame, “JSP is a follow-on technology to the servlet.” So it retains all the benefits over the servlet, while overcoming its shortcomings.

So, in conclusion, JSP is a Scripting language with the goal being capable of handling the presentation. While Servlet is an extension of the HTTP server, which can give an environment for smaller components and scripts to run inside it.

Advertisements

About vatsalad

Hi, I'm Vatsala
This entry was posted in j2ee, Uncategorized and tagged , , , , . Bookmark the permalink.