Showing posts with label tomcat server. Show all posts
Showing posts with label tomcat server. Show all posts

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>

 

 

Installing Apache Tomcat Server

Installing Apache Tomcat Server

 

 

Steps to install Apache Tomcat Server

 

§  Create a directory

o   (In c: as WTServlets)

 

§  Download and Install Tomcat in above folder

o   In  http://tomcat.apache.org find latest version of tomcat. Under Binary Distributions Core find the "zip". Download and extract to WTServlets folder

 

§  Download and install the latest versions of jdk and jre. Create an Environment Variable JAVA_HOME and JRE_HOME as below.

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

(JRE_HOME= C:\Program Files\Java\jre1.8.0_221)



§  Configure the Tomcat Server

o   The Tomcat configuration files, in XML format, are located in the "conf" sub-directory of the installed Tomcat directory

o   Configure the TCP port number and enable automatic reload and listings.

 

§  server.xml – Set the TCP Port Number

In <Connector port="xxxx" protocol="HTTP/1.1"           connectionTimeout="20000"           redirectPort="8443" />

Set xxxx value (Example: port=”8088”)

 


 

§  context.xml – Enabling Automatic Reload

add the attribute reloadable="true" to the <Context> element to enable automatic reload after code changes.

<Context reloadable="true">
   ......
   ......
</Context>

 

§  web.xml – Enable Directory Listing

enable directory listing by changing "listings" from "false" to "true" for the "default" servlet

<servlet>
  <servlet-name>default</servlet-name>
  <servlet-class>org.apache.catalina.
servlets.DefaultServlet</servlet-class>
  <init-param>
    <param-name>debug</param-name>
    <param-value>0</param-value>
  </init-param>
  <init-param>
    <param-name>listings</param-name>
    <param-value>true</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
</servlet>

 

 

§  Start Tomcat Server

o   In command prompt go to c:\WTServlets\apache-tomcat-9.0.24\bin type startup

o   c:\WTServlets\apache-tomcat-9.0.24\bin>startup

xxxxx INFO [main] org.apache.catalina.startup.Catalina.start Server startup in […..] ms

 

§  Start client access to server

o  Open any browser and access the tomcat server by adding http://localhost:8088 at the address bar


§  Shutdown

o   In command prompt go to c:\WTServlets\apache-tomcat-9.0.24\bin type shutdown

o   c:\WTServlets\apache-tomcat-9.0.24\bin>shutdown