A program to understand how Java Servlet works!
Explore the docs »
View Demo
·
Report Bug
·
Request Feature
- About the Project
- Built With
- Getting Started
- Creating the Project from scratch
- Directly running this project
- Roadmap
- Contributing
- License
- Contact
The servlets provide an object-based, independent, web-based application building platform, without the limitations of CGI programs. Servlets have access to the entire Java APIs family, including the JDBC API to access business information. This project is mainly focused to understand how to use Java resources to improve our web-based applications in simple and easy steps.
Applications of this project with certain extensions:
-
Read clear data sent to clients (browsers). This includes data that can come from HTTP client system or an applet or the HTML form on a Web page.
-
Read the requested HTTP request data sent to clients (browsers). This includes cookies, media types and so on.
-
Process data and create results. This process may require database communication, making an RMI, installing a Web service, or entering a direct response to a computer.
-
Send explicit data (e.g., documents) to customers (browsers). This text can be exported in a variety of ways, including text (HTML or XML), binary (GIF images), Excel, etc.
-
Send anonymous HTTP response to customers (browsers). This includes telling browsers or other clients what type of document is being returned (e.g., HTML), setting cookies and cache restrictions, and other such functions.
This project was built using the following tools/frameworks:
To run the project successfully, you need to download and install the three tools/frameworks mentioned before. For ease of access, follow the below steps to complete the process easily.
- Apache Tomcat
- Go to Apache Tomcat
- Download the latest stable version of Apache Tomcat Server. For this project, I have used Apache Tomcat v9.0 Download
- When you are on this page - Apache Tomcat v9.0 Download, download apache-tomcat-9.0.39-windows-x64 by clicking on the option 64-bit Windows zip (pgp, sha512)
- Extract the files/ folders present in_ the downloaded files
- Open the extracted folder(apache-tomcat-9.0.39). Go to bin and install the tomcat9 application
- Eclipse IDE
- Go to Eclipse IDE Download
- Download the latest stable version of Eclipse IDE. For this project, I have used Eclipse Installer 2020-09 R
- On downloading the installer, extract if_ a zip file is downloaded
- Double click on the installer and install the Eclipse IDE for Enterprise Java Developers
- Accept the required permission(after you agree to them by going through the terms and conditions) and finish the process to successfully install the IDE
- Oracle Java Development Kit
- Go to Oracle JDK 8 Download
- Download the latest stable version of Oracle JDK. For this project, I have used Oracle JDK 8(jdk-8u271-windows-x64.exe). Before downloading you have to first create your account on the site and then click on the favorable installer
- On downloading the installer, extract if_ a zip file is downloaded
- Double click on the installer and install the jdk-8u261-windows-x64 jdk8
- Accept the required permission(after you agree to them by going through the terms and conditions) and finish the process to successfully install the IDE
- Create a Dynamic web project
- Create a servlet
- Add servlet-api.jar file
- Write the servlet code
- Configure the server
- Run the servlet
- Creating a Dynamic web project
1.1 Open Eclipse
1.2 File --> New --> Dynamic Web Project
1.3 Enter project_name --> Chose target runtime(Apache Tomcat v9.0 or the version you installed) --> Next --> Next
1.4 Check the checkbox of Generate web.xml deployment descriptor --> Finish
1.5 You will see the message - "Installing Dynamic Web Module Facet..."
1.6 On completion you can see your project folder created under the "Project Explorer" view frame.
- Creating a servlet in Eclipse
2.1 Right click on the project name
2.2 Click on New --> Servlet
2.3 Enter the class name(say "MyServlet") --> Next --> Next
2.4 Uncheck the methods that are not required and check the method names that you require in your project/java file --> Finish
2.5 "MyServlet.java" file will be created with the basic template. Now you can write your own code here.
- Adding servlet-api.jar file
3.1 Right click on the project name
3.2 Click on Build Path --> Configure build path
3.3 Under the <b>Libraries</b> tab, click on <b>Add External JARs</b> present in the right columnar frame
3.4 Browse and open the Tomcat folder that you downloaded --> lib folder --> select servlet-api.jar file --> Open --> Apply and Close
3.5 The required servlet api file would be added
- Writing the servlet code in Eclipse
4.1 Your program will still run with this basic template
4.2 You can write your own code in the java file
4.3 For simplicity you can consider this code for ease of understanding
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class MyServlet
*/
@WebServlet("/MyServlet")
public class MyServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public MyServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
response.setContentType("text/html");
PrintWriter output=response.getWriter();
output.print("<html><body>");
output.print("<h3>Hi! This is Kush Shah.</h3>");
output.print("<h3>And this output verifies that we have successfully deployed the servlet on tomcat v9.0 server.</h3>");
output.print("</body></html>");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
4.4 Ctrl+S to save the code
- Configuring the server
5.1 Right click in the blank area under the server tab in the pane present below the code area.
5.2 Click on New --> Server
5.3 Select the server you installed(I chose Tomcat v9.0 Server) under the server type --> Next
5.4 Click on Add All --> Finish
5.5 The required server is configured and ready to host a program
- Running the servlet
6.1 Right click on the project name
6.2 Select Run As --> 1 Run on Server
6.3 Select <b>Choose an existing server</b> --> select the Tomcat server under the localhost folder --> Next
6.4 Click on Add All --> Finish
6.5 You will encounter <b>HTTP Status 404 – Not Found</b>. Dont worry. It is because the local address is just accessing the folder and not the java file.
6.6 To solve this issue, typw the name of the file after the '/'. So at the end it will look like --> <b>https://localhost:8080/understanding/file_name</b>
6.7 Press Enter and you will be able to see the desired output...!
It is assumed that before running the project you have the already created the required local environment.
- Download the zip file of this project from the Code button of the project
- Extract the files
- Open the Eclipse IDE
- Go to File --> Import
- Open the dropdown of General folder --> Existing Projects into Workspace --> Next
- Browse and select the downloaded and extracted folder of this project --> Check the option "Copy projects into workspace" --> Finish
- Your complete project would be ready to be deployed on the server.
- Right click on the project and follow the above step #6
See the open issues for a list of proposed features (and known issues).
If you like the project then kindly give a star to the repo. Any contributions you make are greatly appreciated.
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature
) - Commit your Changes (
git commit -m 'Add some AmazingFeature'
) - Push to the Branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
Distributed under the MIT License. See LICENSE
for more information.
Project Link: Java Servlet