Can I write a socket programming to provide services to the web clients?
I did it using servlet, but I want to do it using java.net API.
Please give me a sample code of some program, such that I can access that simply by mentioning URL at the address bar of any web browser.
I suggest you look at the source for jetty. It is the simplest web server I can think of. If you want an ultra basic web server, you can do this with plain sockets, however the HTTP protocol is quite complex and using a web server library to handle all the details is likely to be the best approach.
If you want to be able to receive requests typed into a web browser, you need to do a couple things.
-Set the socket to listen on port 80
-Receive/parse/process HTML requests
-Return an HTML response across the socket
Rather than write the code for you, here is some pseudocode
//setup socket on port 80
socket.lisen();
while(true)
{
newsocket = socket.accept();
new thread(process(newsocket));
}
The most complicated part, I think, will be handling the HTML, processing the request, and generating a response. After that, just send it back over the socket.
Considering how many libraries are out there for this sort of thing, I wouldn't reccommend writing one from scratch.
The problem is that "web client" is just a browser, so you don't have direct access to TCP/IP.
Few options:
HTML5 WebSockets (only modern browsers)
flash helper (there are javascript wrappers)
java applet helper (there are javascript wrappers)
some tricks based on ajax pooling
Related
I think this might be a very stupid question.
I have 2 servers that are going to communicate with each other via HTTPS using XML packets.
My question is this, there is no webpage just a Java application. How would I get my Java app to run when it hears an XML packet from the other server? Are the messages to be sent via POST?
I know about HTTP protocols but my knowledge goes as far as webservers to sending the webpage to browsers, server to server communication w/o a webpage is baffling me. Can anyone point me to the right direction or better still to some codes that I might be able to look at.
Thank you.
You can use HttpClient in your server applications so that they become clients to each other. Then maybe simply implement REST services for the 'clients' to call.
I would suggest trying this out firstly using HTTP before trying it in HTTPS.
Of course there are other technologies you could try E.g JMS or SOAP.
I am practicing some web-apps technology. I setup Apache HTTP server which provides HTTP page with JNLP/FlashFX file with simple data-form to fill by the user. My first idea was to send/receive the data from JNLP with help of UDP (I just serialize object inside datagrams). To be more specific:
Apache provides a static HTTP and JNLP/FlashFX (HTTP is just to deploy JNLP)
JNLP communicates via UDP with server
server runs simple Java program to send/receive UDP packets to JNLP
My problem is when I access the page from 'client' browser machine a firewall asking if I want to allow/deny the access to network from 'java'. No doubts this is normal, but I think no one is expecting this from a web page.... I want to change this approach and use existing HTTP protocol.
QUESTION UPDATE
As far as I understood with HTTP protocol we have couple of methods which are used to communicate with the server (GET, PUT, POST .... ) and provided by Apache service.
I would like to use this for data exchange like this:
JNLP sends some serialized data with HTTP methods
Apache will redirect some (or full) traffic to my Java program
My Java program will answers via Apache to my JNLP
How can I do that?
Since I'm a newbie to Java web server, I don't know where to begin to create a server in Java to reply from Android devices request( A WEB SERVER NOT AN ACTUAL SITE).
Which technology should I use and which document should i read?. I have searched on the Internet about Java but i found out many technologies and i don't know which one i can use.
Spring MVC,JSF,JSP,Struts,Servlet..............
Hope this question clear enough for you to reply. Thank you
It doesn't matter what be process android devices requests on server side. In this case main idea is http communication - client sends request, server process this request and sends response to client, and you can use any technology on server side - java, php etc.
If you prefer java I can suggest you try servlets, it is simplest technology of those you mentioned above.
Well, I've been playing around with php for the last week or so, and I am wondering how I would use it to get data from a Java application. i.e: the php script sends a request to the server and asks for a response. In my particular case I want to do just that: I would like to have a java application waiting, and the php would "ask a question" to detemine if it is on or off (the server would not respond if it is off, and would if it is on - type of thing).
My question is: How do I communicate with a php script through java. How do I make requests to an application through php?
Any ideas?
If your PC is the server, then you can write a Java based server that listens to a socket -> then do something in java program when communication is received. Here's a simple example.
For real life implementation though, I'd suggest you use the PHP/Java Bridge instead. It's much faster and optimized for this sort of operations.
PHP has the ability to make web calls, open and communicate through sockets, SOAP, RPC, etc. It all depends on how your Java program would be listening.
Additionally, PHP can be written as a socket server, so your Java program could talk to it via web calls, socket or any other sort of server technology you choose.
I have to find a solution for uploading large content (10GB+) to a remote server. The uploader should be managed by a web page.
I didn't find any reliable solutions (or at least concepts) while searching the internet, so I came up with these scenarios. Can you provide any kind of feedback?
At the moment the common solutions in the market:
Http upload with post form (deprecated by speed and content size);
Flash uploader - create a ftp client, and deliver the content to the server;
Flash uploader - socket based transfer - treat the file as an InputStream and sent it through a socket opened with flash.
Java applet uploader - use Apache's ftp client, and send the content with ftp.
Java network socket - use the JRE to open a socket and send the content through a socket.
There is this other solution of using a c++ ftp client, that will be called from the Java Applet and use the c++ speed advantage (compared to java) to deliver the content.
Which one should be more efficient to deliver the content? Is there any other technology for doing what I'm looking for?
The limiting factor on the speed of upload will almost inevitably be the network - not the language you implement it in.
Do not use FTP if you can possibly avoid it.
Invoking an ftp client from an applet regardless of which client it is nor what language you imlpement the applet in is going to have to work outside the sandbox (which is not a trivial problem to work around). Why does it have to be implemented via a web page?
If you've got any control over the client being used, then websockets are probably the most sensible solution - but support is still patchy in browsers. There are lots of flash uploaders already written - and a few java ones.