File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes How to Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to "Hello World"?" Watch "How to "Hello World"?" New topic
Author

How to "Hello World"?

nan sh
Ranch Hand

Joined: Jan 05, 2001
Posts: 167
I have a very simple program HelloWorld as;
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 compiled and stored it under path
c:\tomcat\Webapps\HelloWorld\Web-inf\class\HelloWorld.class
Aslo I have c:\tomcat\Webapps\HelloWorld\Web-inf\web.xml as following;
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE web-app (View Source for full doctype...)>
- <web-app>
- <!-- Your servlet definitions go here
-->
- <servlet>
<servlet-name>HelloWorld</servlet-name>
<servlet-class>HelloWorld</servlet-class>
</servlet>
- <servlet-mapping>
<servlet-name>HelloWorld</servlet-name>
<url-pattern>HelloWorld</url-pattern>
</servlet-mapping>
</web-app>
After start tomcat, I typed URL in browers as; http://localhost:8080/HelloWorld
what I can see is ;
"
Directory Listing for:/HelloWorld
Up to:/


Tomcat Web Server v3.2.1 "
But according to the book(Core Servlets & JavaSerer Page), I should see
"
Hello World
"
Anybody know what is going wrong?
Thanks

Have you tried this Mock Exam Testing Engine yet?<br /><a href="http://www.mycgiserver.com/~nan111/index.html" target="_blank" rel="nofollow">www.mycgiserver.com/~nan111/index.html</a>
Jimi Rock
Ranch Hand

Joined: Feb 19, 2001
Posts: 134
Nan,
Store your classes under:
C:\Tomcat\webapps\examples\web-inf\classes
Call your class as follows: http://localhost:8080/examples/servlet/yourclassname
Regards,
Jimi.
nan sh
Ranch Hand

Joined: Jan 05, 2001
Posts: 167
Hi Jimi,
It works!!! Thank you!
It looks like your way is simple and easy- do not need web.xml file. But I can not find any documents explain to me how your way works. I thought this way can not work for tomcat.
Hope you enjoy the URL I found!
Thanks
Nan
Jimi Rock
Ranch Hand

Joined: Feb 19, 2001
Posts: 134
Nan, it works for tomcat. why not?
note that I did find this way my self , I will tell you how:
When I run an already exist servlet example, I call the html file that exist under tomcat_home/webapps/examples/servlets
then I have noticed that when I submit a request the following url is appered in my browser title bar: http://localhost:8080/examples/servlet/classfilename.
so I have edited my servlets and compile them , then save them under the classes directory. then call them as specified . that's all!
Thanks,
Jimi.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to "Hello World"?