Being a complete newbie when it comes to web development in Java, I decided to take a step forward and build some expertise around JEE servers and web services. Another motivation is that we have some web technology coming up at Autodesk - more about that when it will turn public - that can be leveraged by, among other things, Java web services, so it makes sense to become a bit more familiar with it.
I'm starting this series of posts where I will report my progresses as I learn along the path. To get started, let's take a look at running a local Tomcat server and deploy our first Java servlet.
Prerequisites:
- Eclipse IDE for Java EE developers
Let's start with installing the Tomcat server. I'm assuming you are running a Linux based system. I found that this blog post was very straightforward and easy to follow, so I'm simply going to copy the detailed steps, you should be able to install Tomcat on your system without pain:
- Download a binary distribution of the core module: apache-tomcat-7.0.47.tar.gz from here. I picked the tar.gz in Binary Distributions / Core section.
- Unarchiving the archive will create a folder structure in your Downloads folder:
~/Downloads/apache-tomcat-7.0.47 - Open to Terminal app to move the unarchived distribution to /usr/local
sudo mkdir -p /usr/local
sudo mv ~/Downloads/apache-tomcat-7.0.47 /usr/local
- To make it easy to replace this release with future releases, we are going to create a symbolic link that we are going to use when referring to Tomcat (after removing the old link, you might have from installing a previous version):
sudo rm -f /Library/Tomcat
sudo ln -s /usr/local/apache-tomcat-7.0.47 /Library/Tomcat - Change ownership of the /Library/Tomcat folder hierarchy:
sudo chown -R <your_username> /Library/Tomcat
- Make all scripts executable:
sudo chmod +x /Library/Tomcat/bin/*.sh
Here is a tutorial that looks descent for Windows users.
Issue a /Library/Tomcat/bin/startup.sh from a terminal to start the server and open the following url in your browser: http://localhost:8080
You should see the Tomcat homepage:
If you try to take a look at the "Server Status" or "Manager App", it will prompt you for login and password: by default Tomcat is installed without user, so you need to create at least one to administrate your server.
Go to the "conf" folder inside Tomcat install, if you followed the tutorial "/Library/Tomcat/conf" and open "tomcat-users.xml", then add the following user, for example:
1 <tomcat-users> 2 <rolerolename="manager-gui"/> 3 <rolerolename="manager-script"/> 4 <rolerolename="manager-jmx"/> 5 <rolerolename="manager-status"/> 6 <userusername="your-admin-name"7 password="your-admin-pass"8 roles="manager-gui,manager-script,manager-jmx,manager-status"/> 9 </tomcat-users>
You should now be able to log with the defined credentials:
Let's now shutdown the server as it will be started automatically by Eclipse when we debug our project: /Library/Tomcat/bin/shutdown.sh
Open your Eclipse IDE, if you didn't install the Java EE version of Eclipse, no worries, you can just install the required packages later on. Go to "Help" > "Install New Software..." > set the "Work with" to your Eclipse release and select the "Web, XML, Java EE, ..." category:
Make sure you have the following packages installed:
Let's create our first servlet: "File" > "New Project" > "Dynamic Web Project"
Then click "New Runtime" and select the appropriate server version, fill up the "Tomcat installation directory" and create the project.
Once created, right-click on the project root in the explorer > "New" > "Servlet" > put some name for your servlet and the package, I'm naming it "HelloServlet" and edit the code as follow:
import java.io.PrintWriter;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class viewer
*/
@WebServlet("/hello")
public class HelloServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public HelloServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(
* HttpServletRequest request,
* HttpServletResponse response)
*/
protected void doGet(
HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<h1>" + "ADN Servlet running!" + "</h1>");
}
/**
* @see HttpServlet#doPost(
* HttpServletRequest request,
* HttpServletResponse response)
*/
protected void doPost(
HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
Once you are done, right-click the servlet in the project explorer > "Run As" > "Run on server" > open the page in a browser "http://localhost:8080/{project}/{servlet}" by replacing with the naming you used.
Comments
You can follow this conversation by subscribing to the comment feed for this post.