Difference Between Servlet and JSP

This section describes you the difference between Servlet and JSP.

Difference Between Servlet and JSP

Difference Between Servlet and JSP

In this section we will describe the Servlet and JSP. We will compare the both technologies. We will discuss the differences between Servlet and JSP.

Servlet

  • Servlet is a Java class that is usually written for extending the servers functionality.
  • To write an HTML code into Servlet you would have to write inside the Java code.
  • Servlet is a compiled class which used for creating dynamic content.
  • When running a Servlet and JSP, Servlet is faster than the JSP.
  • Coding in Servlet is hard than the JSP.
  • In MVC pattern, Servlet plays a controller role.
  • In Servlet, there is no such method for running JavaScript at client side.
  • To run a Servlet you have to make an entry of Servlet mapping into the deployment descriptor file i.e. web.xml file externally.

JSP

  • JSP is a file usually written for showing output of data.
  • In a JSP file HTML can be embed with the Java Code. You can write HTML and Java code separately.
  • JSP is first compiled into a Servlet. JSP also creates the dynamic content.
  • JSP runs slow than the Servlet.
  • JSP provides the various implicit objects, tags, scriptlets due to which the coding in JSP is easier than the Servlet.
  • In MVC pattern, JSP is used for showing output data i.e. in MVC it is a view.
  • In JSP, we can use the client side validations using running the JavaScript at client side.
  • For running a JSP there is no need to make an entry of Servlet mapping into the web.xml file externally, you may or not make an entry for JSP file as welcome file list.

Conclusion : As the differences discussed above I reached out the conclusion that the Servlet should be used there where you have to write more Java codes and JSP should be used where, you have to write more HTML codes i.e. the Servlet should be used for processing and JSP should be used for showing/viewing.

Example

Here I am giving a simple example which will demonstrate you how the Servlet and JSP are different from each other. Here I will write a Servlet and JSP for producing the output. After analysis these examples you will understand where the Servlet should be used and where the JSP should be used. In this example at first we will create a Servlet and then after we will create a JSP.

ServletExample.java

package net.roseindia;

import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServlet;

public class ServletExample extends HttpServlet{

public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();

String str = "Servlet Hello World Example";

out.println("<html><head><title>Servlet Example</title></head>");
out.println("<body><h3>"+str+"</h3></body>");
out.println("</html>");
}
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>servletAndJsp</display-name>
<servlet>
<servlet-name>servlet</servlet-name>
<servlet-class>net.roseindia.ServletExample</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>servlet</servlet-name>
<url-pattern>/servletExample</url-pattern>
</servlet-mapping>
</web-app>

Servlet Output

When you will compile and deploy the ServletExample.java you will get the output as follows :

jspExample.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JSP Example</title>
</head>
<body>
<%
String str = "JSP Hello World Example";
%>
<h3><%=str %></h3>
</body>
</html>

JSP Output :

When you will deploy the jspExample.jsp file then you will get the output as follows :

Download Source Code