Link to home
Start Free TrialLog in
Avatar of rajah_mohammed
rajah_mohammed

asked on

How do you compile and run a Servlet?

Hello;

Im new to this so pls. I need some assistance. I have a simple servlet called : HelloWorld.java

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class HelloWorld extends HttpServlet {

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

    PrintWriter out = response.getWriter();
    out.println("Hello World");
  }
}

I put it in a folder classes in my directory :
C:\Tomcat_5.0\webapps\web1\WEB-INF\classes\HelloWorld.java

My question is how do I compile and run it?

Regards : Rajah
SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rajah_mohammed
rajah_mohammed

ASKER

I had read that already and just gave me complications would you help me
step by step [pls.]
what complications?
I placed my HelloWorld.java which is a servlet in :
C:\Tomcat_5.0\webapps\web1\WEB-INF\classes\HelloWorld.java

Now how do I compile or run it? is it same as JSP? How?
javac HelloWorld.java

(it is covered step by step in the above tutorials)
or with classpath included if necessary:

javac -classpath C:\Tomcat_5.0\webapps\web1\WEB-INF\classes\ C:\Tomcat_5.0\webapps\web1\WEB-INF\classes\HelloWorld.java
I got several errors when I compile it :

C:\TOMCAT_5.0\WEBAPPS\WEB1\WEB-INF\CLASSES>c:\javac\bin\javac HelloWorld.java

HelloWorld.java:2: package javax.servlet does not exist
import javax.servlet.*;
^
HelloWorld.java:3: package javax.servlet.http does not exist
import javax.servlet.http.*;
^
HelloWorld.java:5: cannot resolve symbol
symbol  : class HttpServlet
location: class HelloWorld
public class HelloWorld extends HttpServlet {
                                ^
HelloWorld.java:7: cannot resolve symbol
symbol  : class HttpServletRequest
location: class HelloWorld
  public void doGet(HttpServletRequest request, HttpServletResponse response)
                    ^
HelloWorld.java:7: cannot resolve symbol
symbol  : class HttpServletResponse
location: class HelloWorld
  public void doGet(HttpServletRequest request, HttpServletResponse response)
                                                ^
HelloWorld.java:8: cannot resolve symbol
symbol  : class ServletException
location: class HelloWorld
    throws ServletException, IOException {
           ^
6 errors
try:

c:\javac\bin>javac -classpath C:\Tomcat_5.0\webapps\web1\WEB-INF\classes\ C:\Tomcat_5.0\webapps\web1\WEB-INF\classes\HelloWorld.java
I tried both =-(

C:\Javac\bin>javac -classpath C:\Tomcat_5.0\webapps\web1\WEB-INF\classes\ C:\Tomcat_5.0\webapps\web1\WEB-INF\classes\HelloWorld.java

Or

C:\Tomcat_5.0\webapps\web1\WEB-INF\classes>c:\javac\bin\javac HelloWorld.java

And I got the same error as above. How do I deal about it?

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Here is a PAQ:

Compiling a Java Servlet
http:Q_20787476.html
Thanks Ryancys;

I got it running I just changed the servlet.jar to servlet-api.jar since I'm running Tomcat 5.
It did compile, one last thing how do I run it ?

C:\Javac\bin>javac -classpath C:\Tomcat_5.0\common\lib\servlet-api.jar C:\Tomcat_5.0\webapps\1stweb\WEB-INF\classes\HelloWorld.java
>>It did compile, one last thing how do I run it ?
Have u created web.xml file for ur application ?
If I'm correct I just have to add the servlet to my web.xml to successfully run it. How do I add it
to my web.xml for me to run it.

My web.xml looks like this and I'm not how to add it :

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-app_2_4.xsd"
    version="2.4">
</web-app>

How do I add my servlet to my web.xml above ?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks that was great !