I was reading an article today about this piece of software and I taught that it was quite interesting. You can see all the parts of a swing application in this swing debugger. For someone like me that is not always sure how all the parts are linked together it is a quick visual view. Worth a try for sure.
Servlet programming has always been something I was curious about. Starting this project was a bit ackward since there is plenty of documentation but nothing to hold your hand in what I was looking at. Too much struggling convinced me to write down how to do it for a very simple servlet so I could go from there.I used Eclipsei 3.1 as my Java IDE and I use Tomcat 5.5.x as my Servlet Application Server.
- Creating a new project in Eclipse
This should be a basic task that is quite easy. I called my new project HelloWorldServlet (great craetive title). I then created a new class named “HelloWorldServlet”. This opens the HelloWordlServlet.java file in the editor and I entered this source code in it:
public class HelloWorldServlet extends HttpServlet
static final long serialVersionUID = 1;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
PrintWriter out = response.getWriter();
out.println("Hello World through a servlet!");
- Importing the servlet librarySomething that took me a little while to figure out is that I needed to add the servlet library from Tomcat to be able to properly compile but to also get the proper dynamic help in the Eclipse IDE. Under my working space directory I have a lib directory where I copy the common libraries that I use. The Tomcat servlet-api.jar was copied over there. You then simply right-click on your project in Eclipse, go to “Build Path” and click on “Add External Archives”. You simply browse to where you have copied servlet-api.jar and you now have the library imported.
- Unknown tidbitAfter adding this external archive it highlighted the fact that I need to have a “static final long serialVersionUID”. This is for the serialization of the object but I am not sure what value, if unique, it should have. I only added it as I was told without looking for too many answers right away.
- Compiled and copiedI compiled (ctrl-b) my servlet and copied it to my Tomcat box. Under the Tomcat default directory there is a webapps directory where you have to install your application. I made a new directory simply called “HelloWorld”. Under that directory I created a “WEB-INF” directory. Under the WEB-INF directory I created a “classes” directory where I copied my .class.
The directory structure currently looks like this:
- Application declaration In the WEB-INF directory I had to create a web.xml file containing this: <br>&lt;?xml version=”1.0″ encoding=”ISO-8859-1″?&gt;<br>&lt;!DOCTYPE web-app<br> PUBLIC “-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN”<br> “http://java.sun.com/dtd/web-app_2_3.dtd”&gt;<br>&lt;web-app&gt;<br> &lt;servlet&gt;<br> &lt;servlet-name&gt;HelloWorldServlet&lt;/servlet-name&gt;<br> &lt;servlet-class&gt;HelloWorldServlet&lt;/servlet-class&gt;<br> &lt;/servlet&gt;<br> &lt;servlet-mapping&gt;<br> &lt;servlet-name&gt;HelloWorldServlet&lt;/servlet-name&gt;<br> &lt;url-pattern&gt;/HelloWorldServlet&lt;/url-pattern&gt;<br> &lt;/servlet-mapping&gt;<br>&lt;/web-app&gt;<br>
This file declares my servlet name, class name and URI. Since everything is the same here it is quite simple.
- Calling the applicationThe fun part (when it works) is calling the application and seeing the end result. Since I have already declared in my web.xml that the URI is HelloWordlServlet calling it in my browser was as simple as entering “http://serverName/HelloWordlServlet/”.
- A few more notesBecause I was not perfect on the first try I had to reload my application after each change I made to the class file or to the web.xml. Important to reload otherwise the cached version is what you keep seeing.