File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
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

How to start a servlet from a Java Class

Nitasha Gupta

Joined: Jun 17, 2006
Posts: 14

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.

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: 672
This code will do it:

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:
subject: How to start a servlet from a Java Class
It's not a secret anymore!