Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to "Hello World"?

 
nan sh
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jimi Rock
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic