English | 2015 | ISBN: 978-1-78398-352-0 | 364 Pages | PDF +Code | 11 MB
NetBeans has several features that greatly simplify Java EE development, but with many features and great flexibility, Java developers can become overwhelmed by the options available in NetBeans.
Java EE 7 Development with NetBeans 8 takes you through the most important parts of Java EE programming and shows you how to use the features of NetBeans that will improve your development experience through clear, careful instructions and screenshots. This book will take you through the major Java EE APIs and how to get them working in the NetBeans environment.
This is a practical guide that covers how to leverage NetBeans 8 functionality to quickly and easily develop enterprise applications compliant with the Java EE 7 standard.
Java API for JSON Processing
In order to read and parse JSON data using the JSON-P streaming API, we need to obtain an implementation of the JsonParserinterface. The Jsonclass has two overloaded versions of a createParser() method we can use to obtain a JsonParserimplementation. One version of Json.createParser() takes an instance of java.io.InputStream(or one of its subclasses) as its sole parameter, and the other version takes an instance of java.io.Reader(or one of its subclasses) as its sole parameter. In our example, we use the second version, passing an instance of java.io.StringReader(which extends java.io.Reader) that contains our JSON string as a parameter.
Once we obtain a reference to JsonParser, we invoke its hasNext() method in a while loop. The JsonParser.hasNext()method returns trueif there are more property names or values to read from the JSON string, otherwise it returns false.
Inside the whileloop, we invoke JsonParser.next(). This method returns an instance of the JsonParser.Eventenum. The specific value of the JsonParser. Eventenum we get from JsonParser.next()lets us know what type of data we are reading (key name, string value, numeric value, and so on).In our example, our JSON string contains only string and numeric values, so we only check for those two value types by comparing the JsonParser.Eventinstance we got from JsonParser.next() against Event.VALUE_STRINGand Event.VALUE_NUMBER, respectively. We also check for a JSON key name by comparing the obtained value against Event.KEY_NAME. Once we have read a key/value pair combination from our JSON string, what we do with it depends on our application requirements. In our example, we simply populate a hash map using the corresponding values from our JSON string.
Java API for WebSocket
A Java class that processes WebSocket requests on the server side is called a server endpoint. As we can see, developing a WebSocket server endpoint using the Java API for WebSocket requires very little code.
We can designate a Java class as a server endpoint by annotating it with the @ServerEndPointannotation. Its value attribute indicates the Uniform Resource Identifier (URI) of the server endpoint, and clients (typically client-side web applications) access the server endpoint via this URI.
Any method annotated with the @OnMessageannotation is invoked automatically any time a client sends a message to the WebSocket server endpoint. Methods annotated with this annotation and expecting textual data must take a string argument, which will contain the contents of the message sent from the client. The contents of the message can be anything; however, it is a common practice for the clients to send JSON-formatted data. In the NetBeans sample application, a simple string is passed to the echo()method.
The value returned by the @OnMessageannotated method is sent back to the client. A typical use case is to send a JSON-formatted string; however, in this simple example, the string that was received as an argument is sent back to the client.