Deploying a Servlet in Apache Tomacat Server

Deploying a Servlet in Apache Tomcat Server

 

 

Steps to Deploy a Servlet in Apache Tomcat Server

 

§  Create a directory structure under Tomcat for your application.

o   Inside the webapps folder of apache tomcat server create a folder (Example WT).

o   Inside this new folder (WT) create a folder called WEB-INF

o   Inside WEB-INF create a folder called classes.

o   Create a web.xml file in WEB-INF

o   The html and jsp files should be saved in WT folder.

 

Directory Structure

 

§  Copy the servlet-api.jar file from …\apache-tomcat-9.0.24\lib to …\jre1.8.0_221\lib\ext

 

§  Set path  variable to Java compiler and classpath variable to the  servlet-api.jar file

(path=C:\Program Files\Java\jdk1.8.0_221\bin)

(classpath=C:\Program Files\Java\jre1.8.0_221\lib\ext\servlet-api.jar)




§  Write the servlet source code and Compile. Copy the .class file to the classes folder created in tomcat server. (Example – WelcomeServlet.java)

 

§  Create a deployment descriptor for the servlet in web.xml.

Example:

                   <servlet>

                             <servlet-name>WelcomeServlet</servlet-name>

                             <servlet-class>WelcomeServlet</servlet-class>

                   </servlet>

 

                   <servlet-mapping>

                             <servlet-name>WelcomeServlet</servlet-name>

                             <url-pattern>/WelcomeServlet</url-pattern>

          </servlet-mapping>

 

§  Start the Tomcat server and call the servlet from a web browser.

 

web.xml file:

 

<?xml version="1.0" encoding="ISO-8859-1"?>

<web-app version="3.0"

          xmlns="http://java.sun.com/xml/ns/javaee"

          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

 

          <servlet>

                   <servlet-name>WelcomeServlet</servlet-name>

                   <servlet-class>WelcomeServlet</servlet-class>

          </servlet>

 

          <servlet-mapping>

                   <servlet-name>WelcomeServlet</servlet-name>

                   <url-pattern>/WelcomeServlet</url-pattern>

          </servlet-mapping>

  

</web-app>

 

 

No comments:

Post a Comment

Don't be a silent reader...
Leave your comments...

Anu