aspose 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


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies 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
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
 
Similar Threads
call servlet from simple java file.
Quick Java Classes and Beans question
Invoke servlet from java class
Call servlet from batch file
Javax.servlet.http.HttpServletRequest' is received as weblogic.servlet.internal.ServletRequestImpl