Saturday, October 20, 2007

Running Servlets on Windows XP

20/10/2007

In my last blog, I wrote about how to run servlets in Fedora. As most of us use Windows not Fedora, so it forced me to write on running servlets in Windows XP. For my surprise it is more simple and easy than Fedora. So let's start.....


1. We need two things for running servlets in Windows XP: Sun Java SDK of course and tomcat. If you don't have Sun Java SDK, download the latest from http://72.5.124.55/javase/downloads/index.jsp

Download tomcat6 Windows Service Installer package from http://tomcat.apache.org/download-60.cgi



2. Install Java SDK. Now we have to set the environment variables for Java else we have to type the whole path of java compiler and interpreter each time to compile and run any Java Program. For doing that

i. Go to Start -> Control Panel. If its not in Classical view then choose 'switch to Classical View'. Double click on System icon. Now you'll be seeing a System Properties dialog box similar to the following.



ii. Now click on Advanced tab, You will see a button named Environment Variables at the Bottom of it. Click on it and now you'll see a dialog box similar to the following.



iii. On User variables, click on New. Now for Variable name enter 'path' and for Variable value enter the path of your JDK bin files directory. If you didn't change your default installation directory during installation it'll be something like 'C:\Program Files\Java\jdk1.6.0_03\bin\' where jdk1.6.0_03 will be replaced by your version of Java SDK. Click OK.



iv. Check that you correctly set the environment variable. Start -> Run. Type cmd. Now in Command Prompt type

java -version

It should give the output like this.





3. Install tomcat6. Start Tomcat by finding its start program in the Programs Menu (located in the Start menu). Look under Apache tomcat 6.0 and click Monitor Tomcat. In your notification area on taskbar Apache Tomcat icon will come. Double click on it and you'll see a dialog box. Start tomcat If it is not already started.




For more information on installation and setup tomcat visit the following links:

http://tomcat.apache.org/tomcat-6.0-doc/index.html
http://www.yorku.ca/jhuang/examples/tomcat-install.html (A little outdated though)



4. Check the tomcat installation by typing http://localhost:8080/ in your browser.





5. Now for compiling java servlets program you have to set the CLASSPATH environment variable to include the servlet-api.jar package path which will be 'C:\Program Files\Apache Software Foundation\Tomcat 6.0\lib\servlet-api.jar' for default installation directory. Change the path if you changed the installation directory. Now again set it using the same procedure for setting environment variables for Java. Remember to click on New button of User variables
not System variables.




6. Now we'll take a sample servlet program and will name it as MyHelloWorld.java

import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class MyHelloWorld extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<body>");
out.println("<head>");
out.println("<title>Hello World!</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Hello World!</h1>");
out.println("</body>");
out.println("</html>");
}
}

Save it using notepad as MyHelloWorld.java. Now compile it using javac command from the command prompt.




7. Now go to the tomcat installation directory. For default installation it would be C:\Program Files\Apache Software Foundation\Tomcat 6.0 here You will find webapp directory. Inside webapp directory make a directory named test. In this directory we will keep our web applications. Inside test, create directory WEB-INF. Inside WEB-INF create directory classes. Now inside classes we'll keep both our source and class files.


8. Copy the MyHelloWorld.java and MyHelloWorld.class files to the directory classes. Now to run our servlet we have to write our own web.xml file in the WEB-INF directory. Tomcat server reads the file web.xml to get information about the servlets. We'll open a file in notepad and copy and paste the following contents in the file. Save the file in the WEB-INF directory with the name web.xml

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<servlet>
<servlet-name>MyHelloWorld</servlet-name>
<servlet-class>MyHelloWorld</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyHelloWorld</servlet-name>
<url-pattern>/servlet/MyHelloWorld</url-pattern>
</servlet-mapping>
</web-app>

In the file the tags <servlet-name> and <servlet-class> gives the information about servlet-name and servlet-class and the tag <url-pattern> tells about the URL pattern to run the servlet.

9. Now stop and restart Tomcat from the icon in Taskbar. To run the sevlet type address
http://localhost:8080/test/servlet/MyHelloWorld


3 comments:

Unknown said...

Thanks

Anonymous said...

all the steps till i run the servlet is fine...at the end it gives error 404

Mann said...

@pittya16 are you able to browse http://localhost:8080/