Tuesday, 6 January 2026

Simple JSP and Servlet Java Tutorial

JSP(Java Server Pages) allows to write dynamic web content, it holds the View logic of the MVC architecture and Servlet handles the user's HTTP request then processes the request and responds with the HTTP Response, it holds the Controller logic of the  MVC architecture as in our below example the servlet takes two numbers from the request, process it calculating the sum and then responding with the HTTP response(the calculated sum).

1. First step requires creating a Dynamic Web Project, if the option is not available as in the below image try installing the Eclipse IDE with Eclipse IDE for Enterprise Java and Web Developers follow the steps as in the link

1.1 Navigate to the Dynamic Web Project as in below image directly















In some Eclipse IDE it requires navigating to Other option and then navigating to Dynamic Web Project















Try searching for Dynamic Web Project and then select the option















1.2 Next it requires to enter the project name and then selecting Finish button



















2. Next it requires navigating to mvnrepository.com and downloading some jar files

Here are some direct links to download the jar files, by downloading these jar files you can directly jump to step 3.

However if you prefer to manually follow the basic required steps below are the following steps:

2.1 Navigate to mvnrepository.com













2.2 Search jakarta_servlet-api in the search bar

















2.3 Click on Jakarta Servlet and it will show the output as in the below image or directly navigate to this url https://mvnrepository.com/artifact/jakarta.servlet/jakarta.servlet-api
















2.4 Next it requires to scroll down and clicking the 6.1.0 version as in below image:





















2.5 Next it requires to download Jakarta Servlet 6.1.0 jar file taking reference of the cursor as in the below image:



















2.6 Next it requires another dependency named JSP API, now search JSP API in the search bar and the output can be seen as in the below image, navigate to the third dependency named JSP API:




















2.7 Upon navigating the output webpage can be seen as in below image:





















2.8 Next it requires to scroll down and selecting the right dependency version 2.0:





















2.9 Once navigating to the right dependency webpage, it requires to download the jar file as the cursor points in the below image:



















3. Now we have the required dependencies(jar files) downloaded in our system, next steps requires to configure the build path by adding the dependencies(jar files) to the created dynamic web project

3.1 Next it requires navigating to the Eclipse IDE and right clicking the created project, in our case it's jsp-and-servlet-tutorial and selecting the "Build Path" context menu option and then selecting the "Configure Build Path" option























3.2 Once the Properties window is opened, sometimes it navigates to tab other than "Libraries", like in below image it has navigated to "Order and Export" tab, if it's so switch the tab to "Libraries" as cursor points in the below image




















3.3 Once the "Libraries" tab is switched, next it requires to select the "Classpath" dropdown and selecting the "Add External JARs" option as the cursor points in the below image




















3.4 Upon clicking the "Add External JARs" option, a new window will open, next navigate to the folder where the jar files are downloaded, commonly it will be the "Downloads" directory, in our case it's "jsp-servlet-dependencies" directory within the "Downloads" directory, next select both dependencies and click "Open" button as in image provided below:























3.5 Next step requires to click "Apply and Close" button once the dependencies are selected in the classpath




















4. In this step we need to create a JSP file and then will try running the application on Tomcat Server

4.1 Navigate to the "jsp-and-servlet-tutorial" project directory in the Project Explorer and then move to the "src > main > webapp" and right click the webapp directory, then select the "New" option and select the corresponding "JSP File" option as in the below image























4.2 A "New JSP File" window will open, next enter the file name as index.jsp and then click Finish button






























4.3 Once the index.jsp file is created, make some modifications in the file by adding <h1>Welcome to JSP!</h1> in the body tag as provided in the below image
















5. Next step requires to add the server named "Tomcat" in the Eclipse IDE and then running the application on the Server, if there is already a server installed in your IDE make sure the version is greater than or equal to 10

5.1 Next it requires navigating to the "Servers" tab as the cursor points in the below image if the "Servers" tab is not there follow the next step else jump to 5.3 step



















5.2 Try navigating to the Window > Show View > Servers, if Servers option is not there try searching the Servers view option in the Other option



















5.3 Once Servers view tab is opened up, we will try creating a new server



















5.4 Once the New Server tab is opened up, next it requires to select the server type and version































5.5 Expand the Apache option and scroll down a bit and there we see the Tomcat versions, try selecting a version greater than version 9































5.6 Next try the Download and Install button































5.7 Once we reach the Download and Install dialog box, next accept the terms and license agreement and click on Finish button



























5.8 Next try creating a new folder 























5.9 Next select and open the created directory as in the below image it is tomcat_11_server
























5.10 Next click Open button
























5.11 Once it finishes up loading, the Finish button can be clicked and the Tomcat server can be seen installed in the Servers tab




























6. Upto this point we have successfully created a JSP file and have setup of Tomcat Server version greater than 9(crucial because we have jakarta-servlet-api instead of java-servlet-api which supports tomcat version less than 10), now its time to run the application

6.1 Navigate to the index.jsp file and try running the app by clicking the green button as the cursor points in the below image


















6.2 Select the installed Tomcat version and click Next and then Finish button






























6.3 Once the server is up and running, it automatically opens up the link in the browser if that's not the case try manually navigating to the link as in the below image

















7. In this step we create a form which accepts two values which is sent to the server and the servlet accepts the request parameters and responds the sum as output

7.1 The form is to be created as in the below image in the index.jsp file having two text inputs and one submit button














7.2 Upon running the app the output can be seen with two text inputs and one submit button


















7.3 Next step requires navigating to the Project Explorer in the src/main/java directory

































7.4 Inside Java Resources directory, inside src/main/java we need to create a package by following the steps as in the below image


























7.5 Enter the package name as "com.servlet_demo" and press Finish button























7.6 Next step requires creating a class under the newly created package, below image directs how to and where to create the class























7.7 Enter the name of the class as ServletDemo, then press Finish button





























7.8 Next edit the newly created class file in our case it is ServletDemo with the code provided as in the below image
























7.9 Navigate to the index.jsp as in the below image
















7.10 Next run the JSP file, select the server as in the below image and click Next then Finish
































7.11 Once the server starts it will automatically open the JSP file in the browser, if its not this case try opening the served file with the url given as in the below image, and try inputting any two values in the form and hit the Submit button






















7.12 The resultant servlet success output can be seen once the form is submitted




No comments:

Post a Comment