Java remains the go-to technology for server-side web and enterprise applications today. With it, developers can embed Java code directly into web pages they serve to clients, or create sophisticated server applications, that manage every aspect of the client-server transaction. For this, you need an application server integrated with your web server and, while many such products available, Proloquor.net uses Caucho‘s Resin. Here we introduce the basic concepts of JSP’s and servlets, and discuss how to serve them using Resin.
Setting up Resin
As with MySQL, I recommend finding a web hosting service that provides Resin, Apache Tomcat, or any other suitable application server, rather than standing one up yourself. Unlike, MySQL however, it may be necessary to have a local application server on your development machine to test your applications before deploying it to your production server. Fortunately, you can install, test, and deploy your applications directly from Eclipse.
Begin by installing and launching your copy of Eclipse (Luna or later). To install a Resin server on your local computer, and the accompanying Eclipse adapter, click Window / Preferences / Server / Runtime Environments. You’ll be presented with a list of installed servers, which will likely be empty. Click Add… to add a new one. Here you’ll be presented with a list of available servers your Eclipse knows about. Resin isn’t included by default, so you’ll have to add it.
Clicking Download additional server adapters presents a list of additional servers; find the latest Resin Server and click Next. Select the Resin Server, and click Next to install the adapter.
You still need to install the server itself, so click Download and Install and you’ll then be taken to the Resin download page. Choose the latest version of the base Resin suite (no need for Resin Pro), 4.0.43 at the time of this writing.
If you’re using a Windows platform for development, choose the .tar version and unpack it in a common location, such as c:\Resin. Update the Resin Home field to match this location, accept the defaults on the remaining windows (update the admin username and password as necessary), and you’re ready to go.
Creating Your First Web Project
If you installed the Java Enterprise Edition (JEE) version of Eclipse, launch it now and select File / New / Other / Web / Dynamic Web Project. Give your project a name, like FirstProject, and make sure the Resin target runtime you created above is selected. Check the ‘Generate web.xml deployment descriptor’ box and accept the remaining defaults.
Adding a JSP
A Java Server Page (JSP) is a web page with java code embedded in it. When the page is requested, the server runs the java code and serves the result along with the surrounding HTML. To create a new JSP for your FirstProject, click File / New / Other / Web / JSP File. If you want to make a JSP that displays the current time, give it a name like Time.jsp, and make sure you store it in the ‘web content’ folder (WebContent by default) of your project. The next screen provides various templates you can use to get started. ‘JSP File with HTML markup’ is a good first choice.
There are countless online resources that will help you develop JSPs. Your first stop might be the official JEE reference for JSPs at Oracle.com. For our purposes, let’s consider the simple page that displays the time.
The relevant part of the file is in the <div> tags. Code between <%= and %> is executed when the page is requested, and any output replaces the code; a string representing the current date and time.
To run it on the Resin server you installed locally, click Run / Run as / Run on Server. The server will start and the Eclipse browser will be displayed with the requested page.
The browser shows the HTML with the output of the Java code. There’s no trace of the code on the client side, even when viewing the HTML source.
Adding a Servlet
The ultimate expression of server-side Java however is a Servlet. Servlets are Java classes that inherit from javax.servlet or its children and manage every aspect of the web process. Oracle‘s JEE reference is also the home for the servlet specification, but we can cover the very basics now.
Continuing with our FirstProject in Eclipse, click File / New / Other / Web / Servlet. Give your servlet a name, TimeServlet, and include it in a package, net.proloquor.http. Accept the remaining defaults, including the list of methods you’d like to override.
The generated class includes empty methods for doPost(), doGet(), and a constructor. The doGet() method is called whenever the server receives an HTTP GET request from a browser for the resource and returns the required output. It takes two input parameters, HttpServletRequest request, HttpServletResponse response, which contains information about the incoming request and the response to be produced respectively. If our servlet is to return the current time as well, it might be coded as follows:
Our servlet first sets the content type of the response, then gets a PrintWriter to generate the output and uses it to produce the desired HTML.
But before we can run the servlet, we need to provide some configuration information to the server in the form of a web.xml file. This file is included with every deployed project. It’s main functions are to 1) define the servlets, giving them a name, Time in our case and 2) map that servlets to a URL like /time.
Run the servlet on your local server just as with the JSP. Highlight the servlet in the Project Explorer and click Run / Run as / Run on Server.
Deploying Your Project
So far we’ve seen that it’s quite easy to create dynamic web projects, add JSPs and servlets, and test them with a server on your development machine. The next step is to deploy your work to a public server so the rest of the world can appreciate it. In the JEE world, this is typically done with a Web ARchive (.war) file. This file is an archive of a specific directory structure the server understands, the basis of which is in the WebContent folder of your project.
To create the .war file, click File / Export… / Web / WAR file. Give it a name and accept the remaining defaults. The generate .war does not have to be stored as part of your project.
Instructions for deploying your .war to your production Resin server should be provided by your hosting service. Typically, you’ll be given an account and instruction to move your .war into a specific folder in that account, typically /webapps. Use the File Transfer Protocol (ftp) or Secure SHell (ssh) client of your choice, such as Filezilla. Once you upload your file, the server unpacks it and loads it at that location, making it available for requests.
Deploying your project to a shared, alien server can be fraught with danger. While you generally control all aspects of your development machine, your production platform is at the mercy of your hosting company. One important feature to look for is the version of the Java Development Kit (JDK) the platform is using. This problem is often that the platform’s runtime is not as current as yours, indicated by:
Since it’s unlikely you’ll be able to upgrade your platform’s JDK, you’ll have to downgrade the JDK on your development machine. Fortunately, Java tools have a backward compatibility mode. To set this from within Eclipse, click-right on your project in the Project Explorer and select Properties / Java Compiler.
Here, we set the ‘Compiler Compatibility Level’ from 1.8 to 1.6.
The Resin server has a built-in management dashboard that allows you to configure every aspect of your server. To access it, simply browse to the base URL of your project and log in with the credentials you identified when you set up your server. The default credentials for the Resin server you set up with Eclipse is admin/password. Try the ‘webapp’ link for a listing of the current servlets running on the server.
While its easy to access the admin page for your local server, production servers generally don’t have this feature enabled. Consult your hosting company for management options.
We’ve covered a lot of ground again today. We’ll return to JEE develop often, adding more features and services such as Hibernate and Struts, to our apps.