You are on page 1of 7

Date:-16-01-2024

Experiment :-5
Aim: Create and deploy a java servlet to print welcome on the browser and run it
using the apache tomcat server in eclipse.
Step 1 : Create a new project and the servlet class.

In your eclipse IDE create a new Dynamic web project

New ->Other->Web->Dynamic Web Project->Next-> (Give any Project name ‘Test1’) ->Finish->Click Open
Perspective if option appears for JavaEE perspective.
Date:-16-01-2024

In the Java Resources src/main/java create New ->Package name Test1

Inside Test1 create New -> Servlet

Name it TestServlet

Click Finish.
Date:-16-01-2024

Following code will be auto-generated for your servlet :

Code:-
package aditya;

import jakarta.servlet.ServletException;

import jakarta.servlet.annotation.WebServlet;

import jakarta.servlet.http.HttpServlet;

import jakarta.servlet.http.HttpServletRequest;

import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

import java.io.PrintWriter;

/**

* Servlet implementation class DemoServlet

*/

public class DemoServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

/**

* @see HttpServlet#HttpServlet()

*/

public DemoServlet() {

super();

// TODO Auto-generated constructor stub

/**

* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)


Date:-16-01-2024

*/

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws


ServletException, IOException {

// TODO Auto-generated method stub

PrintWriter pw=response.getWriter();

response.setContentType("text/html");

pw.println("<h1>Welcome here Aditya and Snehit!</h1>");

/**

* @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);

}
Step 2: Download and include the servlet-api jar file in your project.

Download the zip file for apache tomcat 10.1 for your respective environment.

Extract the downloaded zip file at your desired location.

Right click on the project name -> Properties -> Java Build Path ->ClassPath->Add External JAR’s

Browse to the location where you extracted the downloaded files. Inside the lib folder you will find servlet-api jar
file. Select it. Click apply and close.
Date:-16-01-2024

The errors in your auto generated code should be cleared after this step.

Step 3: Write the code to print welcome in your servlet.

Open your WelcomeServlet and edit the doGet method as follows:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,


IOException {

// TODO Auto-generated method stub

PrintWriter pw=response.getWriter();

response.setContentType("text/html");

pw.println("<h1>Welcome here Aditya and Snehit!</h1>");

Step 4: Create the deployment description in web.xml file.

Expand your project directories.

Right click on the web-inf folder and create new file. Name it web.xml.
Date:-16-01-2024

In the web.xml write the following xml description or it would be already written:

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://jakarta.ee/xml/ns/jakartaee" xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee
https://jakarta.ee/xml/ns/jakartaee/web-app_6_0.xsd" version="6.0">

<servlet>

<description></description>

<display-name>DemoServlet</display-name>

<servlet-name>DemoServlet</servlet-name>

<servlet-class>Aditya.DemoServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>DemoServlet</servlet-name>

<url-pattern>/DemoServlet</url-pattern>

</servlet-mapping>

<display-name>DemoServlet</display-name>

<welcome-file-list>

<welcome-file>index.html</welcome-file>

<welcome-file>index.htm</welcome-file>

<welcome-file>index.jsp</welcome-file>

<welcome-file>default.html</welcome-file>

<welcome-file>default.htm</welcome-file>

<welcome-file>default.jsp</welcome-file>

</welcome-file-list>

</web-app>

Step 5: Create and start the apache tomcat server.

In your console go to servers tab.

Click the ‘no servers are available…’


Date:-16-01-2024

Select the apache tomcat server of your current version. Click Next->Add All ->Finish

In the servers tab, tomcat server will appear. Right click on the server and click on start.

Step 6: Run your project on the tomcat server.

Right click on the project. Run As -> Run On Server.

If tom cat server does not appear then click on manually add a server and select apache tomcat server.

If it already appears, the select apache tomcat server.

Your browser window will open and welcome will be displayed on the screen.

You might also like