-1

I'd like to create a simple Java web server that displays the text 'Hello, user!' in the browser whenever I navigate to localhost:8080 in a web browser window. I also want it to display the text "Hi!" whenever I navigate to localhost:8080/hi. Is it possible to write a Java server that does this concisely? (Specifically, I'm trying to find out how to create a simple RPC mechanism in Java that allows Java functions to be invoked from other programming languages.)

4

2 回答 2

2

If you're trying to create RESTful web services in Java, then JAXRS is the way to go.

You don't need to develop an http server, that is already written and there are many choices of well-tested and highly scalable http servers in Java. To develop RESTful web services with JAXRS, you annotate classes and methods in them with annotations that describe which http method, content type, and parts of the URL path they will respond to.

I happen to be familiar with Netbeans, and I could write what you describe and have it running in a matter of minutes.

References: http://en.wikipedia.org/wiki/Java_API_for_RESTful_Web_Services and http://docs.oracle.com/javaee/6/tutorial/doc/giepu.html

于 2012-10-12T04:17:34.973 回答
0

If you really want to write your own webserver, take a look at the httpserver package and the example code in this answer: https://stackoverflow.com/a/3732328/851273

You should then be able to call HttpExchange.getRequestURI() and use it to determine what to respond with. If the request URI is hi, then respond with a 200 and a response body of Hi!

于 2012-10-12T04:16:36.873 回答