Java Servlet

From HelioHost Wiki
Jump to: navigation, search

HelioHost users are able to create and deploy Java servlets to their hosting account. This tutorial will guide you through the steps of requesting Java to be enabled on your account, creating a simple servlet on your local PC, testing the servlet locally, exporting the servlet as a .war file, uploading the .war, and deploying the .war file.



In order to use Java on your HelioHost account you must first request java access, and then wait for it to be enabled. Here are the links to request java if your account is on Johnny, or request Java if your account is on Tommy. You can refresh that same page to check if Java access has been granted. On Tommy, this generally only takes 2-3 weeks, and on Johnny, the wait is usually 3+ months. If you're on Johnny and don't want to wait that long you can always move your account to Tommy. Once Java has been enabled on your account, you may proceed with this guide.

Download The Software

There are many ways to create a valid servlet, but for simplicity this tutorial will use Eclipse Java EE software Kepler version which can be downloaded from the website. Once you have the proper version for your operating system downloaded and installed start the eclipse program.

Starting Eclipse For the First Time

If this is the first time you've run the Eclipse program it will ask you where you want your workspace located. The default should be fine for most users. Once the program loads up it will display the welcome screen, but we can jump straight to the workbench by clicking the link in the top right corner.

Create Dynamic Web Project

Click File > New > Dynamic Web Project
Dynamic web.png

In the Project Name box type MyHelioServlet and then if this is the first servlet you've created the target runtime dropdown box will be empty so you must click New Runtime.
Servlet name.png

Since the Johnny server is currently running Tomcat 8.5.24 select Apache Tomcat v8.0 and then click Next.

Install Tomcat

If this is the first servlet you have created with Eclipse you will most likely need to click Download and Install... if you already have a working Tomcat install you can also locate it for Eclipse to use.
Download tomcat.png

Once Tomcat has been installed (or located) the Finish button should light up and become clickable. Then also click Finish on the New Dynamic Web Project window.

Create New Servlet

In the Project Explorer window right click MyHelioServlet and go to New > Servlet
New servlet.png

In the Java Package box type org.heliohost and in the Class Name box type HelloWorld. Then click the Finish button to create.
Class name.png

Edit Source

Eclipse creates a new file with skeleton code to get you started. Scroll down and locate the method doGet().
Doget empty.png

Then replace the // TODO comment with some code.

  response.getWriter().println("Hello world!");

So that it now looks like this.
Doget code.png

So, now when your .war is successfully deployed on HelioHost you should be able to access your servlet with a url similar to and the browser will display Hello World! However, there is currently no index file in the root of your servlet so if you were to deploy this as is and access it would return a 404 error. Let's fix that next.

Create index.jsp

In the Project Explorer window right click MyHelioServlet and go to New > JSP File.
New jsp.png

In the box labeled File Name type in index.jsp. Note that the i on index.jsp is lowercase. Since Heliohost runs a unix operating system uppercase and lowercase matters, and using a capital I here will cause it to not work correctly. Once you have the file named click Finish to create it.

Edit index.jsp Source

Once again Eclipse creates a new index.jsp file with skeleton code. Between the <title></title> tags type My Helio Servlet, and between the <body></body> tags type

<a href="../MyHelioServlet/HelloWorld">My first Heliohost servlet!</a>

Your code should now look like this.
Index code.png

Note the ../MyHelioServlet/ portion of the link. This is important because of the way that HelioHost proxies the Tomcat output through Apache. Without this code on your links the deployed servlet could send users to the wrong url resulting in 404 errors. Using this format allows you to view your servlet directly through Tomcat locally as well as once the .war file is deployed on HelioHost without changing your code for each location.

Change Project Properties

In the Project Explorer window right click MyHelioServlet and go to Properties.

In the left navigation panel highlight Java Compiler and in the Compiler Compliance Level drop down box select 1.8. If you get errors in your servlet similar to "unsupported major.minor version 51.0" it is probably because you forgot to change this value. Once you have the correct properties set click Ok to save your changes, and click Yes to allow Eclipse to recompile your project.

Test Servlet Locally

Now the time has finally come to test your servlet locally and see if you've done everything correctly so far. In the Project Explorer window right click MyHelioServlet and go to Run As > Run On Server. Click Ok to save both files. It's always a good idea to save your files often to prevent data loss, and Eclipse won't let you forget.
Run as.png

If you've been following this guide the default values on the next page should be fine. Verify that Tomcat v8.0 is selected. If you would like to always use this server you can check the box too. Then click Next.
Run server.png

On the next page you can select which servlets are deployed on your local Tomcat install. The defaults should all be good for us now, but remember this page later for when you're developing future applications. If everything looks good click Finish. Then click Ok when Eclipse prompts you to restart Tomcat.
Add remove.png

Once Tomcat is done restarting a new browser window will open displaying your servlet.
Helio servlet.png

And if you click the link it should say "Hello world!" If anything isn't working as expected at this point go back through the tutorial carefully to find out what you did wrong. You can also find assistance on our support forums or our chat channel. It's always a good idea to test your application as much as possible locally before you proceed. There may be a few small differences between how your servlet executes on the server versus locally, but it will save you a lot of time to debug everything you can locally first before uploading it.

Export as .war File

In order to deploy your working servlet on HelioHost you must create a .war file, upload it to your /home/username/ directory, and then deploy it through cPanel. To create the .war file right click on MyHelioServlet in the Project Explorer and go to Export > WAR File.
Export war.png

Click the Browse button and choose a location to save your .war file on your computer that you can remember and find again. Once you are at the location that you want to save it type the file name MyHelioServlet.war and click Save. Then click Finish to create the .war file in your chosen location.

Upload The .war File

On the same cPanel page that you requested Tomcat access you will see a deploy interface at the bottom of the page. First click the upload button, locate your .war file on your hard drive, and upload it. This will put it in /home/username/MyHelioServlet.war. If you prefer to use SFTP, or web disk make sure you use this same location.

Deploy The .war File

Once the file is uploaded to the proper location the same cPanel page that you requested Tomcat access will now show a drop-down selection where you can pick which .war file you want to deploy, and a deploy button to do so. Please be aware that if this is the first .war you've deployed or you have changed the filename since the last deployment it can take several hours to complete. If you use the same filename as the previous deployment it will go much faster and generally only takes a couple minutes.

Congratulations! You just successfully deployed your first Java servlet on HelioHost.

Personal tools

Google AdSense