Server Program - Responding To Post Requests From HTML pg.

Hello All At The Java Ranch, I want to write a simple Java Server Program That Responds To Post Requests From A HTML page. I've got a html page on apache with a form <form method="post" action="http://localhost:5000/"> <input type="hidden" name="hiddenfield" value="yesitshidden"><p><input type="text" name="query"> <input type="submit"> </p> </form> And I need to write a simple program that will listen to port 5000 and then redirect the user to who submitted the form to a HTML page the program also generates. I don't know how to write the server part of this program, I've looked at the sockets and url tutorials that sun provide but no luck! This program acts as the server part to a client server apllication and listens to a port 5000 is there anyway to adapt it to accept requests from a html form??

Hi John, Welcome to JavaRanch. Wouldn't it be easier to do this with a Servlet? Servlets take care of all the nasty details of POST and GET requests so you can concentrate on just responding. You can do this with sockets but it's going to be a lot tougher, and I'm not sure that UDP is the best way to go anyway since there is no guarantee that the message will ever be delivered.

Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher

John Winstanley
Greenhorn

Joined: Apr 08, 2003
Posts: 14

posted Aug 03, 2003 06:28:00

0

It would be easier to do this with a servlet. That was my first approach for the application and it worked well. But I wanted this to be easy to install no servelt container and no Java SDK. Using datagrams wasn't the way to go its just that I'd used that code for another application and it was similar. I found this code which was a very good start to what I wanted. / * An example of a very simple, multi-threaded HTTP server. * Implementation notes are in WebServer.html, and also * as comments in the source code. */ import java.io.*; import java.net.*; import java.util.*; class WebServer implements HttpConstants { /* static class data/methods */ /* print to stdout */ protected static void p(String s) { System.out.println(s); } /* print to the log file */ protected static void log(String s) { synchronized (log) { log.println(s); log.flush(); } } static PrintStream log = null; /* our server's configuration information is stored * in these properties */ protected static Properties props = new Properties(); /* Where worker threads stand idle */ static Vector threads = new Vector(); /* the web server's virtual root */ static File root; /* timeout on client connections */ static int timeout = 0; /* max # worker threads */ static int workers = 5;