Search This Blog

Monday, 23 January 2017

Difference Between Servlet and JSP

This post highlights the difference between JSP and Servlet technologies.

Java Servlet technology and JavaServer Pages (JSP pages) are server-side technologies that become the standard way to develop web applications.

Most importantly, if used effectively by following best practices, servlets and JSP pages help separate presentation from content.

Servlets support a request and response programming model. When a client sends a request to the server, the server sends the request to the servlet. The servlet then constructs a response that the server sends back to the client. When a client request is made, the service method is called and passed a request and response object. The servlet first determines whether the request is a GET or POST operation. It then calls one of the following methods: doGet or doPost. The doGet method is called if the request is GET, and doPost is called if the request is POST.

Adding more, Servlets are Java classes that can generate dynamic HTML content using print statements.
out.println("<html><head><title>"+thisIsMyMessage+"</title></head>");
So, you can embed HTML code into Java code.

A JSP page is basically a web page with traditional HTML and bits of Java code. The file extension of a JSP page is .jsp rather than .html or .htm, which tells the server that this page requires special handling that will be accomplished by a server extension. When a JSP page is called, it will be compiled (by the JSP engine) into a Java servlet.

Importantly, we can add dynamic content or Java Code inside an HTML Tag using JSP’s Scriplets.
And scriptlets, this is code fragments like:
<% String message = "Hello !"%>
<H2> Welcome and  <%=message%>! </H2>
So, you can embed Java code into HTML code.





SERVLET
JSP
A servlet is a server-side program and written purely on Java.
JSPs are HTML pages with .jsp extension. JSP’s are extension of servlets to minimize the effort of developers to write User Interfaces using Java programming.
Executes inside a Web server, such as Tomcat
A JSP program is compiled into a Java servlet before execution. Once compiled into a servlet, it's life cycle will be same as of servlet. But, JSP has it's own API for the lifecycle.
Servlets run faster than JSP
JSP runs slower because it has the transition phase for converting from JSP page to a Servlet file.
Servlet has the life cycle methods init(), service() and destroy()
JSP has the life cycle methods of jspInit(), _jspService() and jspDestroy()
Difficult to write as one has to write HTML tags within quotes(“<HTML>”) in Java. Mixing HTML content inside Java is tedious.
Easier to write than servlets as it is similar to HTML
Written in Java, with a few additional APIs specific to this kind of processing. Since it is written in Java, it follows all the Object Oriented programming techniques.
One of the key advantage is we can build custom tags using JSP API ,  which can be available as the re-usable components with lot of flexibility
In MVC architecture Servlet acts as controller.
In MVC architecture JSP acts as view.
Servlet advantages include:
·  Performance: get loaded upon first request and remains in memory indefinitely.
·  Simplicity: Run inside controlled server environment.
·  Session Management : overcomes HTTP's stateless nature
·  Java Technology : network access, Database connectivity, j2ee integration
JSP Provides an extensive infrastructure for:
·         Tracking sessions.
·         Managing cookies.
·         JSP is Extensible: Can create custom tags to extend the JSP functionality.
·         Separation of roles: Clearly identifies separation of roles for Developers, Content Authors/ Graphic Designers/ Web Masters.

6 comments:

  1. So if we need to return a view in case of servlet's , the only way is to create an html page in java with quotes and all,and then send them in response??? Correct??

    ReplyDelete
  2. So if we need to return a view in case of servlet's , the only way is to create an html page in java with quotes and all,and then send them in response??? Correct??

    ReplyDelete
  3. In servlets view is an html page but in jsp view is jsp page onley. Is it correct?

    ReplyDelete