How to Setup and Execute Java Servlet Program in Localhost Tomcat Server?

Advertisement

Hello Everyone, We are back with some interesting concepts. Java Servlet is a programming language whose work is similar to PHP. It’s a Server-side Scripting Language, and Here, you can Learn to Execute a single Servlet Program.

Through this tutorial, you will learn about these topics

  1. Execute Java Servlet Program
  2. Install JDK and Xampp
  3. Setup all Environment Variables
  4. Generate a Class file for the Servlet Java Program
  5. Where Should I Place the Created Class File?
  6. What Changes Need to do in the web.xml file and where is that file?
  7. How to Access My Class File?

Requirements:

  • Java Development Kit
  • Tomcat Server [ We Use Xampp Package]
  • Browser

Overview:

  • Install XAMPP and then Install JDK. Set the environment variable to compile regular Java programs using the command prompt. It must point to the lib folder of JDK.
  • Setup JAVA_HOME at your environment variable that must be pointed to your Java folder
  • Setup CATLINA_HOME at your environment variable that must be pointed to tomcat folder (That’s inside the xampp folder. Xampp is in C:\)
  • Compile the java servlet program and generate the class file. Use javac command to generate the class file. It requires the servlet-api.jar file to create the class file. It’s in the lib of tomcat so if you use the xampp its path is like this “C:\xampp\tomcat\lib\servlet-api.jar” After that give your java file path. So the syntax of class file generation is like this – “javac –cp C:\xampp\tomcat\lib\servlet-api.jar [path of your java file]”. Class file will be generated at the same location of your java file.
  • Now copy the generated class file to the classes folder of the Tomcat server. The path is like this “C:\xampp\tomcat\webapps\examples\WEB-INF\classes”
  • Now edit web.xml file. Which is in the very back folder of classes.
  • Now start the tomcat server by pressing CATLINA_START.bat file
  • Open the browser and Run the program. Url will be like this http://localhost:8080/….

Read Also:


Here is the step-by-step process:

  1. Install JDK – Download Link Click Here
  2. Install Xampp Web Server – Download Link Click Here
  3. Now set Environment Variables
    • Go to Environment variable by clicking right click on My computer ->Advanced->Environment Variable->New
    • Set variable for Catalina
      • Variable Name = CATALINA_HOME
        Variable Value = C:\xampp\tomcat\
        Setup Environment Variable for Catalina
    • Set Variable for Java
      • Variable Name = JAVA_HOME
        Variable Value= C:\Program Files\Java\jdk1.6.0_10
        Environment variable for JAVA_HOME
    • Set Variable for Java Basic -PATH
      • Variable Name = PATH
        Variable Value= C:\Program Files\Java\jdk1.6.0_10\bin\
        Java Basic Environment variable setup
  4. Generate Class File for your Java Program
    • Create a java file(Program)
    • Save the program as Greetings.java(Greetings is Class name-That Program available at our sister site Click Here)
    • Run the Program in Command Prompt, Start->Run->Command prompt(cmd)
    • example – javac -cp C:\xampp\tomcat\lib\servlet-api.jar D:\Greetings.java 
      Here javac is command to compile .java file
      C:\xampp\tomcat\lib\servlet-api.jar is the location for servlet-api.jar file. it must need to compile servlet program.
      D:\Greetings.java is location of my java program file
      class file creating
    • Class File is Created in Local Disk D. Because my java program is in D:\
  5. Copy the created class file to classes folder of your tomcat server
    • Go to Local Disk D and copy the class file of Greetings.java and paste it in
      C:\->xampp->tomcat->webapps->examples->WEB-INF->classes
    • Open the Classes folder and Paste the Class File
      Classes folder
  6. Open web.xml file and add servlet url pattern
    • Go to C:\xampp\tomcat\webapps\examples\WEB-INF
    • In Classes folder you can find the web.xml file
      web.xml file
    • Open XML file in notepad and insert the following code to that file
      • <servlet>
              <servlet-name>Greetings</servlet-name>
              <servlet-class>Greetings</servlet-class>
        </servlet>
        Web.xml file editing

        Just replace the word Greetings with your class name
      • <servlet-mapping>
        <servlet-name>Class Name</servlet-name>
        <url-pattern>/servlet/CLASS NAME</url-pattern>
        </servlet-mapping>
        web.xml editing

        Just replace the word Greetings with your class name
    • Save the file
  7. Start the tomcat server
    • Just open CATALINA_START.bat file which is in tomcat folder
    • C:\->xampp->Tomcat->Double Click on the Catalina_Start(Run the tomcat server)
      Catalina start
Tomcat Running
This type output Message will be displayed in Command Prompt When Successfully Tomcat is Running
  • Now Run the Program
    • Open Browser (Chrome or Mozilla)
    • Type http://localhost:8080/examples/servlet/Greetings in URL and press enter
    • http://localhost:8080/ will point to ROOT folder of tomcat
      and /examlples/servlet/ point to examples folder and at the last Greetings is the class name. The Class file which you copied to classes folder will be executed at this moment.
      Greetings output
  • The procedure is available in the video tutorial Watch it here

     

    FreeSV
    FreeSVhttp://freesv.com
    FreeSV is a Blog Contains With Lots Of Information. Share The Articles and Spread the Information to Whole World

    Latest articles

    Advsertisement

    Related articles

    Advertisement

    Leave a reply

    Please enter your comment!
    Please enter your name here