Please enable / Bitte aktiviere JavaScript!
Veuillez activer / Por favor activa el Javascript!

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

0 405

Get real time updates directly on you device, subscribe now.

Hello Everyone, We are Back with some interesting concept. Java Servlet is the programming language its work is similar to PHP. It’s a Server-side Scripting Language and Here You can Learn to Execute the one Servlet Program.

By this tutorial, you will know about these topics

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

Programming

Requirements:

  • Java Development Kit
  • Tomcat Server [ We Use Xampp Package]
  • Browser
Read This Also
1 of 7

Overview:

  • Install XAMPP and then Install JDK and Set the environment variable to compile regular java programs using the command prompt. That must be pointed to lib folder of jdk.
  • Setup JAVA_HOME at your environment variable that must be point to your java folder
  • Setup CATLINA_HOME at your environment variable that must be point to tomcat folder (That’s inside the xampp folder. Xampp is in C:\)
  • Compile 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 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 classes folder of tomcat server. Path is like this “C:\xampp\tomcat\webapps\examples\WEB-INF\classes”
  • Now edit web.xml file. Which is at the very back folder of classes.
  • Now start tomcat server by pressing CATLINA_START.bat file
  • Open the browser and Run the program. Url will be like this http://localhost:8080/….
READ  PAID Games for Free on Android without ROOT

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_10Environment 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 fileclass 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 FileClasses 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 fileweb.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
  8. 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
  9. Done

The procedure is available in video tutorial Watch here

or Click Here

Here we used Greetings as class name and this program is available at our sister site https://edu.freesv.com

For any query use http://discuss.freesv.com – for instant replay ping us through live chat.

Get real time updates directly on you device, subscribe now.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More