Servlet and JSP

Contents

keyboard_tab

The course is part of this learning path

Start course
Difficulty
Intermediate
Duration
50m
Students
3
Description

In this course, we will learn the concepts of Java EE 7 with a focus on Java Server Pages.

Learning Objectives

  • Java Server Pages

Intended Audience

  • Anyone looking to get Oracle Java Certification
  • Those who want to improve Java 7 EE knowledge
  • Java developers

Prerequisites

  • Have at least 2 years of Java development experience 
Transcript

Hello there. In this lesson, we'll examine the differences between Servlet and JSP. So, let's start. As you remember, Servlet is a Java technology that is managed by a container called a Servlet engine. It generates dynamic content and interacts with the client through request and response. Servlet extends the functionality of a web server. Those servlet can respond to many types of requests. They generally implement web containers for hosting any website on web server. Therefore it's qualified as a server-side servlet web API.  And JSP is a collection of technologies developed by Sun Microsystems. It is used to develop web pages by inserting java code into the HTML pages by making special JSP tags. It can consist of either HTML or XML combination of both is also possible with JSP actions and commands. So, why do we use servlet? Because the performance is much better. The servlet is used when you do not like to create a separate process to handle each and every request of the client. Developers can use servlets when they must 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 we use JSP?  Because

in Java server pages JSP, the execution is much faster compared to other dynamic languages. It's much better than common gateway interface (CGI). Java server pages, JSP are always compiled before it's 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. What is the difference between JSP and servlet? This table shows the main differences. Servlets run faster than JSP. JSP runs slower than servlet as it takes time to compile the program and convert it into servlet. It's hard to write code in servlet. It's easier to code in JSP compared to servlet. In MVC architecture, servlet works as a controller. In MVC architecture, JSP works as a view for displaying output. 

It should be used when there is more data processing involved. JSP is generally used when there is no involvement of much processing data. There is no custom tag writing facility in servlet. You can easily build custom tags that can directly call JavaBeans. Servlet is a Java code. JSP is an HTML based code. It can accept all protocol requests including HTTP. It can only accept HTTP requests. You can override the service method. In JSP, you can't override the service method. In servlet by default, session management is not enabled, user must enable it explicitly. In JSP session management is automatically enabled. In servlet, you have to implement both business logic and presentation logic in the single file. In JSP, business logic is split from presentation logic using JavaBeans. Modification in servlet file is time consuming  due to reloading, recompiling, and restarting the server. JSP modification is fast as you just need to click one refresh button. Let's take a look at the lifecycle of servlet. Start, execution of servlet begins. Loading and instantiation void in it. It's called when servlet is first loaded. This method lets you initialize servlet. Initialized void service. The purpose of this method is to serve a request.

You can call it as many times as you like. Handling requests and destroying servlet. Java application must be first determined what code is needed to execute the request URL to provide a response. The destroy servlet void destroy method is used at the end of servlet lifecycle. End of request thread. When service finishes its task, either the thread ends or returns to the thread pool that is managed by servley container, and servlet lifecycle finishes. Stop, servlet stop executing. And this is JSP lifecycle. Transition of JSP page into a servlet. Compilation of JSP page. Compilation of JSP page underscore into underscore JSP.Java. Class loading_jsp.Java is converted to class file_jsp.class. Instantiation. Object of generated servlet is created. Initialization_jsp in it method is invoked by container. Request processing_jsp service method is invoked by the container. Destroy_jsp destroy method invoked by the container. So, what are the pros and cons of servlet? Pros: Servlet loads only one copy of it into JVM, Java virtual machine. It saves time to respond to the first request, which increases response time. 

It uses standard API that is supported by numerous web servers. Programming languages like platform independence and ease of development. You can access the large set of APIs  that are available for the Java platform. The web container makes threads for handling more than one request to the servlet. Easy coordination between two or more servlet to make web applications. Servlet containers support many other features like sessions, resource management, persistence, security, etc. Servlet usage doesn't constrained by the web browser. Cons: One servlet is loaded into JVM. It does matter numbers of requests. When there is a request, there is a thread not a process. Servlet is persistent until it destroys. Designing in a servlet is difficult and slows down the application. You need a JRE, Java Runtime environment, on the server to run servlets. For non Java developers, servlet is not suitable as they are required to have a broad knowledge of Java servlet. HTML code is mixed up with Java code. Therefore, changes done in one code can affect another code. Writing HTML code in servlet programming is very difficult. 

It also makes servlet look bulky. In servlet programming, if you wish to use implicit objects, you need to write some additional code in order to access them. Developers must take care of exception handling because servlet programming is not thread safe by default. How about JSP? Pros: It is very much convenient to modify the regular HTML. We can write the servlet code into the JSP. It's only intended for simple inclusions which can use form data and make connections. You can easily connect with JSP with the my SQL database. The performance and scalability of JSP are good because JSP allows embedding of dynamic elements in HTML pages. JSP is built on Java technology. Hence, it is platform independent and not depending on any operating systems. JSP includes the feature of multi threading of Java. We can also make use of exception handling of Java into JSP. It enables two separate presentation layer with business logic layer in the web application. It's easy for developers to show as well as process the information. Cons: It's hard to trace JSP pages error because JSP pages are translated to servlet. As JSP output is HTML. It's not rich in features. It's very hard to debug or trace errors because JSP pages are first translated into servlets before the compilation process. Database connectivity is not easy. JSP pages require more disk space and time to hold JSP pages as they are compiled on the server. So, that's it. Hope to see you in our next lesson. Have a nice day.

 

About the Author
Students
1300
Courses
64
Learning Paths
4

OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.

Covered Topics