Servlet and JSP for web development

Manish singh rav
4 min readMay 30, 2021

In this article, We will go through JSP and Servlet.

What is Servlet and Why we use ?

Servlet is a java program, Which fetches and process the information on clients request and gives the response.
→Servlet is used for web development.
→In java, We can make a class servlet by extending HttpServlet class.
→Servlet can be used to create a dynamic web page.

Client-Server architecture :

In this figure we can say a client has requested, To the server for a particular HTML page, The server receives the request and passes it to the web container which consists of servlets and web.xml( Deployment Descriptor).

This Deployment Descriptor map the page to its servlet.
After that servlet process the information and send back the response to the server and the server will return the response to the client.

What are the advantages of servlet :

→Servlets are server-side programs, So they can call only by the server, All the security measures are taken by the web-server also apply to a servlet, So it means servlets are secure.

→Servlets can execute more quickly as compared to scripting languages Because it compiled in byte-code that why it is faster than other scripting languages.

→Servlets are extensible.

→Servlets are server independent

→Servlets are portable.

Servlet Life Cycle Methods :

init() : → The Servlet.init() method is called by the container to indicate that this servlet instance is created.

service(): →The service() method is invoked to inform the servlet about the client request, It takes “HttpServletRequest” and “HttpServletRespose” objects as parameters.

destroy(): →The destroy() method executes only once in servlets life, And delete servlet instance.

JSP(Java Server Page) :

→JSP is used to build dynamic web pages

→JSP is built on top of the Java Servlet Specification.

→JSP pages are relatively quick and easy to build, And they interact seamlessly with Java servlet in a servlet container like Tomcat.

→JSP is internally convert into servlet.

→It can consist either HTML or XML with JSP actions and commands.

→It can be used as HTML page, like form registration page.

→We can share information across pages using request and response objects.

How sample JSP file look like:

This small code will internally convert into Servlet.

In the JSP if we want to write the java code the we need to use the some tags.

Let us understand the JSP tags.

Types of tags in JSP :

→Directive tag

→Declarative tag

→Scriptlet tag

→Expression tag

Directive tag:

After the internal conversion, If we want to declare something which can access by the whole file then we use this tag.

<%@ …………. %>

This empty space is for code or expression.

Declarative tag :

By using this tag we can declare variable inside the class and outside the service method.

<%!. …………….%>

This empty space is for code or expression.

Scriptlet tag :

If we want to add something in service method after internal conversion then this tag will help us.

<%………………..%>

This empty space is for code or expression.

Expression tag :

If we want to print something then we can use this tag.

<%=………………..%>

This empty space is for code or expression.

Advantages of JSP :

→JSP uses the java programming language, which is dynamic language and easily portable to other operating systems.

→We can write the servlet code into the JSP.

→JSP can also include the database connections into it.

→We can use the multi threading of java into it.

→It is very easy to maintain.

→It is easy to developers to show as well as process the information.

→As it is built on java technology, hence it is platform independent, not depending on any operating system.

Advantage JSP over Servlet :

→JSP allows tag based programming so extensive java knowledge is not required, On the other hand extensive java knowledge is required for Servlet.

→JSP is suitable for java and non java programmer, On the other hand Servlet is not suitable for non java programmer.

→JSP take care of exception handling, On the other for Servlet programmer need to handle the exception explicitly.

→JSP increases the code readability, On the other hand readability in Servlet is less.

→JSP is easy to learn and apply, However Servlet is not easy as compare to JSP.

Hope you find this helpful.

--

--