Servlet Push
HTTP Server Push
HTTP server push is a mechanism for sending data from a web server to a web browser. HTTP server push can be achieved through several mechanisms. Http Server push also known as HTTP streaming.
Most web servers offer this feature by Non parsed Headers ("Server Push"). Another way of achieving server push is by using the special MIME type called multipart/x-mixed-replace
Most web servers offer this feature by Non parsed Headers ("Server Push"). Another way of achieving server push is by using the special MIME type called multipart/x-mixed-replace
Server Pull and Push
Usually browser asks for a page or image or an object from the server and the server sends the response to the browser. This is called 'Pull' method the most common method in webserver technology. At times the server itself can send data to the browser. Its required sometimes that the server has to push the data to its clients. For example consider the cricket score card or email web clients. These kinds of data change consistently, so server has to push the content to client to synchronize the client with the server data.
Client side Refresh methods
In traditional websites the synch or data update is done using one of the below
- Refresh tag in the HTML metadata
- JavaScript countdown timer to reload the data
Advantages of server push
- Reduced page refresh
- Limited latency
- Quick updates
With server push, the socket connection between the client and the server remains open until the last portion of response is committed. This enables the server to send the updates quickly.
Disadvantages of server Push
- Browser support
- Keep alive connection overhead.
MIME type multipart/x-mixed-replace
Servlet push is achieved by using the MIME type multipart/x-mixed-replace.
From Wikipedia http://en.wikipedia.org/wiki/MIME
The content type multipart/x-mixed-replace was developed as part of a technology to emulate server push and streaming over HTTP.
All parts of a mixed-replace message have the same semantic meaning. However, each part invalidates - "replaces" - the previous parts as soon as it is received completely. Clients should process the individual parts as soon as they arrive and should not wait for the whole message to finish.
Originally developed by Netscape,[5] it is still supported by Mozilla, Firefox, Chrome,[6] Safari, and Opera, but traditionally ignored by Microsoft. It is commonly used in IP cameras as the MIME type for MJPEG streams.[7]
Sample code.
Sample code is given below to explain how servlet push works. This code simply prints the current time on the output (browser) and updates the time every second.
package com.servlet; import java.io.IOException; import java.util.Calendar; import javax.servlet.*; import javax.servlet.http.*; public class TimeServlet extends HttpServlet { private static final long serialVersionUID = 1L; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ServletOutputStream out = response.getOutputStream(); // Binary data // Set the MIME type response.setContentType("multipart/x-mixed-replace;boundary=eor"); for (int i = 0; i < 10; i++) { // Start the response i out.println("Content-Type:text/plain"); out.println(); out.println("Time :" + getTS()); // End the response i out.println(); out.println("eor"); out.flush(); sleep(1000); } out.println("Content-Type:text/plain"); out.println(); out.println("clock is done!"); out.print("eor"); out.flush(); // close the response out.close(); } private static String getTS() { Calendar cal = Calendar.getInstance(); return cal.get(Calendar.MINUTE) + ":" + cal.get(Calendar.SECOND); } private void sleep(int ms) { try { Thread.sleep(ms); } catch (InterruptedException e) { } } }
ReplyDeleteAwesome informations that you have shared for us.I eagerly waiting for more updates in future.
Hadoop Training in Chennai
Big data training in chennai
Big Data Course in Chennai
JAVA Training in Chennai
Python Training in Chennai
Selenium Training in Chennai
hadoop training in Annanagar
big data training in chennai anna nagar
Big data training in annanagar
Wonderful blog...!!! I like your post and I learn lot of info from your great post.
ReplyDeleteEmbedded System Course Chennai
Embedded System Courses in Chennai
Excel Training in Chennai
Corporate Training in Chennai
Tableau Training in Chennai
Oracle Course in Chennai
Oracle DBA Training in Chennai
Unix Training in Chennai
Power BI Training in Chennai
Placement Training in Chennai