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.)

- 30,230
- 67
- 195
- 328
-
I decided to post this question here because I found most of the relevant to be very intimidating (and not necessarily beginner-friendly) - I have little prior experience with network programming of any kind. – Anderson Green Oct 12 '12 at 04:02
-
you need to study basics. Read some tutorials on Servlets. And, very soon you'd be writing pages that can log you in and show your profile too. :) – Nishant Oct 12 '12 at 04:03
-
@Nishant I don't think this problem is related to servlets. – Luiggi Mendoza Oct 12 '12 at 04:04
-
1What is the requirement of creating your own server? Is it purely for study or genuine requirement? – Garbage Oct 12 '12 at 04:04
-
I'm trying to allow Java functions to be invoked from other programming languages using HTTP. – Anderson Green Oct 12 '12 at 04:05
-
Yes it's possible, but it would require much more space than is available here to explain all the concepts required. – Jim Garrison Oct 12 '12 at 04:05
-
When things get more complicated than this, I would recommend checking out [Spring MVC](http://static.springsource.org/spring/docs/current/spring-framework-reference/html/mvc.html) if you're going to stick with Java. – alfredaday Oct 12 '12 at 04:06
-
_"I'm trying to allow Java functions to be invoked from other programming languages using HTTP"_ -- you are reinventing the Web Service. I suggest you do some research first. – Jim Garrison Oct 12 '12 at 04:06
-
HTTP is a protocol, not a programming language – Jon Lin Oct 12 '12 at 04:06
-
I know that HTTP is a protocol - I'm trying to create a simple RESTful web service in Java, so I need to create a simple server that responds with a different string to each URL. – Anderson Green Oct 12 '12 at 04:06
-
Why not use an existing web service container (like Tomcat) that has solved all the nasty low-level problems and provides a standard interface. – Jim Garrison Oct 12 '12 at 04:07
-
1If you mean to write something from scratch, you surely can do that (with sockets for example). Otherwise you can use the servlet api, which however needs to be deployed inside a servlet container (tomcat, jetty, jboss, etc). – b2Wc0EKKOvLPn Oct 12 '12 at 04:08
-
@AndersonGreen your question doesn't reflect what you really need. Here's an article that explains how to create the RESTful service: [REST with Java (JAX-RS) using Jersey - Tutorial](http://www.vogella.com/articles/REST/article.html) – Luiggi Mendoza Oct 12 '12 at 04:08
-
Why is this considered a bad question? I've noticed that I've already been downvoted - should I refrain from asking such basic questions in the future? – Anderson Green Oct 12 '12 at 04:11
-
First, you need to understand the [HTTP/1.1 protocol](http://www.w3.org/Protocols/rfc2616/rfc2616.html), then you're going to need to have an understanding of [Sockets](http://docs.oracle.com/javase/tutorial/networking/sockets/index.html). To do this properly, this is none trivial. – MadProgrammer Oct 12 '12 at 04:16
-
@AndersonGreen it's considered a bad question because **it's difficult to tell what is being asked here**. You ask about how to implement a Java Web Server like Jetty, Tomcat, or GlassFish when you need how to create a RESTful web service. It's never wrong to learn new things, but you have to ask the right things. If english is not your main language, use some tools or ask for guidance in that matter, or at least write your question in a way that other people on SO could understand. – Luiggi Mendoza Oct 12 '12 at 04:35
-
@LuiggiMendoza If I changed the title to something like "Which Java REST framework is the least difficult to learn?", would it still be considered off-topic, since it doesn't refer specifically to any REST framework? Would such questions be considered relevant elsewhere on the Stack Exchange Network, or are they considered too vague to be appropriate for inclusion anywhere? – Anderson Green Oct 12 '12 at 04:52
-
@AndersonGreen read the first question in the FAQ: [What kind of questions can I ask here?](http://stackoverflow.com/faq#questions). It states *a specific programming problem*, and in the [What kind of questions should I not ask here?](http://stackoverflow.com/faq#dontask) section it states *You should only ask practical, answerable questions based on actual problems that you face*. If the question is pretty wide, like *which Java REST framework is the least difficult to learn?* it will be closed because not all the people learn at the same rate. – Luiggi Mendoza Oct 12 '12 at 05:01
2 Answers
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

- 12,602
- 4
- 40
- 57
-
I've also found something called Restlet - how does Restlet compare to JAX-RS? – Anderson Green Oct 12 '12 at 04:18
-
Restlet may have predated JAXRS, or it may be an implementation of JAXRS (which is a spec). I'm not very familiar with it. If it is different than JAXRS, I probably would not use it. I think that JAXRS spec is quite good and there are different high-quality implementations of it out there. So there would need to be a really compelling advantage to use something else. – BillRobertson42 Oct 12 '12 at 04:21
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!