Complete JSP(Java Server Page) Tutorial with examples and real-time practice sessions. JPS tutorial for beginners. If you start learning advance java then this JSP tutorial will help you to perform some real-time examples to develop a Java web application using JSP.

Before, Start the JSP tutorial, I will recommend checking the Java tutorial. Because JSP is another Library or API of Java so you must need some hands-on practice on Java before starting the JSP tutorial.

What is JSP?

JSP is a short form of Java Server Page. JSP is a technology for server-side programming as well as client site programming.  JSP is used to create dynamic web applications. Using JSP we can add Java code inside HTML using JSP tags.

JSP can handle server-side requests and responses. JSP allows us to write HTML code and well as Java logical code. Using JSP, we can easily separate Presentation and Business logic as a web designer can design and update JSP pages creating the presentation layer.

In this JSP tutorial, we will focus on what is JSP and why we use the JSP page. and other basic terms related to the Java server page(JSP).

Important key points for JSP

  • In JSP you can write HTML code as well as Java code in JSP files.
  • JSP is faster than other programming language.
  • In MVC architecture JSP will be part of the view sections.
  • JSP is a part of  JavaEE. It’s used to develop high-security web applications Like enterprise applications.
  • JSP allows JavaBeans object and database code.
  • We can create any database connection in JSP.
  • JPS allows to Page Includes that will save time.
  • We can write complete java code in JSP like collection, multithreading, exception handling, etc.

JSP Tutorial

JSP tutorial is divided into multiple parts as per the topics and examples. So in this JSP article, I will cover the Introduction of the JSP, Environment setup for Java web application, First JSP program and run it over the tomcat server. that will be followed by other JSP topics that are listed below. For deep dive into JSP follow check out the END to END JSP tutorial.

Environment to develop Java web application using JSP

In order to get started with JSP, you will require an environment to set up to create a web application in Java. You should have the following applications installed:

  • JDK(Java Development Kit)
  • Java Application Server
  • IDE for Java (You can use any IDE such as Eclipse, NetBeans, or IntelliJ)

If you want to know How to set up the environment check this article https://codedec.com/tutorials/how-to-set-environment-to-create-jsp-application/

Write your first program in JSP

Create index.jsp file in your favorite IDE tools like Eclipse or Netbeans.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"

pageEncoding="ISO-8859-1"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title>Insert title here</title>

</head>

<body>

<%

out.println("My First JSP code");

%>

</body>

</html>

In the Jsp file we can write java code or business logic also only need to write that java code inside <%     %> tag.

<%

out.println("My First JSP code");

%>

JSP Life cycle

The Life cycle of JSP means a series of changes Java Server Pages go through from beginning till the end. The changes involved here are:

  • Conversion/ Translation of JSP Page.
  • Compilation of JSP Page.
  • Loading of Class.
  • Instantiation of generated Servlet.
  • Initialization
  • Servicing of request.
  • Destruction by the container.

If you want to know in detail the life cycle of JSP check this article https://codedec.com/tutorials/life-cycle-of-jsp/

Tags in JSP

Tags play a vital role for Java Server Pages. It creates a container for Java code, adding it, and providing separation of dynamic content from static content. Different Tags available in JSP such as:

  • Scriptlet Tag.
  • Expression Tag
  • Declaration Tag
  • Comment Tag.

If you want to know in detail check this article https://codedec.com/tutorials/what-are-jsp-scripting-elements/

Implicit objects in JSP

The Implicit Objects are Java Objects that are created by the Container in JSP and we as a developer call them without having been explicitly declared.

A list of the implicit objects is given below:

  • out
  • response
  • request
  • config
  • application
  • session
  • pageContext
  • page
  • exception

To know more about these implicit objects check this article https://codedec.com/tutorials/what-is-jsp-implicit-object/

Directives in JSP

JSP Directives are the special message given to the container at the time of the translation of the JSP page to its respective Servlet. It is represented using <%@%>. It consists of the following directive:

  • page directive.
  • include directive
  • taglib directive

To know more about the JSP Directive check this article https://codedec.com/tutorials/what-are-jsp-directives/

Action Tags in JSP

Here, Action Tags simply means what action to be done while the page is being accessed and to control the behavior of the Servlet engine. For Example, If we want to include a file or forward it to the next page, we will use Action Tags.

There are many types of Action Tags available. You can check here to know the types https://codedec.com/tutorials/action-tags-in-jsp/

Expression Language in JSP

Expression Language was introduced in JSP version 2.0. It helps us to evaluate the expression placed inside it. There is a number of implicit objects provided by EL.

For Example, If we want to perform addition using EL, we just need to use the ${} symbol.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h2>By using Expression Langauge</h2>
${25+25}
</body>
</html>

Check this to know more about it https://codedec.com/tutorials/expression-language-in-jsp/ 

Exception Handling in JSP

As we know, there are multiple implicit objects in JSP. the exception is one of them. To Handle exceptions in JSP we can use the <error page> element in the web.xml file.

To know more about Exception handling do check this article https://codedec.com/tutorials/exception-handling-in-jsp/

JSTL in JSP

JSTL stands for Java Server Pages Standard Tag Libraries. It basically helps to simplify the JSP development. It consists of multiple tags such as:

  • Core tags
  • Formatting tags.
  • SQL tags.
  • XML tags.
  • JSTL functions.

To know the working of the above tags do check this article https://codedec.com/tutorials/jsp-standard-tag-libraryjstl-in-jsp/

Custom Tags in JSP

Custom Tags are those tags that are defined by the user for its own requirement. Here, we just need to extend TagSupport Class and override the doStartTag() method. To know, How we can make a custom tag check this article https://codedec.com/tutorials/custom-tags-in-jsp/

Client Request in JSP

As we know, whenever the request comes from the client, the client not only sends the request but also sends more information along with it and all this information is present in the header part of the incoming request. So, in order to get the header part information we have a request object in JSP.

If you want to see an example check this article https://codedec.com/tutorials/client-http-request-in-jsp/

Server Response in JSP

After the server gets the request from the client, it’s now the server’s responsibility to respond to the query. Now, when the server responds to the client along with the response it sends some information in the header. To know what are the headers check this article https://codedec.com/tutorials/server-http-response-in-jsp/

Form Processing in JSP

In JSP, form processing is done using the two methods like GET and POST. GET is the default method to send information from browser to server. Similarly, the POST method is also used to send information from client to server but with a more reliable method. Check this article to know more about Form Processing https://codedec.com/tutorials/how-to-process-form-data-in-jsp/

Cookies Handling in JSP

In JSP, to handle cookies, we need to create a Cookie where Cookies are data packets that the computer receives, then sends back without altering it. Then, set the maximum age of the cookies, at last, send the cookie in the response object.

If you want to know more about Cookies Handling check this article https://codedec.com/tutorials/how-to-handle-cookies-in-jsp/

Filter in JSP

In JSP, just like Servlet, Filter is classes. It is used for pre-processing and post-processing of objects. For example, we need to validate the form before going to Server, So we can use Filters. If you want to know more about Filters check this article https://codedec.com/tutorials/how-to-use-filter-in-jsp/

Session Tracking in JSP

In JSP, the session is used to store information that we will use across multiple pages until the user is active. We can use the implicit object session to set, get, and remove attributes.

Check this tutorial to learn more about Session Tracking in JSP https://codedec.com/tutorials/how-to-handle-sessions-in-jsp/

Login & Registration in JSP

Registration and Login play a vital role in any web application or project. Here, we will use the MYSQL database to store registration information. Just create the insert method to persist data into the database and validate the method to validate user login.

If you want to learn how to create Registration and Login Functionality using JSP. Check this for a complete example https://codedec.com/tutorials/registration-and-login-example-in-jsp-with-mysql/

Login and Logout In JSP

Login and Logout is an important part of any application. Here, we will use the Session Object in JSP to handle login and logout. Here, we will set the session as soon as a user logins and will invalidate it as soon he/she logs out.

Similarly, If you want to know How it is done then check this example https://codedec.com/tutorials/login-and-logout-example-in-jsp/

File Uploading in JSP

File Uploading is an important task in any web application. In JSP, we can upload text, binary, image, or document files. Following are the important points to be considered while uploading files using JSP.

  • Here, the first enctype should be set to multipart/form-data.
  • Only the POST method will work.

To know a complete example of How to Upload File in JSP check this article https://codedec.com/tutorials/how-to-upload-file-in-jsp/

Downloading File in JSP

Downloading a file in JSP will have the following sets of steps involved:

  • The First step is to locate the File path.
  • Set a response as APPLICATION/OCTET-STREAM because APPLICATION/OCTET-STREAM stands for binary data(it is always good if we specify the actual file type).
  • Set Header as Content-Disposition.The content-disposition filed is added to specify the presentation style.
  • an attachment content disposition, in this case, is not displayed automatically and requires some form of action from the user to open it(in this case we say don’t open the file instead just save it ).

To know a complete example of How to Download File in JSP check this article https://codedec.com/tutorials/how-to-download-file-in-jsp/

CRUD Operation in JSP

CRUD stands for CREATE, READ, UPDATE and DELETE Operation. It is an important part of any web application. Below are the steps to create a simple Java application to perform the CRUD operations:

  • Create a Database and Table.
  • Create a Dynamic or maven project in Java.
  • Create a model(Dao classes)
  • Design pages in Jsp or HTML.
  • Servlet classes to handle the request and responses.

If you want to learn How to create a CRUD Operation in JSP do check this article https://codedec.com/tutorials/crud-operation-using-jsp-servlet-and-mysql/

Practise Task in JSP tutorial