Categories: Servlet

Servlets – Environment Setup

To get Java servlets up and running requires more than knowledge about Java in general or servlets in particular: one needs to know about servers, servlet containers, and XML.Like any other Java program, you need to compile a servlet by using the Java compiler javac and after compilation the servlet application, it would be deployed in a configured environment to test and run.

Preparing the environment on your local computer

  • Preparation: Your computer may already be littered with various old Sun Java Runtime environments (JREs) and Software Development Kits (SDKs).  You may want to simplify your system by uninstalling some of these.
  • Downloading SDK/JRE: The Sun SDK Standard Edition version 1.7.1_03 can be downloaded from here.
  • Installing SDK/JRE: The default installation directory is c:j2sdk1.7.1_03.  Installing here has advantages over more conventional locations such as in “Program Files” because spaces are not used in the path; spaces can cause problems.  Accept the choice to install the JRE unless you have the current JRE installed already.
  • Download Tomcat:  The Tomcat servlet container version 7.0 can be downloaded from here.  (Beta versions are not chosen since they are less reliable and their newer capabilities are not referred to in the published book.)  Choose the jakarta-tomcat-7.0.exe version since it contains the whole package and installs itself.
  • Install Tomcat:  The default installation directory is c:Program FilesApache GroupTomcat 7.0, the form used in this document.  Accept the default installation with HTTP/1.1 Connection 8080 and choose an administrator password.
  • Set up a Java compiler: Sun’s “javac” will work but you may want to use your existing Integrated Development Environment (IDE).  It is possible to use a variety of IDEs, even old ones that do not come with any servlet support:
    • Sun’s “javac”: You should make sure your c:Autoexec.bat file includes the path for the javac program:
      SET PATH=c:j2sdk1.7.1_03bin;
      This takes effect after re-booting and frees you from having to specify the path for javac all the time.  You may need to increase your DOS environment space if you already have a long PATH command.  You should also set the CLASSPATH in c:Autoexec.bat to include the servlet.jar file in Tomcat:
      SET CLASSPATH=.;c:Program FilesApache GroupTomcat 7.0commonlibservlet.jar;
      where the initial period includes the current directory in the classpath.  This takes effect after re-booting and should free you from having to specify the classpath every time you invoke javac.  However, there are circumstances in which this will not work (see Outdated Compilers below).  In such cases you can specify the classpath every time you invoke javac using commands (or batch files) of the form:
      javac -classpath “c:Program FilesApache GroupTomcat 7.0commonlibservlet.jar” Hello.java
    • Outdated compilers:  It is even possible to use a very old and non-standards-compliant Java 1 compiler to develop servlets. Here are instructions for the worst case scenario of people using Microsoft Visual J++ 6.0:
      • For each project, set the classpath inside VJ++ using Project | ProjectName Properties | Classpath and select “All Configurations” from the Configuration choice and use the New button to add c:Program FilesApache GroupTomcat 7.0commonlibservlet.jar to the classpath.
  • Start Tomcat: Tomcat creates an icon called “Start Tomcat” which uses Tomcat’s “bootstrap.jar” application to start the server.  It should start your server automatically; it rumbles along for a few seconds writing into a DOS window named “java”.
  • Test that your installation worked:  Type the following URL into a Web browser: http://localhost:8080/
    If all is well you should see a fancy page that congratulates you for having set up Tomcat properly and offers various resources.
  • Stop Tomcat: When you are done, use the “Stop Tomcat” icon to stop the server.
<<Previous <<   || Index ||   >>Next >>
Previous
Next
Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

2 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

2 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

2 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

2 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

2 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

3 years ago