Servlet & JSP

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.
Servlet file
JSP file
Output will be same in both cases

Servlet advantages

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.

Servlet Disadvantages

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.

JSP Advantages

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.

JSP Disadvantages

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.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store