Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to start a servlet from a Java Class

 
Nitasha Gupta
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Bruce Jin
Ranch Hand
Posts: 672
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This code will do it:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic