So far we’ve been writing all of our code in a basic text editor, and compiling our servlet classes via the command prompt. It’s important to understand the basics of how a web app works: stuff like running the server (in Jetty, that’s the
start.jar file), setting up
web.xml, and writing and compiling servlet classes and copying them into the
But as our code gets more complicated, using an IDE like Eclipse will make our lives much easier.
This tutorial assumes you’re already familiar with Eclipse from the Eclipse tutorial, so I won’t spend a ton of time introducing ideas like workspaces and projects. Instead, this tutorial talks about using Eclipse to develop web apps.
You might have seen stuff referred to as EE, as in Java EE or Eclipse EE. That EE stands for Enterprise Edition, which is basically just versions of Java and Eclipse that include tools for server development.
I think the term is a little outdated, because it dates back to a time when the only interactive websites that existed were owned by businesses: stuff like online stores. That’s where the term enterprise (which is just another word for business) comes from. Now interactive websites are all over the place, not just owned by businesses. But we’ve kept using the term EE to describe stuff used for developing interactive websites.
You’ve actually already been using Java EE. It’s the tools and
.jar files that come with your Jetty server. When you add
servlet-api-3.1.jar to your classpath so you can use the
HttpServlet class, you’re using Java EE. When you use JSP, you’re using Jave EE. Java EE isn’t an alternative to regular old Java- it’s built on top of it.
Simlarly, Eclipse EE is a version of Eclipse that comes with a bunch of tools that make it easier to write server code. Remember how Eclipse let you compile and run a Java file just by pressing the play button? Eclipse EE lets you compile and run a server just by pressing the play button!
Do you already have Eclipse EE?
If you already have Eclipse, let’s check whether you already have the EE version. Open Eclipse, and then go to
Help > About Eclipse. The window that pops up tells you which version you have.
Eclipse IDE for Java Developersmeans you have the standard edition, so you need to download Eclipse EE.
Eclipse Java EE IDE for Web Developersmeans you have the EE version, so you can skip the next step.
Download Eclipse EE
Go to the Eclipse download page and download the installer. When the installer launches, a dialog asks you which version to install. Choose
Eclipse IDE for Java EE Developers:
Step through the installation process and then launch Eclipse EE. In the workspace chooser that pops up, I recommend creating a new workspace directory instead of reusing an existing workspace. If you see a welcome tab, you can close this it (and uncheck “Always show Welcome at startup” if you want). Finally, you should see something that looks like this:
This means that Eclipse EE is running. Now we can start creating servers and web apps!
To use Jetty from Eclipse, we’re going to install the Eclipse Jetty plugin.
Open the Eclipse Marketplace by going to
Help > Eclipse Marketplace.., and then type
Jetty in the search box. You should see a plugin called Eclipse Jetty:
Install Eclipse Jetty. You’ll have to accept its license and then click OK in a box warning you about installing software. You’ll also be asked to restart Eclipse. When it comes back up, you’re done!
File > New > Project.. and in the dialog that pops up, go to
Web > Dynamic Web Project and click
On the next screen, give your project a name. The default settings for everything else should be okay.
You can click through the rest of the settings using the
Next button, or you can just click the
Finish button to accept the default changes.
You should now see your project in the
Project Explorer tab.
It’s empty for now, but we can see how the project is organized:
Java Resources: This is where servlets and other Java classes will go.
WebContent: This is the top-level publically available directory. This is where static files go.
WEB-INF: Files in here are hidden from public access. This is where
web.xmland other files you don’t want users to access directly will go.
You can ignore the other stuff for now. Also note that your Java classes will go inside the
src directory under the
JavaResources - you don’t have to compile them into the
classes directory yourself anymore! We’ll see that in action in a second.
One last thing we need to do is add the servlet API to our project’s classpath. This will give us access to Java EE classes like
HttpServletRequest. We did this using the
-cp path/to/jetty/lib/servlet-api-3.1.jar when we were using the console. In Eclipse, we need to add that
.jar file to our Java build path.
To do that, right-click your project, then go to
Properties, and in the dialog that pops up go to the
Java Build Path section. You should see this:
This shows the list of libraries on your classpath. To add the servlet API to the classpath, click the
Add External JARs.. button, and then select the
servlet-3.1.jar file in your Jetty directory. You should now see this:
Now we’re ready to write some code!
Let’s start by creating a servlet. Right-click the
src directory, then go to the
New menu, and select the
Class option. Name your class
HelloWorldServlet, and click the
That should open up the Java editor. Enter this code into it:
This code creates a basic servlet that adds a message to the request and forwards it to a JSP file. Next, let’s create that JSP file. Right-click the
WEB-INF directory, expand the
New menu, and then select the
JSP File option. Name the file
HelloWorld.jsp and click the
That opens up a JSP editor. You can delete any template code that’s automatically added, and enter this JSP code:
This JSP just displays the
message using the EL
Finally, let’s create a
web.xml file that tells our server to map URLs to our servlet. Right-click the
WEB-INF directory, expand the
New menu, and then select the
Other option. In the dialog that pops up, expand the
XML directory and select the
XML File option. Click
Next, and then name your file
web.xml and click
Jitsi 8x8 video meetings. That opens up the XML editor. If the
Design tab is selected, switch to the
Source tab. Enter this XML:
Your project should now look like this:
Now that we have a web app project, we need to tell Eclipse to run it using Jetty. You only have to do this step once for each project you create.
With your project open, open the
Run menu, and then click the
Run Configurations.. option. In the dialog that pops up, select the
Jetty Webapp tab, and then click the
New button (it looks like a piece of paper with a plus sign) in the upper-left corner.
That opens up a configuration panel where you can change a bunch of settings. Here are the ones we care about now:
Context Pathis the part of the URL after the domain. If you leave this as
/, then your webapp will be at
http://localhost:8080. If you change it to
HelloWorld, your webapp will be at
http://localhost:8080/HelloWorld/. Let’s change it to
Optionstab allows you to specify which Jetty version to use. The default uses a Jetty server that comes with the Eclipse plugin, but let’s use the version of Jetty we already have. Select the
Use Jetty at pathradio button, and then click the
External..button and navigate to your Jetty directory.
Your run configuration should look like this:
You’re done! Click the
You should see some stuff print to the console in Eclipse. That means your server is running, and you can access your web app! Open a browser to http://localhost:8080/HelloWorld/index.html, and you should see this:
Try changing the value of
message in the
HelloWorldServlet class. Click the
Stop button (it looks like a red square in the
Console tab in Eclipse) to stop the server. Then click the
Run button (it looks like a green play button towards the top of Eclipse) to run the server again. Refresh the page, and you should see the new message!
Eclipse gives you a bunch of cool features: it tells you about compiler error directly in the editor, it offers autocomplete and auto-fixing of code, and it makes it easier to compile your code and run a server.
But you don’t have to use Eclipse. You can stick with a basic text editor and the console if you want. And in fact, I think it’s really important to understand the basics of what’s going on behind the scenes (knowing how to run Jetty and how it’s finding web apps and mapping them to URLs). You could also use another IDE designed for writing code instead of Eclipse if you want.
No matter how you write code, the process is the same: you create a
web.xml file that maps a URL to a servlet class, you create a servlet class that contains your logic, and you create JSP files that render your view, which can use static files in the visible directory. You then run the server and visit your web app in a browser. Eclipse just makes stuff like compiling and running a little simpler. There’s no magic!
Happy Coding is a community of folks just like you learning about coding.
Do you have a comment or question? Post it here!
Eclipse is a great open-source Integrated Development Environment that supports a lot of languages.
Install a Java Development Kit
For JavaEE work, I currently prefer JaveSE.
- Browse to Oracle’s Java SE Development Kit downloads
- In the section titled Java SE Development Kit 9.0.1, read the license and, if you agree, click Accept License Agreement
- Still in that section, click on jdk-9.0.1_windows-x64_bin.exe (or the right download for your OS)
- Run the downloaded JDK installer, using Run As Administrator
- Add the Windows (or Linux) Environment Variable JAVA_HOME. Set it to the root folder of your newly-installed JDK, which for me is C:Program FilesJavajdk1.8.0_51
- If you are working behind a proxy, you have my sympathy. Set these additional Environment Variables
- HTTP_PROXY to your proxy server and port. Something of the form http://myproxy:911
- HTTPS_PROXY to the appropriate server and port. Something of the form http://myproxy:911
- NO_PROXY to a comma-separated (on Windows) list of sites that are not on a proxy. Something like localhost,*.mycorp.com
Eclipse Ide For Ee Java Developers
Install Eclipse Oxygen for Java EE
- Browse to Eclipse Downloads
- Click on the Download button under Get Eclipse Oxygen.
- On the resulting page, click on the Download button.
- Note: the width (32 bit or 64 bit) must match the width of your installed JDK. You installed jdk-9.0.1_windows-x64 above, so download the 64-bit Eclipse.
- Run the downloaded installer using Run as Administrator.
- Choose the version of Eclipse you wish to install. I prefer Eclipse IDE for Java EE developers for Java work.
- If the installation fails, try again with realtime virus scanning temporarily turned off. Remember to turn it on again when you’re done.
- If you are behind a proxy, set the Eclipse Proxies to match:
- Start Eclipse
- Select Window / Preferences
- Select General / Network Connections (that is, expand the General section and click on Network Connections)
- For Active Provider, select Manual. In my experience, Eclipse has never been able to use the Native OS proxy settings.
- Set HTTP to your proxy and port as when you set up Java. Something like http://myproxy and 911
- Set HTTPS to the appropriate server and port, as when you set up Java.
- If you have a SOCKS proxy, set it here.
- NOTE: If SOCKS proxy is checked, make sure to set the proxy to something. It seems that a checked but empty Eclipse SOCKS proxy gives Maven some issues.
- Click OK
- Create a folder named lib under your Eclipse root folder. Something like C:UsersYourLoginDocumentseclipselib Do this to avoid Tomcat server errors later on.