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\c lasses\Hel loWorld.ja va
My question is how do I compile and run it?
Regards : Rajah
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
My question is how do I compile and run it?
Regards : Rajah
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
what complications?
ASKER
I placed my HelloWorld.java which is a servlet in :
C:\Tomcat_5.0\webapps\web1 \WEB-INF\c lasses\Hel loWorld.ja va
Now how do I compile or run it? is it same as JSP? How?
C:\Tomcat_5.0\webapps\web1
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)
(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\c lasses\ C:\Tomcat_5.0\webapps\web1 \WEB-INF\c lasses\Hel loWorld.ja va
javac -classpath C:\Tomcat_5.0\webapps\web1
ASKER
I got several errors when I compile it :
C:\TOMCAT_5.0\WEBAPPS\WEB1 \WEB-INF\C LASSES>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
C:\TOMCAT_5.0\WEBAPPS\WEB1
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\c lasses\ C:\Tomcat_5.0\webapps\web1 \WEB-INF\c lasses\Hel loWorld.ja va
c:\javac\bin>javac -classpath C:\Tomcat_5.0\webapps\web1
ASKER
I tried both =-(
C:\Javac\bin>javac -classpath C:\Tomcat_5.0\webapps\web1 \WEB-INF\c lasses\ C:\Tomcat_5.0\webapps\web1 \WEB-INF\c lasses\Hel loWorld.ja va
Or
C:\Tomcat_5.0\webapps\web1 \WEB-INF\c lasses>c:\ javac\bin\ javac HelloWorld.java
And I got the same error as above. How do I deal about it?
C:\Javac\bin>javac -classpath C:\Tomcat_5.0\webapps\web1
Or
C:\Tomcat_5.0\webapps\web1
And I got the same error as above. How do I deal about it?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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\s ervlet-api .jar C:\Tomcat_5.0\webapps\1stw eb\WEB-INF \classes\H elloWorld. java
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\s
>>It did compile, one last thing how do I run it ?
Have u created web.xml file for ur application ?
Have u created web.xml file for ur application ?
ASKER
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>
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>
ASKER
How do I add my servlet to my web.xml above ?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks that was great !
ASKER
step by step [pls.]