JavaEE WebApp Release
The process for creating, deploying, and executing a web application can be
summarized as follows:
1. Develop the web component code.
2. Develop the web application deployment descriptor, if necessary.
3. Compile the web application components and helper classes referenced by
the components.
4. Optionally, package the application into a deployable unit.
5. Deploy the application into a web container.
6. Access a URL that references the web application.
HTML5 for the client
Pure HTML5 clients are all we need.
Servlet
Still core and getting better
… more efficient technologies are in JavaEE7
Servlet3.1.
Asynchronous Servlets
/** approccio sincrono (classico) **/
var dato = ottieniDatoDaRemoto(url);
alert(dato);
/** approccio ad eventi (asincrono) **/
ottieniDatoDaRemoto(url, function(dato) {
alert(dato);
}); //la funzione ritorna subito
JavaEE6 Servlet 3.0
Servlet 3.0 allowed asynchronous request processing but only traditional I/O was permitted. This can
restrict scalability of your applications. In a typical application, ServletInputStream is read in a while
loop.
public class TestServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
ServletInputStream input = request.getInputStream();
byte[] b = new byte[1024];
int len = -1;
while ((len = input.read(b)) != -1) {
. . .
}
}
}
JavaEE7 Servlet 3.1
If the incoming data is blocking or streamed slower than the server can read then the server thread is
waiting for that data. The same can happen if the data is written to ServletOutputStream.
This is resolved in Servet 3.1 (JSR 340, released as part Java EE 7) by adding event listeners -
ReadListener and WriteListener interfaces. These are then registered using ServletInputStream.
setReadListener and ServletOutputStream.setWriteListener. The listeners have callback methods that
are invoked when the content is available to be read or can be written without blocking.
Java WebServices
● JAX-WS: addresses advanced QoS requirements commonly occurring in enterprise
computing. When compared to JAX-RS, JAX-WS makes it easier to support the WS-
* set of protocols, which provide standards for security and reliability, among other
things, and interoperate with other WS-* conforming clients and servers.
● JAX-RS: makes it easier to write web applications that apply some or all of the
constraints of the REST style to induce desirable properties in the application, such
as loose coupling (evolving the server is easier without breaking existing clients),
scalability (start small and grow), and architectural simplicity (use off-the-shelf
components, such as proxies or HTTP routers). You would choose to use JAX-RS
for your web application because it is easier for many types of clients to consume
RESTful web services while enabling the server side to evolve and scale. Clients
can choose to consume some or all aspects of the service and mash it up with other
web-based services.
JAX-WS steps
Official JEE6 tutorial
The basic steps for creating a web service and client are as follows:
1. Code the implementation class.
2. Compile the implementation class.
3. Package the files into a WAR file.
4. Deploy the WAR file. The web service artifacts, which are used to communicate with
clients, are generated by the GlassFish Server during deployment.
5. Code the client class.
6. Use a wsimport Ant task to generate and compile the web service artifacts needed to
connect to the service.
7. Compile the client class.
8. Run the client.
JAX-WS notes
TomEE container is configured and packaged with Apache CXF .
TomEE leave the use od wsimport optional.
Whether or not you use annotations it's important to understand the performance impact they can
have on a server at startup. In order for the server to discover annotations on classes, it must load the
classes, which means that at startup, a server will look through all the classes in WEB-INF/classes and
WEB-INF/lib, looking for annotations. (Per the specification, servers don't have to look outside these
two places.) You can avoid this search when you know you don't have any annotations by specifying a
metadata-complete attribute on the <web-app> root like this:
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
version="3.0" metadata-complete="true">
</web-app>
Run a TomEE example
* Create a Dynamic Web Project
* Add web.xml, beans.xml, index.html
* Add Unit4 library
* Add your package
* Create example sources
RESTFull WebServices
Resource identification through URI: A RESTful web service exposes a set of resources that identify the
targets of the interaction with its clients. Resources are identified by URIs, which provide a global addressing
space for resource and service discovery.
Uniform interface: Resources are manipulated using a fixed set of four create, read, update, delete
operations: PUT, GET, POST, and DELETE. PUT creates a new resource, which can be then deleted by
using DELETE. GET retrieves the current state of a resource in some representation. POST transfers a new
state onto a resource.
Self-descriptive messages: Resources are decoupled from their representation so that their content can be
accessed in a variety of formats, such as HTML, XML, plain text, PDF, JPEG, JSON, and others. Metadata
about the resource is available and used, for example, to control caching, detect transmission errors,
negotiate the appropriate representation format, and perform authentication or access control.
Stateful interactions through hyperlinks: Every interaction with a resource is stateless; that is, request
messages are self-contained. Stateful interactions are based on the concept of explicit state transfer. Several
techniques exist to exchange state, such as URI rewriting, cookies, and hidden form fields. State can be
embedded in response messages to point to valid future states of the interaction.
JAX-RS Code
@Path("/greeting")
public class SimpleRest {
@GET
public String message() {
return "Hi get REST!";
}
@POST
public String lowerCase(final String message) {
return "Hi post REST!".toLowerCase();
}
}
JavaBeansComponents
Web components are supported by the services
of a runtime platform called a web container. A
web container provides such services as request
dispatching, security, concurrency, and lifecycle
management. A web container also gives web
components access to such APIs as naming,
transactions, and email.
Enterprise JavaBeans
Enterprise beans are Java EE components that
implement Enterprise JavaBeans (EJB)
technology. Enterprise beans run in the EJB
container. The EJB container provides system-
level services, such as transactions and security,
to its enterprise beans.