Difference between servlets and JSP

Many people think that difference between JSP and servlet is that a JSP cannot be mapped to some other URL and JSP's don't have a life cycle mechanism. That is not correct. It is possible to map any URL to a JSP file. The configuration is almost the same except that instead of element element is used. Also a JSP can define init and destroy methods. The JSP should implement jspInit() and jspDestroy() methods as jsp declarations. It is possible to specify element for a JSP file and load the JSP file on container start up.

Then what is the difference between JSP and servlet ? Some differences I can point out are

1, JSP can contain HTML mark up same as in a pure HTML file where as in a servlet it needs to be put inside Java code. HTML editors can work with JSP files.
2, JSP has a scripting and expression language.
3, A servlet has access to underlying ServletOutputStream and can send binary data to the client. A JSP gets a java.io.Writer object only and it is not suitable for sending binary content.

I created a sample web app to demonstrate this and is available at http://antopaul.googlepages.com/jsp_mapping.zip


Popular posts from this blog

java.util.logging - Bad level value for property: org.openqa.level

Solaris : Send mail with attachment

Suppressing JExcel warnings