Professional Documents
Culture Documents
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.
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
Name it TestServlet
Click Finish.
Date:-16-01-2024
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;
/**
*/
/**
* @see HttpServlet#HttpServlet()
*/
public DemoServlet() {
super();
/**
*/
PrintWriter pw=response.getWriter();
response.setContentType("text/html");
/**
*/
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.
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.
PrintWriter pw=response.getWriter();
response.setContentType("text/html");
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:
<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>
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.
If tom cat server does not appear then click on manually add a server and select apache tomcat server.
Your browser window will open and welcome will be displayed on the screen.