Skip to the content.

JSP Learning Notes

JSP:

JSP stands for Java Servlet Pages. JSP can be used to create application in Java which are web-based.

Make sure to add javax-servlet library in your project to enable Servlet.

Let’s start with what happens when a request hit the browser window.

We can decide what Servlet/JSP to call after a request hit the browser, we can do this using web.xml (Servlet mapping) or using WebServlet annotation or can be left to JSP to implicitly handle the request and redirect to appropriately named JSP page.

Defining a servlet:

Think of servlet as a Java class which has the logic of what to do when a specific URL is hit.

We can define a Servlet as as Java class which extends HttpServlet.

public class AddServlet extends HttpServlet { ... }

In this class we can override the doGet(), doPost(), etc, methods which will handle specific type of requests.

Once the servlet is defined we need to decide what URL will hit this servlet.

We can do this in multiple ways.

Using web.xml file:

We can use to define our servlet and to decide when it should be called.

<servlet>
    <servlet-name>add</servlet-name>
    <servlet-class>com.github.swapnil.AddServlet</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>add</servlet-name>
    <url-pattern>/add</url-pattern>
</servlet-mapping>

This will call the above servlet when ‘/add’ URL is hit. (Remember, we can pass any data from the HTML page - from user - and retrieve it in the Servlet)

Using @WebServlet annotation:

XMLs are hard to work with as I find them very verbose. We can avoid this by using annotations instead.

@WebServlet("/add")
public class AddServlet extends HttpServlet {

This will tell Java to use AddServlet for /add URL.

Or we can use a JSP page called add.jsp and Java will automagically redirect the /add URL to add.jsp page.

Using MVC with JSP:

Normally you should not define any business logic in the JSP page as it should be only used to render the view layer.

The web-request will go to the Servlet and process the data and then go the view and display the processed data.

The JSP page will be converted to a Servlet by Java.

Anatomy of a JSP page:

A JSP page has four sections:

Built-in Objects:

Some objects are implicitly defined by JSP and you can use it straight away without the need of defining it first.

These are the implicit objects.

JSTL

JSTL is JSP Standard Tag Library,

The idea is, instead of writing in pure Java code in JSP pages, it is better for the front-end developers to write in tag-based code (as the front-end developers would not be familiar with the Java language)

For example, instead of doing <% out.println(request.getAttribute(“name”)) %> we can do <c: out value = ${name}>

To import the JSTL library in a JSP page use, ‘<%@ taglib uri=”http://java.sun.com/jsp/jstl/core” prefix=”c” %>’

Filters

Whenever a client sends a web-request say we want to log it, we can do this in two ways: Either define the logging code in the Servlet that handles the request OR create a filter that intercepts a URL (regex pattern) and executes some business logic before forwarding to the Servlet. Doing the second one is beneficial, as we can then use the same filter and plug it in different URL pattern and voila we will have logging for a new request as well.

No need to edit the code inside Servlet.

FilterChain

When we have multiple filters we need to decide the order of execution of the filters. This is done in the web.xml file.

There are three methods for the filter:

Using SpringBoot Controller instead of WebServlet.

https://www.youtube.com/watch?v=uBjeeUfnhUM&ab_channel=JavaGuides