Difference between JSP and Servlet

Key difference: ‘JSP (Java Server Pages)’ is a server-side technology, whereas ‘Servlet’ is a small program that runs on the server. Both of these Java technologies are used for creating dynamic web pages.

Sun Microsystems released JSP (Java Server Pages) in 1999. JSPs are Java scripting languages that are generally used for creating HTML, XML, or other document type web pages. These pages have dynamic scripting capability of separating the page logic from that of the static components of the page. Their basic design actually makes the HTML more functional, and makes the database queries more dynamic. A compatible web server with a servlet container (such as Apache Tomcat or Jetty) is required to deploy and run the Java Server Pages (JSP). A JSP is the high-level abstraction architecture of a Java Servlet. At runtime, these JSPs are translated into servlets. The JSP servlets are cached and re-used until the original JSP is modified. The JSP can be used independently and can also be viewed as a component of the server-side model–view–controller design. Programmers make use of Java code along with some pre-defined actions to create web markup content pages; these pages are further compiled and executed on the server side to deliver the documents.

JSP pages use several delimiters for scripting functions. The most basic is <% ... %>, which encloses a JSP scriptlet. A scriptlet is a fragment of Java code that is run when the user requests the page.

The following is a valid for loop in a JSP page:

<p>Counting to three:</p>

<% for (int i=1; i<4; i++) { %>

    <p>This number is <%= i %>.</p>

<% } %>

<p>OK.</p>

Its output would be represented as:

Counting to three:

This number is 1.

This number is 2.

This number is 3.

OK.

Servlets are small Java programs which are related to Java Applets. They basically run within a web server environment. The Servlet specification was created by Sun Microsystems, with version 1.0 finalized in June 1997. Its starting version was 2.3, while its current version, as of 2013, was 3.1. A servlet is a Java class used to extend the capabilities of a server. They respond to any kind of requests in the form of appropriate responses. The basic Servlet package defines Java objects to represent the servlet requests and responses. Servlets can be generated automatically from Java Server Pages (JSP) by the JavaServer Pages compiler. Technically, a "servlet" can be defined as a Java class in Java EE that conforms to the Java Servlet API.

Servlets are communicators over a client–server protocol, and are most often used with the HTTP protocol. Their basic purposes are:

  • To process or store and submit the HTML form data.
  • To provide dynamic content such as the results of a database query.
  • Manage state information that does not exist in the stateless HTTP protocol, such as filling the articles into the shopping cart of the appropriate customer.

The servlet works on a servlet life cycle, there are basically three servlet methods which define the servlet life cycle:

  • init(),
  • service(), and
  • destroy()

Comparison between JSP and Servlet:

JSP

Servlet

What are they

JSP is a webpage scripting language, generally used to create the dynamic web content.

Servlets are Java programs that are already compiled and which also create dynamic web content.

Typically

JSP is typically more oriented towards displaying information.

Servlet is more oriented towards processing information.

Role in MVC (Model View Controller)

JSP acts as a viewer.

Servlet acts as a controller.

Applicable at the time of

They are generally preferred when there is not much processing of data required.

They are generally preferred when there is more processing and manipulation involved.

Running speed

JSP runs slower as compared to a Servlet. JSP compiles into Java Servlets.

Servlets run faster as compared to JSP.

Code complications

The code programming is easy as compared to that of Servlets.

The code programming is difficult as compared to that of JSP.

Facility

Here, we can build custom tags which can directly call Java beans.

No such facility is available in servlets.

Consists of

JSP are Java HTML representation mixed with JAVA scriptlets.

Servlet are full functional Java codes.

Consistence of objects

JSP has Implicit objects.

Servlets does not have such type of objects.

Examples

To display a report.

To process a user submitted form.

ncG1vJloZrCvp2OxqrLFnqmeppOar6bA1p6cp2aZo7Owe8OinZ%2Bdopq7pLGMm5ytr5Wau2620qlkmqaUYsCmvtWlnK0%3D