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 start a servlet from a Java Class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to start a servlet from a Java Class" Watch "How to start a servlet from a Java Class" New topic
Author

How to start a servlet from a Java Class

Nitasha Gupta
Greenhorn

Joined: Jun 17, 2006
Posts: 14
Hi,

I want to invoke a servlet printing "hello world" from a simple java class.
I tried using following code fron the java class, but nothing happened.
URL url = new URL("http://hostname ortnumber/servlet path/servletname");

Could anyone tell me how to run a servlet from a simple java program.

Thanks
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
The URL path you started is the right direction. The Sun Networking Tutorial has a section on "working with URLs" that will get you the rest of the way there. In short, your Java program will submit a GET or POST request to the web server that works on the server just like a request from a browser. Your Java program can read the response like a browser; the browser would normally display the response but you can do whatever you like with it.
[ August 27, 2006: Message edited by: Stan James ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Bruce Jin
Ranch Hand

Joined: Sep 20, 2001
Posts: 666
This code will do it:


BJ - SCJP and SCWCD
We love Java programming. It is contagious, very cool, and lot of fun. - Peter Coad, Java Design

Crazy Bikes created by m-Power
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to start a servlet from a Java Class