JavaServer Pages(JSP) and Servlets

KARAN
4 min readOct 2, 2023

--

Understanding JSP and Servlets

what are Jsp and Servlets?

Servlet- A servlet is used to create a web application. Servlets are java classes that run on the server-side of a web application.They extend the functionality of a web server and handle HTTP requests and responses.

Jsp — A Jsp is used to create a web applications like servlets. Jsp is a embedded Java code within a html pages.Making it more easier to make dynamic web pages.

How they work together?

Servlets and jsp work together by sharing data through session,or requests . Servlets can set attributes in the request scope and jsp can access that attributes through embedded code in this (<% %>). So basically Servlets handle the http requests and jsp is responsible for UI rendering.

What is a web application?

A web application is a application accessible from the web. It contains web contents like Jsp ,Servlets,Filtersl,etc. and other resources like Html,css and javascripts . It exectures the web components in the web server and responds to the Http requests.

Advantages of Servlets :

There are many advantages of a servlet :-

  1. Secure: Becuse it runs on java so it is more secure.
  2. Better Perfomance — Because it create a thread of the each requests.
  3. Portability: Because it uses java language.
  4. Platform Independent- Servlets are platform independent because they uses Java language and we can run on any platfrom which supports JVM(Java Virtual Machine).

Web Terminology

  1. Website Static vs Dynamic — It is a collection of related web pages that may contain text,images,audio and video.
  2. Http-It is a used to make a connection between server and client.
  3. Http Requests- It is the request send by the local computer to the web server that contains all the interesting information.
  4. Get vs Post — In case of get request are intended to retrive data from a server and in case of post request, used to send data to the serve and may modify the server’s state.
  5. Container- It is used in java for dynamically generating web pages on the server side.

Servlet’s API

  1. The javax.Servlet contains many interfaces that are used by servlet or web container.
  2. The javax.servlet.http contains interfaces that are resposible for http request only.

Servlet Life Cycle

  1. Servlet class is loaded.
  2. Servlet instance is created.
  3. Init method invoked.
  4. Service method is invoked.
  5. Destroy method is invoked.

Request Dispatcher in Servlets

Request dispatcher is used to send the request from servlet to other resources like html,jsp or srevlet. This dispatcher is also used to include the content of other resources. It includes to methods -

  1. Forward() — used to forward requests from one servlet to other resources .
  2. Include() — used to include the content of other resources.

SendDirect in servlets

It is used to redirect resposes to other resources it can be html, jsp and servlets .

It works as the client side because it uses the url bar of the browser to make the another request.

ServletConfig Interface

An object is of Servletconfig is created by the web container for each servlet. A core advantage of ServletConfig is that you don’t have to modify the servlet file if information is modified from the web.xml file.

ServletContext Interface

ServletContext is another interface provided by the Servlet API, and it represents the entire web application hosted by the Servlet container. Unlike ServletConfig, there is only one ServletContext object per web application, and it is shared among all Servlets.

Session in Serlvets

Session simply means a particular interval of time. Session Tracking is a way to maintain state (data) of an user. It is also known as session management in servlet.A session is a way to store information (in variables) to be used across multiple pages.

Cookies in Servlet

By default, each request is considered as a new request. In cookies technique, we add cookie with response from the servlet. So cookie is stored in the cache of the browser. After that if request is sent by the user, cookie is added with request by default. Thus, we recognize the user as the old user.

Servlet Filter

A filter is an object that is invoked at the preprocessing and postprocessing of a request.It is mainly used to perform filtering tasks such as conversion, logging, compression, encryption and decryption, input validation etc.

Jsp(Java Server Pages)

JavaServer Pages (JSP) Overview:

A Java technology for creating dynamic web pages is called JavaServer Pages (JSP). It makes it easier for developers to create dynamic, data-driven web apps by allowing them to combine HTML and Java code. Compiling JSP pages results in servlets, which execute on the server and provide dynamic content to web browsers.

Describe JSP.

JavaServer Pages is referred to as JSP. With the use of this server-side technology, programmers can include Java code into HTML templates. JSP pages are utilised for database interaction, user input processing, and dynamic content generation. A crucial component of Java web development are they.

Scriptlets and JSP Tags:

JSP pages combine HTML and Java code using a variety of tags and scriptlets:

Directives: Include external files and manage page configuration.
Scriptlets: Implement dynamic actions by embedding Java code inside <% %> tags.
Custom Tags: For reusable functionality, define custom tags.
Language of Expression (EL): With ${} notation, data may be conveniently accessed and shown.

HTML and JSP Combination:

Java and HTML are effortlessly integrated using JSP. To provide dynamic content, such as managing form submissions, presenting database results, or customising user interfaces, developers can enter Java code. Java provides functionality, while HTML provides structure, and this combination is essential to JSP’s ability to create dynamic web applications.

--

--