Setting up environment for java servlet programming – java servlet tutorial
If you just started learning java servlet then the first thing you need to do is to make your system ready for java servlet programming. In this java servlet tutorial I will be teaching you how to set up environment for java servlet programming on your windows operating system. And we will execute a very simple java servlet example in this post. So lets begin.
Things needed for java servlet programming
- JDK (Java development kit)
- Apache Tomcat
If you don’t have the above tools then download it from here
apache tomcat download
JDK free download
- Just double click the executable file which you have downloaded above.
- Click on next, next and finish same as you install other applications on your OS
Setting java environment variables
For compiling java programs from command prompt you need to set environment variables. Just follow these steps to set up environment variables for java
- Go to your java installation directory which is usually
- You will see two folders one is of JDK and other is JRE
- Open JDK and then open bin
- From the address bar copy the whole path as given in the image below
- In my case this is the path but it can be vary a little so don’t bother about the path you are seeing here just copy the path from your system
- Now go to environment variables for this Open properties of computer then go to Advanced system settings the new window will open, on the new window click on environment variable (see the image for help)
- Click on environment variables a new window will open
- Click on new and on variable name write ‘path’ without quotes and on the variable value paste the address you just copied (the address of the java bin folder) as shown in the image below
- Click ok
- Make sure the command prompt is closed doing this or else you need to restart command prompt for the changes to take effect.
- Now for checking that Java is working properly open command prompt and write the command ‘javac’ without quotes and press enter, if you can see the commands then your javac is working correctly.
Now java is in your system and you have to set up server for servlet programming for this you need Apache Tomcat which you have already downloaded.
installing apache tomcat
- The installation is very simple same as you install the other applications
- Just keep in mind that you have to change the port number and installation directory if you are using windows 7 and above because if you install in program files directory then it will be very irritating while writing programs because every time you want to do some changes it will ask you for administrative permission.
- So double click on the executable file which you have downloaded
- When it asks for the directory to install just remove the program files part from the address and install it in you C: drive as shown in the image
- And continue
- When you see this window as shown in the image
- Change the HTTP/1.1 Connector Port to some other value like 8082, 8083, 8085 etc.
- Click next and continue the installation
- Once the installation is finished it will automatically launch the server or you can also manually launch by opening Monitor Tomcat from the start menu.
- See on the taskbar if the green play icon is visible on the Apache Tomcat icon that means your server is running or if not right click on the icon and click on start services.
- If you are seeing the green play icon open your browser
- Type the following URL on your browser
- My port number is 8085 if you have given some other port number then change it with the port number you have given and press enter
- If you can see the apache tomcat server page then it means that your server is running properly
Setting up environment variables for java servlets
- Go to the apache installation directory
- Inside the lib folder find servlet-api.jar and copy this file
- Now go to java installation directory, this time you need to go to the jre folder, in my case the path is
- Paste the servlet-api.jar here and copy the full path of the servlet-api.jar, in my case the path is
- Again open the environment variables as you opened before and click on new
- On the variable name type ‘classpath’ without quotes and variable value paste the path which you have copied (take help from the image given below), and enter a semi colon ‘;’ without quotes at the end of the path
- Now go to your Java directory and go to the folder given below
- Inside the lib folder you will find a file tools.jar, copy the full path of the file
- Now go to the new user variable window and after the semicolon paste the path of tools.jar which you just copied
- At the end of the path place a semicolon a dot and again a semicolon as given below
- Your final variable value will be like this
- Click on ok
- Now your system is ready for java servlet programming
Running a simple servlet program to check your environment
- Go to your apache tomcat installation directory
- Open this directory
- The webapps is the main directory in which you will create your project directories
- Create a folder here and give any name I gave it name StartServlet as it is the first program we will be executing
- Inside the folder StartServlet we need to create another folder named WEB-INF, it is case sensitive so be careful and give the name same as WEB-INF
- Inside the WEB-INF folder create another folder with name ‘classes’ without quotes, this is also case sensitive and should be in small letters. Now your directory structure will be look like
- Now create a java file inside the classes folder. I created StartServlet.java
- Put the following code in your java file.
- Compile the file using command prompt as you compile other java programs
- Press Shift and right click anywhere inside the folder classes and click on open command window here and then compile the program using javac
- If the compilation is successful a class fill will be created
- Now you need to create the web.xml file for execution of your program
Creating sample xml file for your first java servlet program
- Open notepad and paste the following code on it
- In servlet class you need to type the name of your class and in url pattern you have to type anything it will be used as a url to run your servlet program
- Now save the file inside WEB-INF folder, and the name must be web.xml , be careful while naming the file because it is case sensitive
- Now you are ready to execute your first servlet program
Executing your simple servlet example
- Open browser (make sure your server is running and it is a good idea to restart your apache server after compiling the code or making changes to the server)
- Now you need to type the following URL to execute the program
- In my case the url is
- Just type the URL on your browser and hit enter to see output.
|Output of The Program|
- Now that’s all. If you have any confusion or doubts you can ask by your valuable comments, Thank You.
http://surfpk.com/setting-up-environment-for-java-servlet-programming-java-servlet-tutorial/http://surfpk.com/wp-content/uploads/2015/09/servlet.pnghttp://surfpk.com/wp-content/uploads/2015/09/servlet-150x150.pngHelp&HowToJava TutorialTips & Tricksadvanced java tutorial,java servlet,java servlet programming,java servlet tutorial
If you just started learning java servlet then the first thing you need to do is to make your system ready for java servlet programming. In this java servlet tutorial I will be teaching you how to set up environment for java servlet programming on your windows operating system....email@example.comAdministratorHow to jailbreak iPhone 6 without Computer | How to unlock iPhone 6 | How to unlock iPhone 6 Plus| How to Jailbreak iPhone 5 | Absinthe Jailbreak | Taig - SurfPK.com