|
| Advance Java | IndianTechnoEra |
Aim:
Create and deploy a java servlet to print welcome on the browser and run it using the apache tomcat server in eclipse.
What is Servlet?
Servlet is a technology which is used to create a web application.
Servlet is an API that provides many interfaces and classes including documentation.
A servlet is a Java programming language class that is used to extend the capabilities of servers that host applications accessed by means of a request-response programming model.
Although servlets can respond to any type of request, they are commonly used to extend the applications hosted by web servers.
A servlet is a small Java program that runs within a Web server. Servlets receive and respond to requests from Web clients, usually across HTTP, the HyperText Transfer Protocol.
To implement this interface, we can write a generic servlet that extends javax.
There are two types of servlets as
- Generic and
- HTTP servlets
What is Tomcat?
Tomcat is considered a web server instead of an application server because it functions as a web server and Servlet container.
Apache Tomcat is a long-lived, open-source Java servlet container that implements core Java enterprise (now Jakarta EE) specifications, including the Jakarta Servlet, Jakarta Server Pages, and Jakarta WebSocket specs.
Process:
Step 1: Download and install Apache Tomcat and configure
Step 2: Create Project
File > New > Dynamic Web Projects
Give the details like
Project name
Target runtime (like Apache Tomcat v9.0 that is installed)
Dynamic web modules version (like 4.0)
Click on Next
Remove pre added path (src\main\java)
Click ‘Add Folder’ and write ‘src’
Then click on Next
Remove Content directory: src/main/webapp and add only ‘webapp’
Check mark on ‘Generate web.xml deployment descriptor’
Step 3: Create servlet java file in project
Right click on project
New > Servlet
Give the details like
Java package name ( com.welcome_servlet)
Class name (Welcome_Servlet)
Step 4: Access created servlet java file and write code
Project > Java Resources > src > Package (com.welcome_servlet) > Welcome.java
Remove comments and unnecessary code and start write your own code
There will be deGet() method that will display the served name address
Here we will create an object with PrintWriter class and write html web code with object following println method like
PrintWriter out=response.getWriter();
out.println("<html> <head> <title> Servlet 1 </title> </head> <body> <h1>Welcome to the Servlet </h1> </body> </html>");
Step 5: Run the project
Right click on the project or java file if single one
Click on Run As
Choose 1 Run on server
Select Tomcat v9 server at localhost
If running single java then hit on Finish
or
Then click on next in case of run whole project
Select project from Available option and click on Add to put into Configure section
Then click on added project and click on Finish
Input:
Output:
|
| Java Servlet to Print Welcome |

