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
- Execute Java Servlet Program
- Install JDK and Xampp
- Setup all Environment Variables
- Generate a Class file for the Servlet Java Program
- Where Should I Place the Created Class File?
- What Changes Need to do in the web.xml file and where is that file?
- 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:
- How to create PHP Web Server on your android mobile?
- Best Operating Systems For Penetration Testing / Hacking
Here is the step-by-step process:
- Install JDK – Download Link Click Here
- Install Xampp Web Server – Download Link Click Here
- 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\
- Variable Name = CATALINA_HOME
- Set Variable for Java
- Variable Name = JAVA_HOME
Variable Value= C:\Program Files\Java\jdk1.6.0_10
- Variable Name = JAVA_HOME
- Set Variable for Java Basic -PATH
- Variable Name = PATH
Variable Value= C:\Program Files\Java\jdk1.6.0_10\bin\
- Variable Name = PATH
- 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 is Created in Local Disk D. Because my java program is in D:\
- 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
- Go to Local Disk D and copy the class file of Greetings.java and paste it in
- 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
- 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>
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>
Just replace the word Greetings with your class name
- <servlet>
- Save the file
- 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)

- 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.
The procedure is available in the video tutorial Watch it here