Servlet & JSP
In the recent trends, we all have seen many websites like facebook. There’s more than billions of people are using facebook. Have you ever noticed how the developer has designed separate webpages for each user. Has the developer created this many webpages for each user? Let’s understand how the sites like facebook web pages are designed!!
You might have noticed that when you search for the url www.facebook.com, every user will get the same facebook login page. These pages known as static pages. When the user logs in, the design and structure of the web page for every user is same, the only difference will be the content. These structured web pages are designed using JSP. Once the user logs in, their personal facebook data will be pasted on the JSP page. These pages are known as dynamic pages. Where do we find these data or contents to be pasted on JSP file? These contents or data is fetched using servlet.
Servlets and JSPs are parts of a Java web application that is running on a web server to create dynamic web applications. The dynamic web application content can be changed depends on a specific request that was sent to the webserver.
Let’s understand what are the differences between Servlet and JSP technologies:
Why use Servlet?
- The performance is much better.
- Servlet is used when you do not like to create a separate process to handle each and every request of client.
- Developers can use servlets when they have to take advantage of all the features of Java.
- Java class libraries which are available to a servlet can communicate with applets, databases, or other software via RMI and sockets mechanisms.
Why use JSP?
- In Java server pages JSP, the execution is much faster compared to other dynamic languages.
- Java server pages (JSP)are always compiled before its processed by the server as it reduces the effort of the server to create process.
- Java server pages are built over Servlets API. Hence, it has access to all Java APIs, JNDI, JDBC EJB, and other components of java.
- JSP is an important part of Java EE (Enterprise Edition), which is a platform for enterprise-level applications.
Differences between Servlet and JSP
- Servlet is written with Java code, JSP can consist of either HTML or XML, or as a combination of both is also possible. Servlet code is harder to write as it is HTML in java. On the contrary, JSP has a Java code inside HTML, that is easier to write and to maintain.
- Servlets run faster than JSP, on the other hand JSP runs slower than servlet as it takes time to compile the program and convert into servlets.
- If you need to do modification in Servlet, it will take time, because it includes recompiling the java class, reloading it on the server, and restarting the server. The modification of JSP takes less time. You just need to refresh the browser page.
- JSP technology allows you to build a custom tag. This is one of the key advantages because with re-usable components we get a lot of flexibility. On another side, servlets are written in Java language. Therefore, it allows using all Object Oriented programming techniques.
- You can override the service() method in servlet but, in JSP, you can’t override the service() method.
- In MVC architecture, servlet works as a controller while JSP works as a view for displaying output.
Here is one simple program using Servlet and JSP:
Servlets technology is a good way to generate easier to write and faster to run dynamic documents. The first valuable thing to mention is the performance. Servlet is loaded after the first request and remain in memory till the destroy. Secondly, is the simplicity. Servlet runs inside a controlled web server environment and you don’t need specific client software, a web browser is usually enough. They provide easy portability across Web Servers. One more advantage of servlets is session management. Since all web applications are stateless protocol, servlets overcome HTTP stateless nature. The servlets use its own API to maintain session. Servlets are built on Java Technology, which gives a lot of powerful features, such as Exception handling, garbage collection, network access, connectivity with Databases, integration with J2EE, etc.
It is not easy to work with user interface designing in servlet and it slows down the application development. When we are writing complex business logic in servlets, it makes the code difficult to understand. You need to setup Java Runtime Environment on the server to run servlets.
The main advantage of JSP technology in comparison with Servlet technology is that it allows us to separate presentation logic(HTML) from business logic (Java code). Therefore, the code will be cleaner. work on the user interface will be quicker because the developer will see just the presentation code. JSP technology allows tag-based programming, this means that extensive Java language knowledge is not required. JSP provides built-in JSP tags. Also, it allows us to develop custom tags or to use third-party tags, which makes the development of pages even easier. One more important advantage is that modification done in JSP is recognizing by webserver automatically. You don’t need to reload your web application. JSP automatically takes care of exception handling, developers don’t need to explicitly handle this.
Anyway, even with a lot of cool features, JSP technology has room for improvement. It is not easy to diagnose errors, because JSP pages are translated, and then compiled into Java servlets. All errors that appear in your pages will be rarely seen as errors arising from the coding of JSP pages. We will see them as Java servlet errors or HTML errors. JSP pages require more disk space because the webserver has to store the resultant class files(generated from JSP pages) and the JSP pages themselves. During the first access, JSP pages must be compiled on the server. This initial compilation causes a delay that is noticeable.