Hiển thị các bài đăng có nhãn Java EE 5. Hiển thị tất cả bài đăng
Hiển thị các bài đăng có nhãn Java EE 5. Hiển thị tất cả bài đăng

Thứ Tư, 21 tháng 1, 2009

Using RESTClient with Java's HttpServlet

I previously blogged about using RESTClient with the Sun JVM-provided HTTP Server. As I stated in that blog posting, I intend to use these tools to demonstrate HTTP and REST concepts at Rocky Mountain Oracle Users Group (RMOUG) Training Days 2009. I also intend to use the tried and proven Java HttpServlet in this presentation.

When I started using the Java servlet back in 2000, I read about the HttpServlet methods that correspond to the significant HTTP methods, but I didn't really override any methods in the majority of my servlets other than doGet and doPost. Looking back on the early servlet-focused books and articles, it is not surprising that this was the case because these materials focused on those two methods as well.

Although the Javadoc-based API information for Java EE 5 briefly discusses alternative methods in HttpServlet that can be overridden, many tutorials available even today focus on doGet and doPost. For example, the highly useful Java Servlet API Tutorial has an HTTP Support section that mentions the other methods but focuses on doGet, doPost, doHead. Similarly, the Java EE 5 Tutorial (PDF) also focuses on use of the HttpServlet's doGet method.

Roy Fielding's REST dissertation has been a significant catalyst in motivating many of us to think about HTTP differently. Specifically, we are now more inclined to think about the other methods supplied with HTTP. While many tools and browsers do not provide great support for all the major HTTP methods, the Java servlet has provided this support for some time and is an excellent mechanism for implementing and simulating operations that take advantage of the major HTTP methods.

It is very easy to take advantage of HttpServlet's support for major HTTP methods. This is demonstrated in the next code sample for the Java servlet class SimpleHttpServer.java:

SimpleHttpServer.java


package dustin.flex.rest;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
* Simplistic Java EE server-side application intended for demonstration of
* HTTP methods often use in REST-based applications.
*
* @author Dustin
*/
public class SimpleHttpServer extends HttpServlet
{
/**
* Servlet method responding to HTTP GET methods calls.
*
* @param request HTTP request.
* @param response HTTP response.
*/
@Override
public void doGet( HttpServletRequest request,
HttpServletResponse response ) throws IOException
{
final PrintWriter out = response.getWriter();
out.write("GET method (retrieving data) was invoked!");
}

/**
* Servlet method responding to HTTP POST methods calls.
*
* @param request HTTP request.
* @param response HTTP response.
*/
@Override
public void doPost( HttpServletRequest request,
HttpServletResponse response ) throws IOException
{
final PrintWriter out = response.getWriter();
out.write("POST method (changing data) was invoked!");
}

/**
* Servlet method responding to HTTP PUT methods calls.
*
* @param request HTTP request.
* @param response HTTP response.
*/
@Override
public void doPut( HttpServletRequest request,
HttpServletResponse response ) throws IOException
{
final PrintWriter out = response.getWriter();
out.write("PUT method (inserting data) was invoked!");
}

/**
* Servlet method responding to HTTP DELETE methods calls.
*
* @param request HTTP request.
* @param response HTTP response.
*/
@Override
public void doDelete( HttpServletRequest request,
HttpServletResponse response ) throws IOException
{
final PrintWriter out = response.getWriter();
out.write("DELETE method (removing data) was invoked!");
}

@Override
public String getServletInfo()
{
return "Server-side application demonstrating HTTP methods.";
}
}



This class can be referenced in the Java EE web descriptor file (web.xml) as shown next.

web.xml


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5" xmlns="http://java.sun.com/xml/ns/javaee">

<display-name>Simple Servlet Example Demonstrating Http Method Support</display-name>
<description>Demonstrates HttpServlet Support of HTTP Methods</description>

<servlet>
<servlet-name>SimpleHttpServer</servlet-name>
<servlet-class>dustin.flex.rest.SimpleHttpServer</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>SimpleHttpServer</servlet-name>
<url-pattern>/httpServer</url-pattern>
</servlet-mapping>

</web-app>


If we build the servlet shown above and assemble it with the web.xml file shown above into a WAR file that is deployed to a Java EE web server, we can then have clients contact the simple servlet via one of the four HTTP methods GET, POST, PUT, and DELETE (assuming the client is capable of using these different HTTP methods). In fact, we'll also see later in this blog post that even the HTTP methods TRACE, OPTIONS, and HEAD are supported by the HttpServlet. I did not override them in my servlet above because the default implementations of these methods (doTrace, doOptions, and doHead) typically work well for what we need.

For the example in this blog posting, I am deploying the assembled WAR with the servlet and web.xml file shown above to GlassFish (though any Java EE web/application server will work) using a context of restful. Coupled with the path defined in the web descriptor file and using the default port on GlassFish, my overall URL for accessing my servlet is http://localhost:8080/restful/httpServer. When I place this URL in my web browser, the results appear as shown in the figures below for Firefox and for Google Chrome.





Note that in both cases, the default method used for the browser communication with the servlet was HTTP GET. I will now begin using RESTClient to easily communicate with this servlet with alternative HTTP methods. This tool is provided as an executable JAR and is easily used, but additional instructions on using it are available here and here.

Because the browsers automatically demonstrated use of a client using HTTP GET to communicate with the server, I will begin showing RESTClient examples with GET as well. The following two screen snapshots demonstrate RESTClient showing the headers and then the body of the response. The HTTP response information is shown at the bottom of the UI.

HTTP GET Response Headers



HTTP GET Response Body




The two screen snapshots above use RESTClient to show the headers and body of the HTTP GET response. For POST, PUT, and DELETE, I will only show the bodies.


HTTP POST Response Body




HTTP PUT Response Body




HTTP DELETE Response Body




The response body for the TRACE method is shown next.

HTTP TRACE Response Body




The HTTP HEAD method works like the GET, but intentionally does not include a body. Similarly, the most interesting portion of the response for OPTIONS is also in the header. Because the HEAD's body is empty and the header looks like that for GET, I don't show it here. For OPTIONS, I show the headers rather than the bodies of those responses next. Note that the options provided by the server are shown in the "Allow" header field of this response.


HTTP OPTIONS Response Headers




As the "Allow" header field in the OPTIONS response indicates, the server supports the HTTP methods GET, HEAD, POST, PUT, DELETE, TRACE, and OPTIONS.


Conclusion

Although Java servlets are more than a decade old, they remain a powerful and highly useful technology. While they may not be as trendy as younger and more currently hip technologies, this blog posting serves as a reminder of how relatively easy it is to apply their rich features to modern-day development. Servlets have supported the major HTTP methods for many years and because of the HttpServlet's close association with HTTP, it is not surprising that the HttpServlet was ready for action when REST became a widely popular topic.

Thứ Ba, 13 tháng 1, 2009

Java EE and Flex: A Compelling Combination

My article Java EE and Flex: A Compelling Combination, Part 1 was published on JavaWorld today. I have presented on Java EE+Flex at Collaborate08 and at the Colorado Software Summit, but it was nice to be able to use an article to provide a little different focus on the compelling combination that Java EE and Flex make.

In the article, I introduce Flex features, concepts, and syntax while building a Flex application that gets its data from a Java EE server. The article articulates several general advantages of Flex in addition to advantages of Flex that are specific to Java developers.

The very simple Flex-based example built up in this article provides opportunities to look at several Flex features including some ActionScript 3.0 syntax, custom component creation, styling with CSS, and use of several built-in Flex components.

The article focuses on moving the example from a completely static application with highly redundant code to a leaner application supporting dynamic data population from a Java-based server and dynamic user interaction. The mechanism used in this article for communicating with the Java back-end is Flex's built-in HTTPService. I intentionally used the non-proxied HTTPService provided with the Flex SDK rather than BlazeDS's proxied HTTPService in this first part. In the second part, I will be introducing BlazeDS to the sample application.

I did not cover GraniteDS in this first part and will not be covering it in any detail in the second part. However, for a nice introduction to Flex+Java with GraniteDS, see Franck Wolff's Migrating Java EE Web Applications to Adobe Flex and Granite Data Services.

Thứ Năm, 27 tháng 3, 2008

Five Things to Like About Spring ... Even if You Don't Use It Directly

A while back (May 2005), Bruce Tate's article Five Things I Love About Spring was published. As one would expect, this article focused on things that the Spring Framework does for developers using Spring. There are numerous other articles on the subject and perhaps no better source on the architectural justification for and advantages of using Spring exists than the "book that started it all" (J2EE Design and Development).

In this blog entry, I briefly look at five things that the Spring framework has done or can do for Java developers who don't directly use the Spring Framework in their applications.

1. Spring prompted J2EE to become much more usable as Java EE 5.

While many Java EE developers use Spring in one way or another, some still develop with Java EE without Spring. These users still benefit from Spring because Spring's presence has helped motivate change to Java EE, particularly in terms of increased usability.

2. Spring inspired many of the Java EE 5 advancements such as dependency injection and POJO-based development.

Not only did Spring motivate improvements to Java EE, but Spring also provided examples of techniques and approaches that enterprise developers like that Java EE could emulate. Some of these approaches were even around before Spring, but Spring popularized them as the framework itself rapidly rose in popularity.

3. Spring with Dependencies makes many useful libraries and frameworks available in a single download.

More than once, I have benefited from having a necessary open source product or library readily available from the Spring dependencies directory. This may seem like a minor thing, but it saves me a little time and potentially a lot of aggravation from having to find the appropriate web site to download the appropriate files and then unload the files. This effort is not a big deal in most cases, but there are times when I just want to try something out that I have read very quickly and the time saved from having to download it is greater than the time I spend trying it out. Spring's bundling of these dependencies can be a little reassuring regarding compatibilities and versions of the dependencies.

4. Spring developers help identify useful open source libraries and frameworks.

Related to the point above, I have learned about interesting of promising libraries and open source products simply by browsing the list of Spring dependencies. The Spring developers seem to pick the best of the compatible open source software and so, in many respects, have done research into these products that I can now take advantage of.

5. Spring provides developers with examples of design and Java coding best practices.

The Spring Framework probably shows more flexibility and extensibility than many of us need because of its nature as a common framework. That being said, we all still want a certain degree of flexibility and extensibility in our applications and Spring provides many good examples of how to achieve this. Note that I am not talking about Spring best practices here, but am rather talking about Java best practices that the Spring framework enables, encourages, and/or provides examples for. Because Spring is open source, we can view and get ideas from its code base.