Tuesday 15 September 2015

HTML5 Server Sent Events using Java Servlet 3 and tomcat -


I tried to run this code with TomCase in Eclipse but I did not get any output from the server in the client side. Should I include some libraries (jar files, js files ...) for the project? Thank you.

Here are two files:

  & lt; Html & gt; & Lt; Body Onload = "Register SSE ()" & gt; & Lt; Script & gt; Function Register SSE () {Warning ('Test 1'); Var Source = New Event Source ('http: // Local Host: 8080 / Hello / SSE'); Warning ('test2'); Source.onmessage = function (event) {document.getElementById ("Result"). InnerHTML + = event.data + "
"; }; /*source.addEventListener ('server-time', function) (warning ('EA';), true); * /} & Lt; / Script & gt; & Lt; Output id = "result" & gt; & Lt; / Output & gt; & Lt; / Body & gt; & Lt; / Html & gt;

servlet:

  import java.io. *; Import javax.servlet.http.httpServlet; Import javax.servlet.http.HttpServletRequest; Import javax.servlet.http.HttpServletResponse; Try the public class SST HTPreserve {Public Zero Depot (HTTPSvette Request Request, HTTPSvette Response Feedback) ({system.out.println ("SSE Demo"); response.setContentType ("Text / Event Section"); PrintWriter PW = ; Getitre (Int); Int i = 0; while (true) {i ++; Pw.write ("Event: Server-Time \ n \ n"); // 2 \ n 's Keep in mind, on the next line Also .pw.write ("data:" + i + "\ n \ n"); System.out.println ("data sent !!!" + i); if (i> 10; break); Pw.close ();} Hold (up) Ad e) {e.printStackTrace ();}} Public Zero doGet (HttpServletRequest request, HttpServletResponse response) {doPost (request, response);}}   

Which version of Tomcat I want ? And which connector type should be required?

_ WEB.XML _

  & lt; xml version = "1.0" encoding = "UTF-8"? & Gt; web-app xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance "Xmlns =" ​​http://java.sun.com/xml/ns/javaee "xmlns: web =" http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd "xsi: schema Location = "http://java.sun.com/xml/ns/javaee http: //java.sun. Com / xml / ns / javaee / web-app_3_0.xsd "id =" WebApp_ID "version =" 3.0 "& gt; & lt; display-name & gt; demo & lt; / display-name & gt; & lt; Welcome-File-List & gt; & lt; welcome-file & gt; index.html & lt; / welcome-file & gt; & lt; welcome-file & gt; index.htm & lt; / welcome-file & Gt; & lt; welcome-file & gt; index.jsp & lt; / welcome-file & gt; & lt; welcome-file & gt; default.html & lt; / welcome-file & gt; & lt; welcome -File & gt; default.htm & lt; / welcome-file & gt; & lt; welcome-file & gt; default.jsp & lt; / welcome-file & gt; & lt; / welcome-file-list & Gt; & lt; servlet & Gt; & lt; servlet-name & gt; SSE & lt; / servlet-name & gt; & lt; servlet category & gt; SSE & lt; / servlet category & gt; & lt; load-on-startup & gt; ; 1 & lt; / load-on-startup & gt; & lt; / servlet & gt; & lt; servlet-mapping & gt; & lt; servlet-name & gt; SSE & lt; / servlet-name & gt; ; & Lt; url pattern & gt; * html. & Lt; / url pattern & gt; & lt; / servlet-mapping & gt; & lt; / web application & gt;    

I do not know about event resources, but I understand that if you enable To open a resource in the browser, you should take a step further.

The Web Make changes as under XML: -

  1. & lt; Servlet-class & gt; Provide the complete path to your servlet with the package name - com.test.sse
  2. & lt; Url-pattern & gt; You can / * here it will work for every URL.

    After making this change, try opening the resource in the browser and see if it helps.

No comments:

Post a Comment