Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

May tomcat servlet handle ws & http requests together

 
Evgeny Nel
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All!

I need to make servlet that handle as HTTP as WS requests.

When browser(s) make request like ws://<ip>:8080/myapp/myservlet - my servlet must open WS-connection with browser(s).
When 3rd-party software ( Oracle database in my case ) make request http://<ip>:8080/myapp/myservlet - my servlet must send message to all ws-clients.

is it possible?
May you give me any example?

Apache Tomcat 7.0.67

 
Tim Moores
Bartender
Posts: 3137
50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The ws protocol is handled by classes extending the EndPoint class, not by servlets.

My guess would be that you can't map 2 resources to the same URL; at least I would hope that's not possible. But I'd anyway advise to use different URLs for different protocols, for clarity's sake.
 
Evgeny Nel
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Tim.
Thank you for your attention.

Tim Moores wrote:The ws protocol is handled by classes extending the EndPoint class, not by servlets.

My guess would be that you can't map 2 resources to the same URL; at least I would hope that's not possible. But I'd anyway advise to use different URLs for different protocols, for clarity's sake.


I can use different URL.
But then I must will be create two classes : the first for http , the second for websocket. When I receive HTTP-request in HTTP-class How can I notify WS-class to send messages to browsers ?

 
Tim Moores
Bartender
Posts: 3137
50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you would have 2 classes.

A full example of how to implement web sockets is part of the "examples" web app that comes with Tomcat. And there's a ready-to-deploy war file in http://www.coderanch.com/t/633446/Tomcat/Tomcat-creating-connection-web-socket
 
Tim Holloway
Saloon Keeper
Posts: 18367
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Web Services in Tomcat are typically managed by a dispatcher servlet for a pre-written/pre-debugged framework such as Apache CXF.

There's nothing at all magic about that servlet. A web service is simply an HTTP request conforming to an agreed convention with the idea that probably a logic component on the client will be processing the response, and therefore the overhead of a full displayable web page isn't necessary - you just want to initiate an action and/or return data. If this sounds like what AJAX does, it's because there's a lot in common there.

So you can write dual-purpose servlets that can do both stand-alone web pages and simple web-service data operations based on the parameters and other "decorations" of the URL being dispatched to those servelets, and I have done so. Including some where multiple return data formats are supported, such as YAML and XML.

If you actually wanted a stock web services framework to operate like that, you'd probably have to make the target servlet redirect incoming web service requests (as defined by you) to the stock web services servlet, although it's wise to read their documentation first. In some cases, in fact, it might even be suitable to subclass the stock dispatched servlet, but again, read the instructions!
 
Tim Moores
Bartender
Posts: 3137
50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Holloway wrote:Web Services

Evgeny is asking about web sockets, though, not web services. While it may be possible to implement web sockets via servlets (I'm not sure), all the examples I've seen use an EndPoint.
 
Tim Holloway
Saloon Keeper
Posts: 18367
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Moores wrote:Evgeny is asking about web sockets, though, not web services.


Which is why we ask people to avoid using abbreviations and acronyms where reasonable. I think I counted 6 entirely different definitions of the acronym "CRA" in various IBM program products once. Avoiding ambiguity is the first step towards problem resolution.

Ultimately, every URL-triggered process in a web application goes through a servlet. Either an explicit servlet, a JSP compiled into a servlet, a framework servlet, or (at least in the case of Tomcat), the server's built-in Default Servlet. Servlets can, of course, communicate with non-URL processes, such as batch services initiated and/or managed from the ServletContextListener, in which case the URL requests are indirect, but still a "servler" communicates with the background process.

If I haven't forgotten too much on the topic, a Websocket is based on having the webapp initiate a network connection as opposed to the traditional approach of being the target, and I seem to recall some examples where that was done by handing over to batch services, since process may take a significant amount of time.

The key consideration is that a servlet should be something that receives a request and returns a response as fast as reasonably possible. Anything that would cause an extended delay - websocket or otherwise - should be handed off to a backend process.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic