I've tried to create async-servlet for long-polling, using this tutorial on my Glassfish server, however it didn't work well, the POST requests were too long (timed out) and GET returned sometimes void response with "Error parsing XML file" firebug message. Can somebody please provide simple async-servlet for handling long-polling requests?
It is better you move away from the servlet based approach if you want to implement a long polling solution. Netty is very good choice for such applications. Just build a simple HTTP instance with child.keepAlive option in bootstrap kept to true (server doesnt close off the client connections). That way you don't need to deal with the servlet containers time out and connection handling intricacies. Netty is an asynchronous IO framework and should give you great performance matrix.
Related
I have a console Java application. Some of HTTP requests return error codes, and I want to debug them. In browser I can watch POST requests in detail, but how can I manage to do it in Java application?
In your getHttpRequest (or whatever it is called) method print incoming request.
That would be the simplest solution.
More sophisticated - you can extend standard classes which work with requests and modify methods which get requests to print them first.
Try Wireshark or fiddler. The both sniff traffic in a similar manner as firefox and various browsers do now.
I am not looking specific answer, just an idea or a tip.
I have following problem:
Android application is a client for web service. It has a thread, which sends events (XML form with request ID) through http protocol and for every request server sends confirmation, that he understand message right with granted event ID - server is a synchronizer for few clients. I want use websocket protocol to send events through websocket too but it is a little bit tricky - instead of http, I don't expect to get response for every request. Moreover, incoming websocket messages is parsed in other thread. Primary mechanism it's a little bit overgrown and I don't want to write everything from scratch.
I want to make this asynchronous websocket mechanism to pretend to be synchronous.
There is my idea for now - after send event through websocket I will wait no more for e.g 5 seconds for response which will processed in other thread (it's came as XML) and regarding too request ID it will notify proper paused thread. I worry Condition.await() and condition.signal isn't the best idea, what do you think?
According to this problem, I've realized that I have problems with project this kind of mechanism. Do you have an idea, where can I find information about good pattern and tips which good to now to avoid bad approach? Thanks in advance!
The only difference between websocket and HTTP requests is the lack of HTTP headers when a message comes in. In websocket, you have a heartbeat that keeps the connection alive and allows full duplex communication, and then you have pure payloads. It's your job to find which message headers you will use to route the requests properly in your server/client.
So, that doesn't stop you from communicating in a request/response manner by simply writing to the output stream right after receiving. I suggest you take a look at the RFC
https://www.rfc-editor.org/rfc/rfc6455
If you're a little more visual, this slideshow can help:
http://www.slideshare.net/sergialmar/websockets-with-spring-4
Or if you want some more serious implementations as an example, take a look at spring's docs:
http://docs.spring.io/spring/docs/current/spring-framework-reference/html/websocket.html
I currently have an TCP Java socket communication implementation in which I have a server that is listening to a port (let's say port 5478). Then I need an Android client to remotely connect to the Java server and send a SQL query, than will then be executed on the server side database and then I want to send a list of results back to the Android client (already implemented with a custom Java class named Result that implements Serializable). I do this by sending an ArrayList of Result to the Android client. The Java server is always listening to the port and supports multiple clients trough multiple Threads. How can I migrate this implementation to a more secure platform and what is the best way to do it? I don't need to respect HTTP protocol to afford this communication. Is Tomcat the best solution?
Thanks
I would use Servlet3.0 as part of tomcat.
Then from android you just have to send http requests to the server using a URL and the servlet can database them. You can also serialize the data as well if you need to.
I hope that answers your question.
~ Dan
//EDIT:
Once you have set up eclipse and tomcat, you can start writing servlets. First - you have to configure the server to use servlets for certain addresses, for example localhost:8080/myServlet - that means that anything you send to local host triggers the servlet. The code for your first servelet looks like this:
public class ExampServlet extends HttpServlet {
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
Your doPost method is what gets called when you perform a http post request on the address the servlet is listening on. Then, all you have to do it put some code in to read the request to get the data out of the message body. Basically you read your request object that gets passed in, and you write to your response object to send the response back to the client. There are plenty of guides out there. I followed something like this to get started:
http://www.coreservlets.com/Apache-Tomcat-Tutorial/tomcat-7-with-eclipse.html
Hope that helps :)
~ Dan
Tomcat is an Servlet container + webserver. If you plan to move to tomcat then you are implicitly moving to http. And yes, if you want a secure communication .. you can create a soap based webservice(apache axis) and host it on https.
I'm not sure how mutch additional security tomcat is able to provide for your application. Two tings come to mind:
Enforcing authentication and some access rules. This is not too hart to implement and heavily depends on the rule quality. However it may help f you use it. It's often replaced by own imlpementations. However, to get securty you need encryption i.e. https. Or it's possible to steel the session and gain the rights bound to it.
Request to file mapping. This in fact somewhat more complicated. You shouldn't code this on your own. It's more complicated than it looks at first sight.
However, one of the biggest security wholes ever is directly executing code you got from somewhere. For example SQL statements. Ok it's secure as long as your databse rights are set perfectly...
Developing a securly encrypted protocol is not simple either.
However, the major win on switching to tomcat (or whatever) might be scaleability for free. And I think implementing servlets is much simpler than programming against sockets. And there are many great to tools fo working with http(s) though ven it might be more complicated than yours, it's pretty simple to deal with.
Unfortunately I can't answer our question. I don't know what's the best solution is. But I think there's at least some potential for wins.
I have a third party server that is periodically sending http post request messages to an URL(can be configured). In my application I am reading data by starting a jetty server and listening for data on the configured URL.
Wondering if it is possible to listen for the data sent by the server without starting any server like the jetty?
You can always create a socket yourself and listen at port 80 (or something similar) for HTTP requests. See http://download.oracle.com/javase/6/docs/api/java/net/ServerSocket.html
But there are several problems: Theres a lot of overhead that you need to do yourself. Parse the HTTP request, extract the headers and the body and depending on the headers you need to do certain things like caching, authentication, etc. And that's a lot of stuff you need to implement. Using an existing web server is usually a better idea, since the people who wrote it (usually) know exactly what they are doing.
Another option is the Apache HttpCore library (http://hc.apache.org/httpcomponents-core-ga/index.html). You can use it to write your own Http Server... But again, there's still a lot of stuff you need to take care of ...
If you want to do it for learning purposes, go ahead and implement it yourself. When it is for production, stick with the commonly used web servers.
I'm implementing a long poll http connection using java servlet.
How can I know that the http client is still active at any instance? Currently, what I do is to write a byte to the output stream and flush data. If there's an IO exception then the client is dead.
But in ASP.NET there is a property, Response.IsClientConnected which can find out if the client is active without writing anything to the output stream.
I want to know how if it is possible to develop in java servlet. I do not want to keep writing data into the http response stream as it may cost network.
Thanks in advance.
It will be difficult to achieve that using Servlet APIs. Though the low level Socket APIs provide this functionality (Socket.isConnected() ), but same functionality is not available through any higher level APIs. Not sure if you any compulsions of using Servlet APIs or you can use low level socket APIs.
Maybe you've taken the wrong approach? HTTP protocol is developed to be used in a request-response style, it is not suited to be used for a long polling. In fact, there should be lowest possible delay before client gets a server response.
The case you've described looks like a job for a good old Socket.