Introduction to JSP and Servlet

Introduction

JavaEE gives the programmer very rich sets of API to use directly out of the box without implementing them manually. One such API is a servlet that helps Java programmers create a java component that acts as a server to its client on a remote location. We use servlet technologies most of the time for web development to develop dynamic web pages. Servlet and JSP are fairly old technologies, but they are the building blogs of the current java web ecosystem. All of the latest and greatest technologies such as Spring use servlet and JSP under the hood. So even if you do not write much Servlet code in your java career. It is still better if you understand what happens in the background of your java application.

Servlets

What is servlet?

Servlet is simply a Java class that has the capabilities to act as a server to a particular request from the client. Generally, a servlet runs inside of a container that gets a request from the client and performs an appropriate operation on a servlet. The container maintains the connection between the servlet and a client. Some of the famous containers for servlets are tomcat and glassfish. Java servlet does not have the main method instead, it has something called lifecycle methods. Which gets called by the container at appropriate timings. Let us see all of them in the below section.

LifeCycle methods in servlet

The life cycle of a servlet is controlled by the container (like a tomcat). Servlet acts as a wrapper to a servlet. Every client request goes to the container which it forwards to the appropriate servlet.

Fig. Lifecycle of servlet.

There are three methods present in the Servlet interface, which gets called by the servlet container.

  1. init(): Gets called to notify the servlet that it is being instantiated.
  2. destroy(): Gets called to notify the servlet that it is no longer needed.
  3. service(): Gets called whenever there is a new request comes for that servlet.

Java Server Pages(JSP)

What is JSP?

JSP stands for Jakarta Server Pages(Formerly Java Server Pages). It is an extension added to a servlet to develop web pages. JSP is HttpServlet Specific, meaning we cannot use JSP for other purposes than to create web applications or more specific dynamic web pages.

In a servlet, it is very tedious to create a dynamic web page. Because in a servlet we write HTML code using PrintWriter object, Which is a very error-prone method to create a web page. And more often than not. Front end programmers don’t know the backend technologies such as a servlet or Java Programming Language. To make the job of front end developers easy JSP is introduced. It looks just like any normal HTML page would look like, but it is a Servlet with added functionality.

JSP consist of HTML tags and JSP specific tags. We can also write java code inside of a JSP using those tags. Let’s see them below.

JSP Scripting

In JSP we can manipulate its Implicit Objects, Servlet Code, Java Related functionality, etc. by using these four tags. Namely Scriptlet, Expression, Declaration, and Directive tags. Well, see all of them below one by one

Scriptlet tag

The java code written inside of these tags gets, copied inside of a service method of a Servlet. Which gets generated at run time from the JSP in which it is written.

<% any normal java code will work here. but declarative code will not %>
ex.
<% if(password.length() < 6) { errorMessage = "Enter correct password"} %>

Expression Tag

Here we generally write variables whose value we want to render onto the web page. the code inside of these tags gets copied into printwriter.print() method.

<%= veriable-name %>
ex.
<p>First Name of a user is := <%= firstName %></p>

Declaration Tag

Declaration tag works exactly similar to the scriptlet tag with just a minor behavior change. Instead of placing code inside of service() method, it puts code at the class level. So by using this we can write declarative code such as methods, class-level variables, etc.

<%! declarative code here %>
ex.
<%!
public boolean validateEmail(String email) {
// code for validation.
}
%>

Directive Tag

The directive tag defines elements that relay the message to the JSP container(tomcat) and affect the compilation of JSP to a servlet. We can define three types of Directive include, page, and taglib.

  • Page: This directive type is used to define the page related properties and tell them to the Servlet container. We can define lots of properties inside of this directive. Such as declaring an error page when an error occurs, import java package, etc.
<%@page errorPage="error.jsp" /> // here this page gets called whenever error occurs.
  • Include: This is used to insert the content of another file into a JSP. It can also be used to insert the already created JSP pages inside of the current JSP.
<%@ include file="filepath"%>
  • Taglib: This directive type is used to link to the custom tag file or the JSTL library. We generally don’t want to write the java code inside of JSP hance by using this tag we can map the java code to simple HTML-like tags so that we can have the same functionality by using tags instead of using java code.
<%@ taglib prefix = "mytag" uri="http://www.example.com/custlib"%>

Conclusion

Servlet and JSP are essential building blocks of modern java based web applications. Learning this technology will help you understand the mechanics of an application to a core. This blog is not enough to master the topic so, dig in if you want to know more. And Thank you for reading.

Links to resources

Software Engineer