Hello Everyone, We are back with some interesting concepts. 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
- Execute Java Servlet Program
- Install JDK and Xampp
- Setup all Environment Variables
- Generate Class file for 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 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 Also:
- How to create PHP Web Server on your android mobile?
- Best Operating Systems For Penetration Testing / Hacking
- Top 7 Tips for Secure E-Banking or Internet Banking
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)
- 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.
- Done
The procedure is available in the video tutorial Watch here