Embedded Jetty 9 HTML form sending data to application - java

I have created a simple login form using a servlet receiving information via HTTP POST trough an SSL connection on an embedded Jetty 9 server. The servlet receives the information as it's supposed to but I've been unable to find out how the servlet should communicate with the application jetty is running in.
The documentation for using Jetty in embedded mode seems to be very lacking and I havn't had any success doing a general search on the subject either. In short:
How do I get information from an HTML login form hosted on an embedded Jetty 9 server to the application Jetty is running in?

For the embedded mode examples, see the git repository.
http://git.eclipse.org/c/jetty/org.eclipse.jetty.project.git/tree/examples/embedded/
For communicating out from the servlet to your application, you have many options, but they all wind up being event driven at the application side.
2 Example Options (there are many ways to accomplish this, this is just two quick examples):
Simply call methods in MyApplication singleton.
Put a reference to the MyApplication object into a static MyServlet.app field.
Call your app.somethingHappened() in the servlet when you want to communicate what has happened.
Listener metaphor.
Create MyServletListener with the methods representing the events you want the application to know about
Make MyApplication implement MyServletListener
In MyServlet.init() grab a reference to MyApplication
In the various MyServlet.doGet() or MyServlet.doPost() style handling calls, call the appropriate MyServletListener events for the action you want to communicate.
Note: these are naive examples, and don't deal with threads and threading.
But that's another topic entirely.

Related

Call WebService method created with Netbeans and Glassfish from browser(localhost)

This may sound like a dumb question but i am new to web services.
I followed this tutorial and successfully created a Calculator web service up to the point where i created a local client application that consumed the web service method (A java class with main method which calls the web service method).
My question now is how can i access this method via my browser and set the parameters there, just get an XML/JSON result.
My guess is that i am missing something and i have to publish the web service somehow.
Any solutions, links are highly appreciated.
You have hosted your webservice on glassfish server. Now you want to consume it using internet browser like you did it using test webservice option in net beans.
That means you need to built your own client which runs on web browser.
You need to built a webpage having two input fields in it and a button as result and whenever you hit this button call your webservice and display it's result. You can follow any simple tutorial of web client or application and again you can deployed it on glassfish or apache server.
Also you can use soapui (a client to consume web services) to import your wsdl and access its methods, providing inputs and get result in form of soap.

2 Tomcat Instances - Communication between 2 applications

I have one webserver with 2 instances of tomcat running. On each tomcat instance I have multiple web apps or web services.
What is the best way to call a function (or trigger some event with parameters) from a webapp of the first tomcat server on a webapp running on the second tomcat server. If it's for example a call using a url with parameters then this call should be secure and not accessible from outside the server.
I've read something about getting the servlet context but is this possible on different tomcat instances? Im thinking that this is only possible with webapps running in the same instance.
I dont want to use CORBA, RMI or SOAP because this is a bit oversized for my problem ... that is what Im thinking :)
Code examples are welcome. Thank you!
The ServletContext is only valid within the same container and can't be shared between two JVMs. The simplest method to do what you're asking is to just use some variety of RPC between the two containers, and RMI doesn't seem like particular overkill. The other usual approach would be a simple HTTP Web service (note the lowercase "s") that invokes your logic in the receiving container.
Spring's HTTPInvoker is great for this. You can use a Java interface, and your code on each instance doesn't need to know the call is remote - it just calls Java methods.
For security, you can use the Sun HTTP server on a different port (instead of using a servlet within Tomcat) and listen only on localhost.
Have a look here
http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/remoting.html#remoting-httpinvoker
Use Simple REST services , not that much secured .

Stop one of Http method Online

I have a couple of http methods in my application which is hosted in JBoss.
And Now I am trying to find some kind of hot-way to disable one of them,like click a button in a certain of page or calling a certain of http method. What i means of 'disable it' is making any web client which intends to send get/post request to it will go to failure . Maybe we can say the web client will got a http 404 response.
Can anybody give me some solutions? Thanks.
I think JMX would be appropriate for this situation.
You can pretty easily create an MBean (Managed Bean, a component of JMX) with Managed Attributes corresponding to boolean's for each of the endpoints you want to be disable-able. Registering it is the hard part, but there are libraries out there that make working with JMX easier. Spring has good support for setting up and working with MBeans.
As far as a JMX client goes, I usually use VisualVM, which ships with the JDK. From it, you can invoke methods on your MBeans at runtime, or even change their properties.

Implementing a Hl7 parser listener to java

Background
I'm writing a medical record app for a friend who is a Doctor. I was told to write a listener in the app that awaits HL7 messages. That way a hospital can send out HL7 messages and my listener will catch them. So I came to the HAPI site and viewed this example. What I understand from it is that it's creating a server to listen for a message.
I'm developing this in Eclipse using JSF 2.0 on Tomcat 7.0. Where does one normally put this kind of listener in a project with JSF? I've tried searching online for this answer and found nothing!
My question
I know this code goes inside a class. When the class gets called the socket will be "turned on" and it's going to wait for a response. So I want to call this class as soon as the project is deployed. How is that done? How do I call that class only once (when the app is starting) in order to turn on the listener?
Any and all help is greatly appreciated! And if I'm not being clear on something let me know!
You don't normally get port listeners running inside an application hosted in Tomcat. You're usually best to keep the two things separated. In the main, web servers aren't meant to run separate threads of execution outside of their control.
You could consider using something like Spring Integration, JBossESB or Apache Camel to receive the messages and process them into a database, file folder (or whatever) that your Tomcat hosted web application then allowed you to manipulate. The ESB container could be hosted in the same JVM process as Tomcat but I wouldn't take that approach myself - I'd have a separate one doing the message processing and another running the webapp.
If you really wanted a "single application" you could consider creating a Java application that kicked off a listener as per the example you have, then started up an embedded version of Tomcat.
If you really really wanted to run it inside Tomcat, as part of web application itself, you could create a class which did the listening and get it loaded into the Application context of the web application. You can do this by adding an instance of it into the appl context within an autoloaded servlet - use <load-on-startup>1</load-on-startup> within the servlet definition. You'd code the servlet to check if there was already an instance in context before adding a new one (on the off-chance it was ever manually invoked), or go down the Spring container route to manage this object as a singleton.
* EDIT: 20120114T004300Z *
Apache Camel is an example of a routing engine that might be used by an Enterprise Service Bus (ESB) such as Apache ServiceMix which allows multiple applications to interoperate by exchanging messages. You'd only use a fraction of the functionality availability for this app by the sounds of it. For what you're doing you might just be able to use Camel capability embedded in Spring, for example.
In essence, the ESB runs "adapters" (or endpoints) - one types of which would by the socket "listener" you talk about here, or might be watching a folder for files to arrive, or polling a database table for rows to appear, or waiting on a JMS queue, etc. The transport (the means by which the "message" (in your case the HL7 file) arrives becomes abstracted away from the functionality of the application itself. The adapter puts the message onto a channel which can be configure to transform the message en-route. Camel actually ships with a HL7 component which can understand the HL7 file format and unmarshal it into a HL7 model. (It also gives you the listener/adapter you need). You'd then set up routing in the ESB to pass that model into a "consumer" Java class that does whatever you need to do with it.
If you're dealing with "standard" transports, protocols and message types most of the file receipt, parsing, and routing is just handled by declarative configuration of the ESB rather than coding.
Your Tomcat webapp can run completely autonomously to this message handling. As mentioned, there are various deployment options as to how exactly you'd do this - including loading Camel inside a Spring container hosted in Tomcat by your webapp if you want to.
Apologies if this is a bit woffley. Take some time to read around the subject on the web, given that HL7 is a standard you'll probably find a lot of code/components already out there that might save you a lot of time in re-implementing the basic file handling so you can concentrate on the value-add webapp for your friend.
A "Listener" is just a class which listens on an open port. In Java, this is mostly done through the Socket API, although you may find a library that better suits your purpose.
The Java Tutorial has some examples here:
http://docs.oracle.com/javase/tutorial/networking/sockets/index.html
In this case, you'd be writing a server (the listening half of a client-server arrangement), whereas the Hospital system sending the message would play the role of client.
Once you're listening on the port, then HL7 messages arrive as plain text onto that socket's inputstream. You can either hand-parse the message (viable if you're only interested in one or two details from a message) or if you're planning on handling dozens of types of messages you can look into one of the HL7 parsing libraries out there.
Keep in mind though, that different implementors of HL7 messages can sometimes send data in subtly different arrangements. (Many users treat HL7 as a 'recommendation' rather than a 'standard', unfortunately!) If you're planning on supporting lots of different feeds from lots of different providers, you'd be much better off using a middleware layer like MirthConnect to handle the parsing and translation of messages into something your application is designed to understand.
Over a year old so you probably figured it all out, but an Enterprise Service Bus (ESB) is a type of middleware (when you think of software, there is back-end i.e. Database/Analytics/Admin Tools and front-end i.e. App/WebApp/GUI displayed to and interacted with end-user), middleware sits in between and helps perform integration or separation/coordination of tasks. Apache ServiceMix (an ESB which contains Apache Camel routing engine) is probably what you want and can be used to implement a number of different Enterprise Integration Patterns such as "Message Routing" (the one you want).
Apache Camel has a built-in HL7 v2 Message parser (uses HAPI) which is the Tab-Separated variant of HL7: http://camel.apache.org/hl7.html
For HL7 v3 messages which are in XML format you can use the toolkits available here under v3 utilities:
http://www.hl7.org/participate/toolsandresources.cfm?ref=nav
There are both server (message listening and reading) and client (message creation and sending) examples.
"Listener" is usually an event listener in Java.
In the example you posted a link to, you have a server class, which handles the business of opening a network socket and waiting for messages to arrive.
The Application objects are the event listeners. These are added to an internal collection of the server class (in this case, with additional parameters that tell the server which listeners to route which classes of HL7 message to).
Each Application class must implement a particular interface - this constitutes the event listener. The SimpleServer class will call the methods of this interface ; processMessage() ; in order to perform actions based on message content, you write a class that implements this interface, and pass instances of it to the server class. In the processMessage() method, you perform all the required actions.
Since you can register multiple listeners, you can implement a number of actions, e.g. you could have two listeners for ADT A01 messages (admit patient) ; one that booked them in, and one that assigned them a bed.
I would suggest looking at Mirth Connect http://www.mirthcorp.com/community/mirth-connect as your HL7 message integration engine. Internally it makes use of HAPI.

How to allow interaction between Java application and user front Web interface

I need to create a user facing web interface (perhaps with HTML5 and Javascript) that allows a user to draw lines using a mouse. The interaction would involve mouse drag, clicks, etc. I need to send these inputs to a Java application on a remote machine and get back some result and update the web page the user's drawing on. So this would require a two way communication.
Since this is a proof of concept prototype, I need a solution that's easy and simple, and hopefully fast since the user would like to see the update quickly. What technology do you recommend to allow the communication between the web interface and java application? I was thinking about writing a simple server in Java and talk to the remote application using JMS... not sure this is the right direction.
Thank you for your insights.
Any servlet container, such as Tomcat, JBoss, Jetty, GlassFish or WebSphere would do.
I'm not sure JMS is a good fit here.
The browser would communicate with the server the way all web-apps do, via http requests. So, on the server you would use servlets, or some frameworks that builds on top of servlets, running in the container of your choice. You webapp would periodically send an xml http request (XHR/AJAX) to update the state of the drawing. Or it would do it when the user wanted to save their design.
Keep in mind what you are describing is a Web Application. This means that an application is running in the browser, so it can maintain its own state independent of the server. It just needs to sync up every now and again. You don't need to continuously send requests to the server.
it can be done also using RIA: actionscript3/flash+xml socket+java server. You can handle events via actionscript3 and then send parameters to server and after receive answer. There is lot of source for as3 drawing api after you can modify for socket connection.
have you tried this ? Look at the demo section on the shared canvas.
http://jwebsocket.org/

Categories