Java Reference
In-Depth Information
@HeaderParam ( "User-Agent" ) String userAgent ,
@CookieParam ( "last-visit" ) String date )
{
The getCustomer() method retrieves a Customer object from the in-memory map referen-
ced by the id path parameter. The @HeaderParam annotation injects the value of the User-
Agent header. This is a standard HTTP 1.1 header that denotes the type of client that made
the request (Safari, Firefox, Internet Explorer, etc.). The @CookieParam annotation injects
the value of the last-visit cookie that the client should be passing along with each re-
quest:
final
final Customer customer = customerDB . get ( id );
iif ( customer == null
null ) {
throw
throw new
new WebApplicationException ( Response . Status . NOT_FOUND );
}
String output = "User-Agent: " + userAgent + "\r\n" ;
output += "Last visit: " + date + "\r\n\r\n" ;
output += "Customer: " + customer . getFirstName () + " "
+ customer . getLastName ();
String lastVisit = DateFormat . getDateTimeInstance (
DateFormat . SHORT , DateFormat . LONG ). format ( new
new Date ());
return
return Response . ok ( output )
. cookie ( new
new NewCookie ( "last-visit" , lastVisit ))
. build ();
}
The implementation of this method is very simple. It outputs the User-Agent header and
last-visit cookie as plain text ( text/plain ). It also resets the last-visit cookie to the
current time and date.
Build and Run the Example Program
Perform the following steps:
1. Open a command prompt or shell terminal and change to the ex05_2 directory of the
workbook example code.
2. Make sure your PATH is set up to include both the JDK and Maven, as described in
Chapter 17 .
3. Perform the build and run the example by typing maven jetty:run . This command
is a bit different than our previous examples. This script builds the WAR file, but it
also starts up the Jetty servlet container.
You test-drive ex05_2 by using your browser. The first step is to go to http://localhost:8080 ,
as shown in Figure 20-1 .
Search WWH ::




Custom Search