In brief, it can be defined as Servlet are the java programs that run on a Web server and act as a middle layer between a request coming from HTTP client and databases or applications on the HTTP server.While JSP is simply a text document that contains two types of text: static text which is predefined and dynamic text which is rendered after server response is received.
The following are the important differences between ArrayList and HashSet.
| Sr. No. | Key | Servlet | JSP |
|---|---|---|---|
| 1 | Implementation | Servlet is developed on Java language. | JSP is primarily written in HTML language although Java code could also be written on it but for it, JSTL or other language is required. |
| 2 | MVC | In contrast to MVC we can state servlet as a controller which receives the request process and send back the response. | On the other hand, JSP plays the role of view to render the response returned by the servlet. |
| 3 | Request type | Servlets can accept and process all type of protocol requests. | JSP on the other hand is compatible with HTTP request only. |
| 4 | Session Management | In Servlet by default session management is not enabled, the user has to enable it explicitly. | On the other hand in JSP session management is automatically enabled. |
| 5 | Performance | Servlet is faster than JSP. | JSP is slower than Servlet because first the translation of JSP to java code is taking place and then compiles. |
| 6 | Modification reflected | Modification in Servlet is a time-consuming task because it includes reloading, recompiling and restarting the server as we made any change in our code to get reflected. | On the other hands JSP modification is fast as just need to click the refresh button and code change would get reflected. |
Example of Servlet
JavaTester.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class JavaTester extends HttpServlet {
private String message;
public void init() throws ServletException {
// Do required initialization
message = "Hello World";
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// Set response content type
response.setContentType("text/html");
// Actual logic goes here.
PrintWriter out = response.getWriter();
out.println(message);
}
}Output
Hello World