Servlet runs in a web browser with the help of a web-server like Tomcat which is popularly used as an HTTP-web server and Java Virtual machine in the background that includes both JDK and JRE. There are several open-source and paid Integrated Development Environments (IDEs) available on the Internet using which you can quickly develop Servlet based applications.
There are various methods to compile and run Servlet applications such as:
- Command Prompt (CMD), Console or Terminal
- IDE and Compilers such as Eclipse and Java IDE NetBeans
How to compile Servlets from CMD or Console?
To compile Servlets from CMD or Console:
- Install latest JDK and JRE on your PC and make sure to add JAVA_HOME Environment in case of Windows operating system.
- Download and Install Apache Tomcat as we would be using servlet-api.jar that comes with Apache Tomcat to compile our Servlet Java source code file.
- Open the command prompt and run following command to compile Java source code file:
javac -classpath C:\apache-tomcat-7.0.23\lib\servlet-api.jar YourServlet.java
- Replace apache-tomcat-7.0.23 with your Apache Tomcat installation path and ensure servlet-api.jar is located in the lib directory also replace YourServlet.java with your Servlet Java source code file and make sure that you are running from the same directory where your servlet Java source code file is placed.
- Once the Java class file has been generated, you can copy it and place in your project.