I am looking for advice on how to go about writing a small and simple application that will receive http GET and http PUT request, process the data (simple text files) and respond.
I have all ready done this using threads and sockets but there must be a simpler and more efficient way. Also when I run my application using wireshark I am not convince I am using the http protocol as I should be.
Thanks
Alexis
You can use Tiny Java Web Server. (http://tjws.sourceforge.net/)
Alternatively, if you are using Java 6 or later, you can use the Http server API.
I used HttpComponents for similar purposes - it provides functionality for HTTP server and client parts implementation. It's easy to learn and use.
You can consider embedding a webserver like Jetty (start/stop it from java app) if you want to get the full benefits of HTTP parsing.
Related
Last time I think how can I connect my Java external program to Apache webserver.
I wanna send HTTP requests to process and then send output back as HTML file.
I thought about JNI but with multi-thread structure, it doesn't look good for me.
I think with help of Sockets it could be nice but I want to hear Your ideas.
Last but very important: should I use "normal" Apache or Tomcat for that?
For the level of integration you are looking for, JNI is way too low-level and involved.
Instead, take a look at:
FastCGI: on the Apache side use mod_fastcgi and jfastCGI on the Java side.
small HTTP servers: mod_proxy on the Apache side, which reverse proxies to a small HTTP server on the Java side such as Netty or Undertow.
I am going to implement webRTC for voice and video call, for this i would be needing a signalling server first.Can anyone please send me some links or some guidance for the implementation of the signalling server using java.
If you want to build signalling server using java, you can use sip servlet.
Sip Servlet
A SIP servlet is a Java programming language server-side component
that performs SIP signalling. SIP servlets are managed by a SIP
servlet container, which typically are part of a SIP-enabled
application server
See also mobicent implemention on sip servlet mobicent
And also see JainSip for signalling server JSIP
I am not sure about java. But you can try node.js(JavaScript) and socket.io to set up a signalling server.
Go to the signalling section of the following link:
http://www.html5rocks.com/en/tutorials/webrtc/basics/
If you want to try java, try setting up a websocket server to handle data from browsers.
Personally, I prefer to use PeerJs (Javascript) as the signally server. Peerjs is built on node.js and provide a lot friendly API for user to implement signalling.
I have no idea whether you can do it with JAVA. But JavaScript seems a better choose to build a signalling server.
To implement a signalling server, This tutorial should be a very good example about how to do it with Peerjs and Crosswalk.
You can also have a look at RestComm http://restcomm.com/ and https://github.com/Mobicents/RestComm.
It already contains :
a WebRTC client : http://www.telestax.com/livechat-and-video-call-with-restcomm/
a WebRTC Client Side Javascript framework
an Android SDK https://github.com/Mobicents/restcomm-android-sdk/
an iOS SDk https://github.com/Mobicents/restcomm-ios-sdk.
Of course, everything is open source so any contributions are welcome ;) !
I have a java client that sends a message to the server .The server is a servlet deployed on jboss. My question is can the servlet send data back to the client as acknowledgment? is it possible in servlets?
From Java EE 5 tutorial chapter 4:
What Is a Servlet?
A servlet is a Java programming language class that is used to extend the capabilities of servers that host applications accessed by means of a request-response programming model. Although servlets can respond to any type of request, they are commonly used to extend the applications hosted by web servers. For such applications, Java Servlet technology defines HTTP-specific servlet classes.
Yes it is possible with servlets. Read the basics here.
Of course it's possible; a servlet that didn't have the ability to send data back to the client wouldn't be very useful.
There are many ways to do so but the most "raw" way is by writing to ServletResponse.getOutputStream(). The ServletResponse is passed to the service method of a Servlet implementation. Note that the data is of course going to be sent back as a valid HTTP response (identified using MIME, etc).
Ashwinm if you are looking for posting a result to a socket (on client) and not returing the result through mormal HTTP response.
Which I believe is not a servlet question then. You can anyway do anything in java. But have to look at normal "how to write to a socket" tutorial.
GenreicServlet might provide some extensibility or some solution.
EDIT
I read your commentes below later. Yes you need URLConnection in that case at the bare minimum.
An http servlet works on http request and can send back the http response. You can connect to it using URLConnection as described above.
If you don't want to get at this level (layer), you can work at with the packets directly (using sockets). Here you dont need a servlet actually. Here your server (a simple java application) will listen on a socket for any incoming connection and the client can connect to it.
could you please give me a sample code on how an Http Server(Java) receives the request of a client(android)? I mean the client sends the request via Httppost, how the server takes the content of these requests in order to see the context and reply? I am trying to built a chat application.
Thank you in advance!
The server-side of HTTP is usually implemented using the protocol stack provided by a web container. You would then implement your application's server-side as servlets. There are numerous tutorials on this.
If that's the way you want to proceed, look at one of the standard web containers; e.g. Tomcat, Jetty, Glassfish, etc. The source code for all of these is available for you to browse, though I should warn you that they are all complicated under the hood.
Assuming that your HTTP service is going to be delivering JSON or XML (rather than HTML) to clients, you may want to look into using a RESTful framework.
Have a look at ServerSocket. Keep in mind that accept() blocks and, as you will probably run it in a service, you will want to time it out and check for the completion of the service. That should probably run in its own thread as should the responders to requests.
From there, you can open input and output streams to receive the request and write the response. There are any number of packages that can help you with the interaction, or you can roll your own, but it doesn't seem like you've done a lot of homework. Perhaps some searching, reading, and more specific questions would more you along more quickly.
I have a website (python/django) that needs to use a load of Java resources that may or may not be on the same server. Therefore I am writing a mini webserver in Java that will receive a request and then when processing is finished, POST some data back to a url on the site.
I have got the java code receiving connections on sockets and responding with some simple HTML.
My problem is that I will POST data to the Java server and that code needs to act on the data. How do I go about reading the data that is posted in the HTML request, if it is even possible. If not, is there any other way you would do this.
If you think I am going about this in completely the wrong way then please tell me and I will consider another method, but after conversing with some Java developers, this seemed like the best way for what I was doing.
Thanks
You probably don't need to write a http server yourself, just use some lightweight java web server/servlet container like jetty or simple
and looks here if you still want to know how to parse a http POST request http://www.jmarshall.com/easy/http/#postmethod
In your case I probably wouldn't work with low level socket connections.
I recommend you to use a servlet container with some sort of webservice or maybe only a simple servlet depending on your needs. With a servlet you can easily access the POST data, process it and return something to the caller.
If you don't want to use a stand alone servlet container like Tomcat you can try an embeddable servlet container like Jetty or winstone servlet container.
If you need something more sophisticated you can use some webservice technology like JAX-RS or JAX-WS. This allows you to provide a fully featured API for your Java resources in an easy way.