java-servlet
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

Once you have both the files you can move to the next step which is installation.

install java

  • 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
C:Program FilesJava
  • 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
javac-classpath
  • 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
C:Program FilesJavajdk1.7.0_21bin
  • 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)
javac-classpath1
  • 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
javac-classpath2
  • 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
install-apache-tomcat
  • And continue
  • When you see this window as shown in the image
install-apache-tomcat1
  • 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
http://localhost:8085
  • 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
C:Apache Software FoundationTomcat 8.0lib
  • 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
C:Program FilesJavajre7libext
  •  Paste the servlet-api.jar here and copy the full path of the servlet-api.jar, in my case the path is
C:Program FilesJavajre7libextservlet-api.jar;
  • 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
 servlet-variable
  • Now go to your Java directory and go to the folder given below
C:Program FilesJavajdk1.7.0_21lib
  • Inside the lib folder you will find a file tools.jar, copy the full path of the file
C:Program FilesJavajdk1.7.0_21libtools.jar
  • 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
C:Program FilesJavajre7libextservlet-api.jar;C:Program FilesJavajdk1.7.0_21libtools.jar;.;
  • Click on ok
servlet-classpath
  • 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
C:Apache Software FoundationTomcat 8.0webapps
  • 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
simple-servlet-program
  • 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
 simple-servlet-program
  • Now create a java file inside the classes folder. I created StartServlet.java
  • Put the following code in your java file.
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;public class StartServlet extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws IOException{
PrintWriter out = response.getWriter();
out.println(“<html>”);
out.println(“<body>”);
out.println(“<h1>Welcome to Servlet Programming</h1>”);
out.println(“<h2>Your simple servlet program</h2>”);
out.println(“</body>”);
out.println(“</html>”);
}
}
  • 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
<web-app>
<servlet>
<servlet-name>My First Servlet</servlet-name>
<servlet-class>StartServlet</servlet-class>
</servlet> <servlet-mapping>
<servlet-name>My First Servlet</servlet-name>
<url-pattern>/StartServlet</url-pattern>
</servlet-mapping>
</web-app>
  • 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
http://localhost:8085/<your project directory/<url pattern in xml file>
  • In my case the url is
http://localhost:8085/StartServlet/StartServlet
  • Just type the URL on your browser and hit enter to see output.
simple-servlet-program6
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/wp-content/uploads/2015/09/servlet.pnghttp://surfpk.com/wp-content/uploads/2015/09/servlet-150x150.pngSurfPK.comHelp&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....