Saturday, October 25, 2008

How to run your first Java Servlet

Step by step running your first Java Servlet.

Step 1. Install tomcat http server from here. Select the binary based on your operating system. If you are using Windows, just click here. After download, just install it. After installation, you can access your home page at "http://localhost:8080" actually it is the content in "C:\xxx\Tomcat 6.0\webapps\" where "xxx" is the installation directory of your tomcat.

Step 2. Install java. How to do that? Search online, it is pretty simple. After installation, run in the command windows "javac", if it says "'javac.exe' is not recognized as an internal or externa command, operable program or batch file." You have to run "set PATH=%PATH%;C:\Program Files\Java\jdk1.6.0_10\bin" to add the java bin directory to Windows PATH variable. Make sure use your own directory for javac (I'm using jdk 6 update 10).

Step 3. Write your first Servlet
Store the following into "HelloServlet"
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HelloServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

public void doGet(HttpServletRequest request,
            HttpServletResponse response)
throws ServletException, IOException {

// Use "request" to read incoming HTTP headers (e.g. cookies)
// and HTML form data (e.g. data the user entered and submitted)
// Use "response" to specify the HTTP response line and headers
// (e.g. specifying the content type, setting cookies).
PrintWriter out = response.getWriter();
out.println("Hello Servlet");

// Use "out" to send content to browser

Step 4.  In "cmd" command line, run "javac", if everything goes well, you will get a new file "HelloServlet.class"

Step 5. Goto to your home directory for tomcat (mine is "F:\Study\tomcat\Tomcat 6.0\webapps"). Make a new directory called whatever, say "test".
Inside test, make a new directory called "WEB-INF". Go into "WEB-INF", make a new directory called "classes", put "HelloServlet.class" into "classes". 

Step 6. Inside directory "WEB-INF", make a new file called "web.xml".
It can be downloaded here. After you download, change the file name from "web.xml2" to "web.xml", because .xml file can be interpretted by many browsers.

Step 7. Open your browser and type "http://localhost:8080/test/HelloServlet"

If you see a blank page with a line of words "hello servlet", you are all set with your first java servlet.

Actually, I'm new to java servlet too. But I found it really powerful and interesting.


Ashwin Perti said...

Those are really good. But that can also include more number of things in it.

Anonymous said...

you have a nice site. thanks for sharing this site. there are various kinds of ebooks are available here

brieweb said...

Correction to your web.xml. You don't need to put test.HelloServlet as the name of the servlet. You only have to put HelloServlet



brieweb said...

I just tried to put the contents of the xml, and it just stripped the tags! Ugh!