Java and JSP: Learn Why It’s the Most Popular Programming Language
Disclosure: Your support helps keep the site running! We earn a referral fee for some of the services we recommend on this page. Learn more
The most widely used programming language in the world today is Java (according to the PYPL measurement). With over 10 million active users and billions of devices, the Java language has shaped the modern world.
The power of the Java language has very little to do with the syntax or construction of the language itself, which is very similar to C and C++ in style.
Instead, the power behind the Java exists in the Java Runtime Environment (JRE), which is a platform that can run on any type of computer, and a huge variety of devices.
This extreme level of portability is possible because the JRE runs on what is known as a Virtual Machine. A virtual machine that can be made to run on any device, which means that software written in the Java language can run on any device.
With this setup, Java is both compiled and interpreted. Java code is fed into a compiler that processes code into bytecode. This gets run by a bytecode class loader, then split into tasks for both an interpreter and a machine code generator.
The Java Virtual Machine (JVM) platform does this all seamlessly for the developer, and can run on anything from a mainframe to a Mars rover.
Java’s relationship with the mobile market has been controversial with the legal battle between Oracle and Google, but no one can argue how valuable Java’s cross-platform capabilities have been in the mobile market.
Java 2 Platform Micro Edition (J2ME) and third-party Java tools, allow JRE implementation on all the major mobile operating systems, letting Java developers to code for any device they choose.
So Java is a popular language because the Java Platform is so easy to get setup and run on any device.
The language itself has been refined and is perhaps the most well-documented language of all. This combination of factors makes Java so popular, it’s not just the language, it’s the entire ecosystem.
Let’s get started with the basics — the stuff you really ought to know before digging into Java programming
Java’s Origin Story
A project at Sun Microsystems in 1991 was spearheaded by James Gosling. The goal was to create interactive television.
However, this proved to be too ahead of its time for cable providers, so the project pivoted to find a new primary use. The language was crafted using the familiar syntax of C++, under a project name called ‘Oak.’
The key concept that remained central to the project, however, was the ‘Write Once, Run Anywhere’ (WORA) approach to using the language, portability was key. The goals of the project can be summarized succinctly as:
- It must be Simple, OOP, and Familiar to programmers
- It must be robust and secure.
- It needed to be architecture-neutral and totally portable across devices.
- Interpreted, Threaded, Dynamic — and thus easy to use, easy to scale.
- High Performance! Competition with C++ and other languages hinged on speed.
With all this packaged into one language and one platform, Java was released to the public in 1995. In the years to follow, browsers began to allow for Java applets to run on websites, and a strong evangelist team helped the Java enterprise take off.
By 2006, Sun released Java as open source under the General Public License, since most of their revenue came from their enterprise platform. In 2009, Sun Microsystems was acquired by Oracle.
However, they also wanted a lightweight interpreted language that could compete with Windows’ Visual Basic.
This means both are portable across operating systems and machines, but for different reasons. Be sure to never get these two languages mixed up, or you might get laughed at by experienced programmers.
There are lots of ways to learn Java. So regardless of your learning style, we’ve got you covered.
The first thing to do is try and understand the basics of the language. If you’re new to programming, you will also want to become familiar with data types, object-oriented concepts, and the basic control statements like loops.
Here’s some good resources for getting started with those basics:
- The Java Tutorials by Oracle
- Getting Started with Java — JavaCoffeeBreak.com
- Getting Started with Eclipse — this is a popular interactive development environment (IDE) for Java.
- Introduction to Java Programming – by IBM.
Online Courses and Video Lectures
Enrolling for an online class, or just watching video lectures is a good way to make sure that you become familiar with all of the idea paradigms of the language. There are a lot of places to take classes online, either free, or for a small charge. Some might even provide certification.
- Introduction to Programming in Java by MIT Open Courseware
- Java Programming Courses on EdX
- Java Programming Courses on Coursera
- Java Tutorials on Lynda.com
- Java Programming Tutorials Youtube Playlist
- The Official Java Youtube Channel.
Books are great for people who want to have a comprehensive resource always readily at hand. Since Java is so big and well-used, there are a lot of free resources out there. Shop around and find a writing style you like, and be sure to watch out for the most recent editions.
- Java Programming on Wikibooks
- Amazon Best Sellers for Java Books
- Java Books from O’Reilly Publications
- Java on Bookboon.com.
Forums and Communities
Forums and online communities are a great place as well. Through online conversation, IRC, and sharing of information, you can become much more embedded into the active community of users.
This helps tremendously in learning quickly. Don’t be afraid to ask questions on StackOverflow either.
Learning Java for Minecraft
One fun way to learn programming with Java is through the popular game Minecraft. While the game development community for Java is relatively small compared with C++ and C#, this title has a lot of users and utilizes Java for both game and server code.
There are a lot of resources out there for this, because parents have figured out that this is a good way to get kids interested in coding at an early age! Modding Minecraft can be fun for adults too, and is a great way to learn Java.
- A Grownup’s Guide to Minecraft and Writing Minecraft Mods — resources for adults
- Mod Design 1 — Learn to Code in Java with Minecraft — a class for kids
- Learn to Program with Minecraft Plugins — a book
- How to Make your own Minecraft Plugin & Learn Java — youtube guide
- The Young Person’s Guide to Programming in Minecraft — Github page
- Java-Gaming.org — Not minecraft, but all gamedev on Java.
JavaServer Pages and Java Servlets
Probably the most used application of Java is as JavaServer Pages (JSP). And this JSP Java Servlet tutorial runs through the basic steps of setting up and running your first Java servlet.
Follow the clear, simple instructions below to set up the Tomcat Server (needed to run Java servlets). Once your server is set up, move on to the tutorial’s second section to create, configure, compile, and deploy a basic “Hello World!” Java servlet.
Setting up an Environment (Tomcat)
Before we can start with our first Java servlet, we’ll need a stable environment in which to run it. That means installing the Apache Tomcat server. Follow the steps below.
1. Find the Latest Stable Release of Tomcat
Find out which version of Tomcat is the latest stable release. To do that, visit the Tomcat version page from Apache. The table at the top of the page shows the latest Tomcat version at the very top. Note that in the image below, however, the top version is in the alpha testing phase. Therefore, at the time of this writing, the latest stable version was 8.5.15. The table also shows us which version of Java we’ll need.
2. Install the JRE or JDK
Now that we know which version of Tomcat we’ll be using, we also know which version of the Java Runtime Environment (JRE) we’ll need. If you’re still not sure, see the image above. For this JSP tutorial, we’ll use Tomcat 8.5.15, which means we’ll need JRE 8. (“7 andlater. “)
Note that JRE is part of JDK (Java Development Kit). To date, there’s no way to simply update the JDK, so if you’re running an outdated version, the easiest course is to install the latest version of JDK.
You can install JRE here.
You can install JDK here.
3. Download Tomcat
To download Tomcat, visit the Tomcat download page. There are a lot of links on the page, but if this is your first time installing Tomcat, you’ll want the “zip” link under the “Core” heading for Windows, or “tar.gz” for Mac.
It’s not 100% crucial to choose zip over tar or vice versa since the contents are the same, but Windows prefers zip.
4. Install Tomcat
Installing Tomcat is pretty simple.
For Windows: create a project directory. We’ll call it c:projectOne. Unzip the Tomcat download file into that directory. It’ll show up in c:projectOneapache-tomcat-8.1.5 (or whatever version you’re installing). You can shorten this directory name to c:projectOnetomcat. From now on, we’ll just call it the Tomcat folder.
For Mac double click the downloaded file. Move the folder “apache-tomcat-8.1.5” (or similar) to your applications folder. You can shorten the folder name to “tomcat”. From now on, we’ll just call it the Tomcat folder.
5. (Windows Only) Create an Environment Variable
In Windows, we’ll need to make an environment variable named JAVA_HOME and point it to our JDK installed directory.
- Find the JDK install directory. Look in “c:Program FilesJavajdk1.8.0…”
- Set the environment variable by choosing Start > Control Panel > System and Security > System > Advanced System Settings > Switch to “Advanced” Tab > Environment Variables > System Variables > “New”.
- Under “Variable Name”, enter “JAVA_HOME”.
- For “Variable Value”, enter the name of your JDK install directory from step 1.
- Verify by restarting, then in a CMD shell issue “SET JAVA_HOME”. This should return the location of your new Java environment variable, which should be the install directory from step 1.
6. Configure the Tomcat Server
To configure Tomcat Server, first find the four configuration files. Look in the “conf” folder in the Tomcat install directory for the following files:
Backup all four files before you edit them.
Using a text editor like BBEdit, TextWrangler, or similar, open and edit the four files like so:
Locate the code shown below in the web.xml file, then change “false” to “true” as shown in green. Note that you’re looking for the default servlet and the listings parameter. There’s a full copy of the file here. Use this configuration for learning only. Using it in production undermines proper security.
<!-- The default servlet for all web applications, that serves static --> <!-- resources. It processes all requests that are not mapped to other --> <!-- servlets with servlet mappings. --> <servlet> <servlet-name> default </servlet-name> <servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class> <init-param> <param-name>debug</param-name> <param-value>0</param-value> </init-param> <init-param> <param-name> listings </param-name> <param-value>true</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet>
In the server.xml file, find the code below. Change the default TCP port number from 8080 to any port number from 1024 to 65535. For this tutorial, we’ll use 8888.
<!-- A "Connector" represents an endpoint by which requests are received and responses are returned. Documentation at : Java HTTP Connector: /docs/config/http.html (blocking and non-blocking) Java AJP Connector: /docs/config/ajp.html APR (HTTP/AJP) Connector: /docs/apr.html Define a non-SSL HTTP/1.1 Connector on port 8080 --> <Connector port="8888" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />
We’ll alter the tomcat-users.xml file as shown below to enable Tomcat’s manager. (Pick your own username and password for the Tomcat manager. For more info about using the Manager app, see here.)
<tomcat-users> <role rolename="manager-gui"/> <user username="tomcatManager" password="XYZ" roles="manager-gui"/> </tomcat-users>
To the context.xml file, find the <Context> element and add the “reloadable=true” attribute. This will let us enable automatic reload after we make any changes to our code. Use this for testing only. In production, this would cause unnecessary overhead.
<Context reloadable="true"> ...... ...... </Context>
7. Start the Tomcat Server
Next we’ll need to start Tomcat Server.
In Windows: open a CMD shell and do the following to change to the right directory and start Tomcat:
c: // Assuming Tomcat is installed in the c: drive. cd projectOnetomcatbin // Assuming Tomcat is installed in projectOnetomcat.
startup // To start Tomcat
In Mac: open the Terminal, change to the right directory, and start Tomcat:
Cd /Applications/tomcat/bin // Change to the Tomcat install directory and bin
./catalina.sh run // Start tomcat server
8. Start a Tomcat Client
To access the Tomcat Server, we’ll need to start a client. In your browser, go to “
http://localhost:8888” to reach the Tomcat Server welcome page. This works with a local machine. For remote access, substitute the actual IP address in place of “localhost”.
9. Test Your Tomcat Install
Test that Tomcat is working properly by browsing to
http://localhost:8888/examples. Try running a few of the example servlets.
10. Shutdown Tomcat Server
Don’t simply close out of Tomcat by closing the active window. Instead, shutdown Tomcat properly.
In Windows: shutdown Tomcat Server by pressing Ctrl-C from the Tomcat console. Or from the CMD prompt, enter “shutdown” from the tomcat install directory and bin (c:projectOnetomcatbin).
In Mac: press Ctrl-C from the Tomcat console. Or in the Terminal, enter “./shutdown.sh” from the tomcat install directory and bin (so, cd to /Applications/tomcat/bin).
Tomcat is Now Installed and Running
If you’ve followed the steps above, you should have a running Tomcat Server instance, complete with everything you need to run Java servlets. Let’s start with our first Java servlet (below).
Write Your First Java Servlet
A Java servlet is just a java program running on an HTTP server. Web users access it by browsing to the correct URL.
To write your first Java servlet, follow the steps below.
1. Create a Web App and Directory Structure
The first thing we need to do is make a Web App and directory structure for our Java servlet. We’ll call it helloWorld. In our tomcat folder (see step 4 above to locate this folder) find the webapps folder.
- In the webapps folder, make a new folder called helloWorld
- In the helloWord folder, make a new folder called WEB-INF .
- In the WEB-INF folder, make a new folder called classes .
Now we have tomcat folder > webapps > helloWorld > WEB-INF > classes. We’ll use them like this:
- helloWorld is our context root (also called the document base directory) for our web app. In it, we’ll put all HTML, CSS, scripts, images, and other files visble to web users.
- WEB-INF is for the application’s web.xml file.
- classes is for Java servlet class files.
After you’ve created the above folder structure, restart the server.
2. Create Your First Java Servlet
Once you’ve created the proper folder directory, create the servlet file. We’ll call it MyServlet.java, and we’ll save it in the “classes” directory. In the Tomcat folder, put the code below in webappshelloWorldWEB-INFclassesMyServlet.java.
Use the standard Hello World code from Tutorials Point to test your first servlet.
3. Compile Your Servlet
Compiling the first servlet isn’t easy. To compile it, we’ll need the Servlet API, located in our Tomcat folder, in /lib/servlet-api.jar.
In Windows: open a CMD shell and enter:
c: cd projectOnetomcatwebappshelloWorldWEB-INFclasses javac -cp .;C:projectOnetomcatlibservlet-api.jar MyServlet.java
// Where Tomcat is installed in c: projectOnetomcat // Note: if any part of the path contains a blank space, put the entire path in quotes // like this: "c:folder onetomcatlibservlet-api.jar"
In Mac: open Terminal and enter:
Cd /Applications/tomcat/webapps/helloWorld/WEB-INF/classes Javac -cp .:/Applications/tomcat/lib/servlet-api.jar MyServlet.java
This will create a new class in the classes folder called MyServlet.class.
4. Configure the Servlet’s Request URL
To configure the URL for the MyServlet servlet, create a web.xml file for it and put it in webapps/helloWorld/WEB-INF. In the example below, we’re creating a new arbitrary servlet name “HiThere” and using it to map our recently created class file “MyServlet.class” to our new request URL “urlhello”.
<?xml version="1.0" encoding="ISO-8859-1"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <servlet> <servlet-name>HiThere</servlet-name> <servlet-class>MyServlet</servlet-class> </servlet> <!-- Group all <servlet> elements together. Put them in front of all <servlet-mapping> elements --> <servlet-mapping> <servlet-name>HiThere</servlet-name> <url-pattern>/urlhello</url-pattern> </servlet-mapping> </web-app>
The URL to access the above servlet is
http://hostname:port/helloWorld/urlhello, where “helloWorld” is the name of our web app created in step 1 above.
Note that this configuration file applies only to our web app “helloWorld.” To refresh the web.xml file, restart the Tomcat Server.
5. Invoke the Servlet
To invoke your new Java servlet, open a new browser window and browse to:
This Java tutorial for beginners gives you everything you need to set up a Tomcat environment and create your first Java servlet. You should be able to download and install the JRE/JDK and Tomcat Server, configure Tomcat, create, compile, and deploy your first Java servlet. If you notice problems or have suggestions, contact us and let us know.
Where to Go From Here?
Learning the basics of programming, in general, can easily be accomplished by knowing Java. It’s a great first language, and learning new ones after Java is highly recommended. Also, since Java is used for a multitude of devices and applications.
As you may have noticed while looking through all the available books, there are a ton of different uses for Java, and each one can become an entire arena of expertise.
As such an enormous platform, there will always be a need for Java developers. This could be about getting new devices Java-capable, using Java to connect networks, or writing code to run web software.
All of these niche-uses of the language and the platform come loaded with tremendous career opportunities, so get out there, learn, make and explore!