<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-18313336</id><updated>2012-01-01T19:00:13.980-05:00</updated><category term='example/sample code'/><category term='xml'/><category term='hibernate'/><category term='charts'/><category term='javascript'/><category term='news'/><category term='authentication'/><category term='Java EE 5'/><category term='REST'/><category term='ajax'/><category term='patterns'/><category term='security'/><category term='messaging'/><category term='aop'/><category term='how-to'/><category term='web services'/><category term='concurrency'/><category term='flex'/><category term='pdf'/><category term='oracle'/><category term='batch'/><category term='struts'/><category term='build'/><category term='servlet3'/><category term='websphere'/><category term='tips'/><category term='spring'/><category term='persistence'/><category term='code quality'/><category term='Java EE 6'/><category term='weblogic'/><category term='JAX-WS'/><category term='testing'/><category term='caching'/><category term='Guice'/><category term='ide'/><title type='text'>Abhi On Java</title><subtitle type='html'>On Java, Websphere and related technologies</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://java-x.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/18313336/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://java-x.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><link rel='next' type='application/atom+xml' href='http://www.blogger.com/feeds/18313336/posts/default?start-index=101&amp;max-results=100'/><author><name>Abhilash Vuyyuru</name><uri>https://profiles.google.com/107328798079149921932</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh6.googleusercontent.com/-NxAgT10-nrk/AAAAAAAAAAI/AAAAAAAAB0o/Gz3VMjlEXb0/s512-c/photo.jpg'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>195</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-18313336.post-7784626437533411700</id><published>2011-03-28T15:03:00.013-04:00</published><updated>2011-03-28T16:01:53.015-04:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='REST'/><category scheme='http://www.blogger.com/atom/ns#' term='web services'/><category scheme='http://www.blogger.com/atom/ns#' term='example/sample code'/><title type='text'>Resful Web Services With Jersey API</title><content type='html'>This post describes how to use Jersey API to create RESTful Web Services. To keep it simple, I'm not going describe how REST works or should be implemented. Rather, it is simple example on how to use the API to implement some of the more common features of a REST service. The post describes how to &lt;ul&gt;&lt;li&gt;Create a RESTful Web Service using Jersey&lt;/li&gt;&lt;li&gt;Create GET, POST, PUT and DELETE handlers in the service&lt;/li&gt;&lt;li&gt;Create a Jersey Client to invoke the various methods on the Service&lt;/li&gt;&lt;/ul&gt;The example here has been tested on Tomcat7, with Java SE 6. The following steps explain how to use the API &lt;br /&gt;&lt;a href="/2011/03/resful-web-services-with-jersey-api.html"&gt;More ...............&lt;/a&gt;&lt;span class="fullpost"&gt;&lt;ol&gt;&lt;li&gt;Download Jersey from &lt;a href="http://jersey.java.net/"&gt;http://jersey.java.net/&lt;/a&gt;&lt;/li&gt;&lt;li&gt;Create a Dynamic Web Project(JerseyRest) in Eclipse&lt;/li&gt;&lt;li&gt;Add the Jersey Jar files to the classpath. The following files were used:http://www.blogger.com/img/blank.gif&lt;br /&gt;Server:&lt;ul&gt;&lt;li&gt;asm-3.1.jar&lt;/li&gt;&lt;li&gt;jersey-core-1.4.jar&lt;/li&gt;&lt;li&gt;jersey-server-1.4.jar&lt;/li&gt;&lt;/ul&gt;Client:&lt;ul&gt;&lt;li&gt;jersey-client-1.4.jar&lt;/li&gt;&lt;li&gt;jersey-core-1.4.jar&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;li&gt;Add the Jersey Servlet to Web.xml&lt;pre&gt;&amp;lt;?xml version="1.0" encoding="UTF-8"?&amp;gt;&lt;br /&gt;&amp;lt;web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"&amp;gt;&lt;br /&gt;  &amp;lt;display-name&amp;gt;JerseyRest&amp;lt;/display-name&amp;gt;&lt;br /&gt;  &amp;lt;welcome-file-list&amp;gt;&lt;br /&gt;    &amp;lt;welcome-file&amp;gt;index.html&amp;lt;/welcome-file&amp;gt;&lt;br /&gt;    &amp;lt;welcome-file&amp;gt;index.htm&amp;lt;/welcome-file&amp;gt;&lt;br /&gt;    &amp;lt;welcome-file&amp;gt;index.jsp&amp;lt;/welcome-file&amp;gt;&lt;br /&gt;    &amp;lt;welcome-file&amp;gt;default.html&amp;lt;/welcome-file&amp;gt;&lt;br /&gt;    &amp;lt;welcome-file&amp;gt;default.htm&amp;lt;/welcome-file&amp;gt;&lt;br /&gt;    &amp;lt;welcome-file&amp;gt;default.jsp&amp;lt;/welcome-file&amp;gt;&lt;br /&gt;  &amp;lt;/welcome-file-list&amp;gt;&lt;br /&gt;  &lt;br /&gt;   &amp;lt;servlet&amp;gt;&lt;br /&gt;        &amp;lt;servlet-name&amp;gt;Jersey Web Application&amp;lt;/servlet-name&amp;gt;&lt;br /&gt;        &amp;lt;servlet-class&amp;gt;&lt;span style="font-weight:bold;"&gt;com.sun.jersey.spi.container.servlet.ServletContainer&lt;/span&gt;&amp;lt;/servlet-class&amp;gt;&lt;br /&gt;        &lt;span style="font-weight:bold;"&gt;&amp;lt;init-param&amp;gt;&lt;br /&gt;            &amp;lt;param-name&amp;gt;com.sun.jersey.config.property.packages&amp;lt;/param-name&amp;gt;&lt;br /&gt;            &amp;lt;param-value&amp;gt;com.blogspot.aoj.restws&amp;lt;/param-value&amp;gt;&lt;br /&gt;        &amp;lt;/init-param&amp;gt;&lt;/span&gt;&lt;br /&gt;        &amp;lt;load-on-startup&amp;gt;1&amp;lt;/load-on-startup&amp;gt;&lt;br /&gt;    &amp;lt;/servlet&amp;gt;&lt;br /&gt;    &amp;lt;servlet-mapping&amp;gt;&lt;br /&gt;        &amp;lt;servlet-name&amp;gt;Jersey Web Application&amp;lt;/servlet-name&amp;gt;&lt;br /&gt;        &amp;lt;url-pattern&amp;gt;/*&amp;lt;/url-pattern&amp;gt;&lt;br /&gt;    &amp;lt;/servlet-mapping&amp;gt;&lt;br /&gt;&amp;lt;/web-app&amp;gt;&lt;/pre&gt;Note:&lt;uL&gt;&lt;li&gt;The Jersey Servlet maps to all requests under /rest&lt;/li&gt;&lt;li&gt;The package where your service class resides is provided in the package-name init param.&lt;/li&gt;&lt;/ul&gt;Create the Service Class&lt;pre&gt;/**&lt;br /&gt; * @author Abhi Vuyyuru&lt;br /&gt; */&lt;br /&gt;&lt;br /&gt;package com.blogspot.aoj.restws;&lt;br /&gt;&lt;br /&gt;import javax.ws.rs.Consumes;&lt;br /&gt;import javax.ws.rs.DELETE;&lt;br /&gt;import javax.ws.rs.FormParam;&lt;br /&gt;import javax.ws.rs.GET;&lt;br /&gt;import javax.ws.rs.POST;&lt;br /&gt;import javax.ws.rs.PUT;&lt;br /&gt;import javax.ws.rs.Path;&lt;br /&gt;import javax.ws.rs.PathParam;&lt;br /&gt;import javax.ws.rs.Produces;&lt;br /&gt;import javax.ws.rs.core.MediaType;&lt;br /&gt;&lt;br /&gt;@Path("/restws")&lt;br /&gt;public class RestWS {&lt;br /&gt; @GET&lt;br /&gt; @Produces("text/plain")&lt;br /&gt; @Path("hello")&lt;br /&gt; public String sayHello() {&lt;br /&gt;  return "Hello World";&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; @GET&lt;br /&gt; @Path("concat/i/{i}/j/{j}")&lt;br /&gt; @Produces("text/plain")&lt;br /&gt; public String concat(@PathParam("i") String i, @PathParam("j") String j) {&lt;br /&gt;  System.out.println("GET Input : " + i + j);&lt;br /&gt;  return (i + j);&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; @POST&lt;br /&gt; @Path("posts")&lt;br /&gt; @Produces(MediaType.TEXT_HTML)&lt;br /&gt; @Consumes(MediaType.APPLICATION_FORM_URLENCODED)&lt;br /&gt; public String consumeReq(@FormParam("i") String i, @FormParam("j") String j) {&lt;br /&gt;  System.out.println("POST Input : " + i + j);&lt;br /&gt;  return i + j;&lt;br /&gt; }&lt;br /&gt; &lt;br /&gt; @PUT&lt;br /&gt; @Path("puts")&lt;br /&gt; @Produces(MediaType.TEXT_HTML)&lt;br /&gt; @Consumes(MediaType.APPLICATION_FORM_URLENCODED)&lt;br /&gt; public void putReq(@FormParam("i") String i, @FormParam("j") String j) {&lt;br /&gt;  System.out.println("PUT Input : " + i + j);&lt;br /&gt;  return;&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; @DELETE&lt;br /&gt; @Path("dels/i/{i}/j/{j}")&lt;br /&gt; public void deleteReq(@PathParam("i") String i, @PathParam("j") String j) {&lt;br /&gt;  System.out.println("DELETE Input : " + i + j);&lt;br /&gt;  return;&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt;}&lt;/pre&gt;&lt;/li&gt;&lt;li&gt;Create the Client&lt;pre&gt;/**&lt;br /&gt; * @author Abhi Vuyyuru&lt;br /&gt; */&lt;br /&gt;package com.blogspot.aoj.clients;&lt;br /&gt;&lt;br /&gt;import java.io.IOException;&lt;br /&gt;&lt;br /&gt;import javax.ws.rs.core.MultivaluedMap;&lt;br /&gt;&lt;br /&gt;import com.sun.jersey.api.client.Client;&lt;br /&gt;import com.sun.jersey.api.client.WebResource;&lt;br /&gt;import com.sun.jersey.core.util.MultivaluedMapImpl;&lt;br /&gt;&lt;br /&gt;public class RestWSClient {&lt;br /&gt;&lt;br /&gt; public static void main(String[] args) throws IOException {&lt;br /&gt;  Client client = Client.create();&lt;br /&gt;  WebResource wsClient = client.resource("http://localhost:8080/JerseyRest/restws/");&lt;br /&gt;&lt;br /&gt;  String helloResp = wsClient.path("hello").get(String.class);&lt;br /&gt;  System.out.println(helloResp);&lt;br /&gt;&lt;br /&gt;  MultivaluedMap queryParams = new MultivaluedMapImpl();&lt;br /&gt;  queryParams.add("i", "val1");&lt;br /&gt;  queryParams.add("j", "val2");&lt;br /&gt;  String postResp = wsClient.path("posts").post(String.class, queryParams);&lt;br /&gt;  System.out.println(postResp);&lt;br /&gt;&lt;br /&gt;  String getResp = wsClient.path("concat").path("i/" + "val1").path("j/" + "val2").get(String.class);&lt;br /&gt;  System.out.println(getResp);&lt;br /&gt;&lt;br /&gt;  wsClient.path("puts").put(queryParams);&lt;br /&gt;  &lt;br /&gt;  wsClient.path("dels").path("i/" + "val1").path("j/" + "val2").delete();&lt;br /&gt;&lt;br /&gt; }&lt;br /&gt;}&lt;/pre&gt;&lt;/li&gt;&lt;/ol&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/18313336-7784626437533411700?l=java-x.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://java-x.blogspot.com/feeds/7784626437533411700/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://java-x.blogspot.com/2011/03/resful-web-services-with-jersey-api.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/18313336/posts/default/7784626437533411700'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/18313336/posts/default/7784626437533411700'/><link rel='alternate' type='text/html' href='http://java-x.blogspot.com/2011/03/resful-web-services-with-jersey-api.html' title='Resful Web Services With Jersey API'/><author><name>Abhilash Vuyyuru</name><uri>https://profiles.google.com/107328798079149921932</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh6.googleusercontent.com/-NxAgT10-nrk/AAAAAAAAAAI/AAAAAAAAB0o/Gz3VMjlEXb0/s512-c/photo.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-18313336.post-1928238070714707737</id><published>2010-08-31T10:09:00.005-04:00</published><updated>2010-08-31T10:18:40.511-04:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='servlet3'/><category scheme='http://www.blogger.com/atom/ns#' term='Java EE 6'/><title type='text'>File Upload with Servlet 3.0</title><content type='html'>This code works on Glassfish 3.&lt;br /&gt;This is a simple example of how to upload files using JSP and &lt;span style="font-weight: bold;"&gt;Servlet 3.0&lt;/span&gt; which is a part of Java EE 6. While in earlier versions of Servlets we had to use commons fileupload or other libraries, this feature has been integrated into the Servlet 3.0 specification. Here is the code for a simple servlet and a JSP file that makes a file upload request to the servlet.&lt;br /&gt;&lt;br /&gt;&lt;a href="/2010/08/file-upload-with-servlet-30.html"&gt; Full post &lt;/a&gt;&lt;br /&gt;&lt;span class="fullpost"&gt;&lt;ol&gt;&lt;li&gt;&lt;span style="font-weight: bold;"&gt;FileUploadServlet.java&lt;/span&gt;&lt;pre&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;package &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;com.blogspot.aoj.servlet3;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;import &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;java.io.FileOutputStream;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;import &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;java.io.IOException;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;import &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;java.io.InputStream;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;import &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;javax.servlet.ServletException;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold; color: rgb(127, 0, 85);"&gt;import &lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;javax.servlet.annotation.MultipartConfig;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold; color: rgb(127, 0, 85);"&gt;import &lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;javax.servlet.annotation.WebServlet;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;import &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;javax.servlet.http.HttpServlet;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;import &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;javax.servlet.http.HttpServletRequest;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;import &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;javax.servlet.http.HttpServletResponse;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;import &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;javax.servlet.http.Part;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;import &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;org.apache.log4j.Logger;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold; color: rgb(100, 100, 100);"&gt;@WebServlet&lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;urlPatterns = &lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(42, 0, 255);"&gt;"/fileUpload"&lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold; color: rgb(100, 100, 100);"&gt;@MultipartConfig&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;public class &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;FileUploadServlet &lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;extends &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;HttpServlet &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;  &lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;private static &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Logger logger = Logger.getLogger&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;FileUploadServlet.&lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;class&lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;  &lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;public &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;FileUploadServlet&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;() {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;    &lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;super&lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;()&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;  &lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;protected &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;void &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;doGet&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;HttpServletRequest request,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;      &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;HttpServletResponse response&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;) &lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;throws &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;ServletException, IOException &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;   &lt;span style="font-weight: bold;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(127, 0, 85);"&gt;for &lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;Part part : request.getParts&lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;()) {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;      &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;logger.info&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;part.getName&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;())&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;      &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;InputStream is = request.getPart&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;part.getName&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;())&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;.getInputStream&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;()&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;      &lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;int &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;i = is.available&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;()&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;      &lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;byte&lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;[] &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;b  = &lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;new &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;byte&lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;[&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;i&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;]&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;      &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;is.read&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;b&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;      &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;logger.info&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(42, 0, 255);"&gt;"Length : " &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;+ b.length&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;      &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;String fileName = getFileName&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;part&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;      &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;logger.info&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(42, 0, 255);"&gt;"File name : " &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;+ fileName&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;      &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;FileOutputStream os = &lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;new &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;FileOutputStream&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(42, 0, 255);"&gt;"c:/temp/logs/" &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;+ fileName&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;      &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;os.write&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;b&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;      &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;is.close&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;()&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;  &lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;private &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;String getFileName&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Part part&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;) {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;String partHeader = part.getHeader&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(42, 0, 255);"&gt;"content-disposition"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;logger.info&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(42, 0, 255);"&gt;"Part Header = " &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;+ partHeader&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;    &lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;for &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;String cd : part.getHeader&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(42, 0, 255);"&gt;"content-disposition"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;.split&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(42, 0, 255);"&gt;";"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)) {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;      &lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;if &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;cd.trim&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;()&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;.startsWith&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(42, 0, 255);"&gt;"filename"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)) {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;        &lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;return &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;cd.substring&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;cd.indexOf&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(153, 0, 0);"&gt;'='&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;) &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;+ &lt;/span&gt;&lt;span style="color: rgb(153, 0, 0);"&gt;1&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;.trim&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;()&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;            &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;.replace&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(42, 0, 255);"&gt;"\""&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(42, 0, 255);"&gt;""&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;      &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;    &lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;return null&lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;  &lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;protected &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;void &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;doPost&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;HttpServletRequest request,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;      &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;HttpServletResponse response&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;) &lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;throws &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;ServletException, IOException &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;doGet&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;request, response&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;}&lt;/span&gt;&lt;/pre&gt;Note:&lt;ul&gt;&lt;li&gt;There is no need to use a deployment descriptor the &lt;span style="font-weight: bold;"&gt;@WebServlet&lt;/span&gt; annotation is enough, which uses the urlPatterns property to define servlet mappings&lt;/li&gt;&lt;li&gt;The&lt;span style="font-weight: bold;"&gt; @MultipartConfig&lt;/span&gt; is used to indicate that the servlet expects requests wof &lt;span style="font-weight: bold;"&gt;multipart/form-data&lt;/span&gt; MIME type which is required for file upload. Using &lt;span style="font-weight: bold;"&gt;@MultipartConfig&lt;/span&gt; allows you to use the request.getParts get the parts of the request&lt;/li&gt;&lt;li&gt;The getFileName method gets the file name from the content-disposition header.&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-weight: bold;"&gt;upload.jsp&lt;/span&gt;&lt;pre&gt;&amp;lt;html&amp;gt;&lt;br /&gt;&amp;lt;head&amp;gt;&lt;br /&gt;&amp;lt;title&amp;gt;File Upload with Servlet 3.0&amp;lt;/title&amp;gt;&lt;br /&gt;&amp;lt;/head&amp;gt;&lt;br /&gt;&amp;lt;body&amp;gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;&amp;lt;form action="fileUpload" enctype="multipart/form-data" method="post"&amp;gt;&lt;/span&gt;&lt;br /&gt;&amp;lt;input type="file" name="uploadFile" /&amp;gt; &amp;lt;input type="submit" /&amp;gt;&amp;lt;/form&amp;gt;&lt;br /&gt;&amp;lt;/body&amp;gt;&lt;br /&gt;&amp;lt;/html&amp;gt;&lt;/pre&gt;&lt;/li&gt;&lt;/ol&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/18313336-1928238070714707737?l=java-x.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://java-x.blogspot.com/feeds/1928238070714707737/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://java-x.blogspot.com/2010/08/file-upload-with-servlet-30.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/18313336/posts/default/1928238070714707737'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/18313336/posts/default/1928238070714707737'/><link rel='alternate' type='text/html' href='http://java-x.blogspot.com/2010/08/file-upload-with-servlet-30.html' title='File Upload with Servlet 3.0'/><author><name>Abhilash Vuyyuru</name><uri>https://profiles.google.com/107328798079149921932</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh6.googleusercontent.com/-NxAgT10-nrk/AAAAAAAAAAI/AAAAAAAAB0o/Gz3VMjlEXb0/s512-c/photo.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-18313336.post-8089916138218384018</id><published>2010-07-27T16:36:00.004-04:00</published><updated>2010-07-27T16:47:02.071-04:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='REST'/><category scheme='http://www.blogger.com/atom/ns#' term='JAX-WS'/><category scheme='http://www.blogger.com/atom/ns#' term='web services'/><category scheme='http://www.blogger.com/atom/ns#' term='example/sample code'/><title type='text'>Implementing REST WebService using JAX-WS</title><content type='html'>This example was implemented on Weblogic 10.3 server.&lt;br /&gt;The following example demonstrates how to implement a simple REST WebService using JAX-WS. This example shows the following two ways to pass parameters to a REST Webservice as part of a HTTP GET Request&lt;br /&gt;&lt;ul&gt;&lt;li&gt;&lt;span style="font-weight: bold;"&gt;Query String&lt;/span&gt;&lt;pre&gt;http://localhost:7001/RestfulWS/RestfulWS?i=1&amp;amp;j=5&lt;/pre&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-weight: bold;"&gt;Path Info&lt;/span&gt;&lt;pre&gt;http://localhost:7001/RestfulWS/RestfulWS/i/1/j/5&lt;/pre&gt;&lt;/li&gt;&lt;/ul&gt; In order to implement you have to&lt;br /&gt;&lt;a href="/2010/07/implementing-rest-webservice-using-jax.html"&gt;&lt;span style="font-weight:bold;"&gt;More&lt;/span&gt;&lt;/a&gt;&lt;br /&gt;&lt;span class="fullpost"&gt;&lt;ol&gt;&lt;li&gt;&lt;span style="font-weight: bold;"&gt;Implement the Provider&lt;/span&gt;&lt;t&gt;&lt;span style="font-weight: bold;"&gt; Interface&lt;/span&gt;&lt;pre&gt;&lt;span style="color: rgb(100, 100, 100);"&gt;@WebServiceProvider&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;targetNamespace = &lt;/span&gt;&lt;span style="color: rgb(42, 0, 255);"&gt;"http://java-x.blogspot.com/wsSamples/restfulWebService1"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, serviceName = &lt;/span&gt;&lt;span style="color: rgb(42, 0, 255);"&gt;"RestfulWS"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(100, 100, 100);"&gt;@BindingType&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;value = HTTPBinding.HTTP_BINDING&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;public class &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;RestfulWS &lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;implements &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Provider&amp;lt;Source&amp;gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;{&lt;/span&gt;&lt;/pre&gt; The provider interface has only one method &lt;span style="font-style: italic;"&gt;&lt;t&gt;invoke().&lt;/t&gt;&lt;/span&gt; Setting the BindingType annotation to &lt;span style="font-weight: bold;"&gt;HTTP_BINDING&lt;/span&gt; ensures that HTTP GET requests are sent to this service. The most common way to implementing Provider&lt;t&gt; interface is using Provider&lt;javax.xml.transform.source&gt; which is used to pass XML data back to the client&lt;/javax.xml.transform.source&gt;&lt;/t&gt;&lt;/t&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;span style="font-weight: bold;"&gt;Implementing the invoke Method&lt;/span&gt;: The invoke method handles the all the logic for the Service. In the following snippet, you can see how to get the request type and the handling logic based on whether the request parameters are sent in a QueryString or as part of the request path.&lt;pre&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;   &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;String requestMethod = &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;String&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;) &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;messageContext.get&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;MessageContext.HTTP_REQUEST_METHOD&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;   &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;String query = &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;String&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;) &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;messageContext.get&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;MessageContext.QUERY_STRING&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;   &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;String path = &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;String&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;) &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;messageContext.get&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;MessageContext.PATH_INFO&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li&gt;Implementing the process request: In this method, you can see that the request parameter string is parsed by using separators "&amp;amp;", "=" and "/". This enables handling of both &lt;span style="font-weight: bold;"&gt;QueryString ("&amp;amp;" and "=") and Path Info ("/")&lt;/span&gt; requests.&lt;pre&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;StringTokenizer st = &lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;new &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;StringTokenizer&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;queryString, &lt;/span&gt;&lt;span style="color: rgb(42, 0, 255);"&gt;"=&amp;amp;/"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li&gt;Implementing createSource Method: This method creates an XML string that will be sent as the response as a StreamSource.&lt;pre&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;return new &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;StreamSource&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;new &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;ByteArrayInputStream&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;result.getBytes&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;()))&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li&gt;Create a web.xml descriptor: The descriptor file should contain a servlet-mapping for the servlet "[&lt;span style="font-style: italic;"&gt;ServiceName&lt;/span&gt;]Servlethttp" to accept all requests to [&lt;span style="font-style: italic;"&gt;ServiceName&lt;/span&gt;]/* to allow path info requests. Make sure that you do add the servlet declaration as this is done by JWSC ant task and may create a duplicate.&lt;pre&gt;&amp;lt;servlet-mapping&amp;gt;&lt;br /&gt;&amp;lt;servlet-name&amp;gt;RestfulWSServlethttp&amp;lt;/servlet-name&amp;gt;&lt;br /&gt;&amp;lt;url-pattern&amp;gt;/RestfulWS/*&amp;lt;/url-pattern&amp;gt;&lt;br /&gt;&amp;lt;/servlet-mapping&amp;gt;&lt;/pre&gt;&lt;br /&gt;The reason for creating a servlet-maping is because the JWSC ant task creates the servlet and a servlet mapping in the web.xml, however, the mapping generated by JWSC maps to [&lt;span style="font-style: italic;"&gt;ServiceName&lt;/span&gt;]/ and not [&lt;span style="font-style: italic;"&gt;ServiceName&lt;/span&gt;]/*, which means that path info requests will not be processed.&lt;/li&gt;&lt;li&gt;&lt;span style="font-weight: bold;"&gt;Creating the Web Service using ANT&lt;/span&gt;: The following ant task creates the webservice as an exploded WAR.&lt;pre&gt;&amp;lt;target name="build.service" description="Target that builds the Web Service"&amp;gt;&lt;br /&gt;&amp;lt;echo message="${java.class.path}" /&amp;gt;&lt;br /&gt;&amp;lt;mkdir dir="${ear.dir}" /&amp;gt;&lt;br /&gt;&amp;lt;jwsc srcdir="./src/com/blogspot/aoj/restws/" destdir="${ear.dir}" fork="true" keepGenerated="true" deprecation="${deprecation}" debug="${debug}" verbose="false"&amp;gt;&lt;br /&gt; &amp;lt;jws file="${ws.file}.java" explode="true" type="JAXWS" &amp;gt;&lt;br /&gt;  &lt;span style="font-weight: bold;"&gt;&amp;lt;descriptor file="./resources/web.xml" /&amp;gt;&lt;/span&gt;&lt;br /&gt; &amp;lt;/jws&amp;gt;&lt;br /&gt;&amp;lt;/jwsc&amp;gt;&lt;br /&gt;&amp;lt;/target&amp;gt;&lt;/pre&gt;The task has to contain the location of web.xml file created earlier. This file will be modified by JWSC&lt;/li&gt;&lt;/ol&gt;The complete sources&lt;ul&gt;&lt;li&gt;&lt;span style="font-weight: bold;"&gt;RestfulWS.java:&lt;/span&gt;&lt;pre&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;package &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;com.blogspot.aoj.restws;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(63, 95, 191);"&gt;/**&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt; &lt;/span&gt;&lt;span style="color: rgb(63, 95, 191);"&gt;* &lt;/span&gt;&lt;span style="color: rgb(127, 159, 191);"&gt;@author &lt;/span&gt;&lt;span style="color: rgb(63, 95, 191);"&gt;Abhi Vuyyuru&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt; &lt;/span&gt;&lt;span style="color: rgb(63, 95, 191);"&gt;*/&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;import &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;java.io.ByteArrayInputStream;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;import &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;java.util.StringTokenizer;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;import &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;javax.annotation.Resource;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;import &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;javax.xml.transform.Source;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;import &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;javax.xml.transform.stream.StreamSource;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;import &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;javax.xml.ws.BindingType;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;import &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;javax.xml.ws.Provider;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;import &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;javax.xml.ws.WebServiceContext;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;import &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;javax.xml.ws.WebServiceProvider;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;import &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;javax.xml.ws.handler.MessageContext;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;import &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;javax.xml.ws.http.HTTPBinding;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(100, 100, 100);"&gt;@WebServiceProvider&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;targetNamespace = &lt;/span&gt;&lt;span style="color: rgb(42, 0, 255);"&gt;"http://java-x.blogspot.com/wsSamples/restfulWebService1"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, serviceName = &lt;/span&gt;&lt;span style="color: rgb(42, 0, 255);"&gt;"RestfulWS"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(100, 100, 100);"&gt;@BindingType&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;value = HTTPBinding.HTTP_BINDING&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;public class &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;RestfulWS &lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;implements &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Provider&amp;lt;Source&amp;gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt; &lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;public &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Source invoke&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Source source&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;) {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;  &lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;try &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;   &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;MessageContext messageContext = wsContext.getMessageContext&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;()&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;   &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;String requestMethod = &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;String&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;) &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;messageContext.get&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;MessageContext.HTTP_REQUEST_METHOD&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;   &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;String query = &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;String&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;) &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;messageContext.get&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;MessageContext.QUERY_STRING&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;   &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;String path = &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;String&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;) &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;messageContext.get&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;MessageContext.PATH_INFO&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;   &lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;if &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;requestMethod.equals&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(42, 0, 255);"&gt;"GET"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)) {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;    &lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;if &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;query != &lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;null &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;amp;&amp;amp; query.contains&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(42, 0, 255);"&gt;"i="&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;) &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;amp;&amp;amp; query.contains&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(42, 0, 255);"&gt;"j="&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)) {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;     &lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;return &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;processRequest&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;query&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;} &lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;else &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;     &lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;return &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;processRequest&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;path&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;   &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;} &lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;else &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;    &lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;throw new &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Exception&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(42, 0, 255);"&gt;"Unsupported operation"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;   &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;} &lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;catch &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Exception e&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;) {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;   &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;e.printStackTrace&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;()&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;   &lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;return &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;createSource&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(153, 0, 0);"&gt;1&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, e.getMessage&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;())&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt; &lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;private &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Source processRequest&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;String queryString&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;) {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;StringTokenizer st = &lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;new &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;StringTokenizer&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;queryString, &lt;/span&gt;&lt;span style="color: rgb(42, 0, 255);"&gt;"=&amp;amp;/"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;st.nextToken&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;()&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;  &lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;int &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;i = Integer.parseInt&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;st.nextToken&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;())&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;st.nextToken&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;()&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;  &lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;int &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;j = Integer.parseInt&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;st.nextToken&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;())&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;  &lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;return &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;createSource&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;i + j, &lt;/span&gt;&lt;span style="color: rgb(42, 0, 255);"&gt;"Sum"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt; &lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;private &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Source createSource&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;int &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;status, String errorMessage&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;) {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;String result = &lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;null&lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;result = &lt;/span&gt;&lt;span style="color: rgb(42, 0, 255);"&gt;"&amp;lt;?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\" ?&amp;gt;&amp;lt;Response&amp;gt;&amp;lt;StatusCode&amp;gt;" &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;+ status&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;      &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;+ &lt;/span&gt;&lt;span style="color: rgb(42, 0, 255);"&gt;"&amp;lt;/StatusCode&amp;gt;&amp;lt;StatusMessage&amp;gt;" &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;+ errorMessage + &lt;/span&gt;&lt;span style="color: rgb(42, 0, 255);"&gt;"&amp;lt;/StatusMessage&amp;gt;&amp;lt;/Response&amp;gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;  &lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;return new &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;StreamSource&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;new &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;ByteArrayInputStream&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;result.getBytes&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;()))&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt; &lt;/span&gt;&lt;span style="color: rgb(100, 100, 100);"&gt;@Resource&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;type = Object.&lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;class&lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt; &lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;protected &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;WebServiceContext wsContext;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-weight: bold;"&gt;resources/web.xml&lt;/span&gt;&lt;pre&gt;&lt;br /&gt;&amp;lt;?xml version='1.0' encoding='UTF-8'?&amp;gt;&lt;br /&gt;&amp;lt;web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5"&amp;gt;&lt;br /&gt;&amp;lt;display-name&amp;gt;RestfulWSWebApp&amp;lt;/display-name&amp;gt;&lt;br /&gt;&amp;lt;servlet-mapping&amp;gt;&lt;br /&gt;&amp;lt;servlet-name&amp;gt;RestfulWSServlethttp&amp;lt;/servlet-name&amp;gt;&lt;br /&gt;&amp;lt;url-pattern&amp;gt;/RestfulWS/*&amp;lt;/url-pattern&amp;gt;&lt;br /&gt;&amp;lt;/servlet-mapping&amp;gt;&lt;br /&gt;&amp;lt;/web-app&amp;gt;&lt;/pre&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-weight: bold;"&gt;build.xml&lt;/span&gt;&lt;pre&gt;&amp;lt;project name="wsSamples" default="all" basedir="."&amp;gt;&lt;br /&gt;&amp;lt;!-- set global properties for this build --&amp;gt;&lt;br /&gt;&amp;lt;property file="./build.properties" /&amp;gt;&lt;br /&gt;&amp;lt;property name="ws.file" value="RestfulWS" /&amp;gt;&lt;br /&gt;&amp;lt;property name="ear.dir" value="${build.dir}/wsSamples" /&amp;gt;&lt;br /&gt;&amp;lt;taskdef name="jwsc" classname="weblogic.wsee.tools.anttasks.JwscTask" /&amp;gt;&lt;br /&gt;&amp;lt;!--Target that builds sample Web Service and client--&amp;gt;&lt;br /&gt;&amp;lt;target name="all" description="Target that builds sample Web Service and client" depends="clean,build.service,deploy" /&amp;gt;&lt;br /&gt;&amp;lt;!--Target that builds sample Web Service and client--&amp;gt;&lt;br /&gt;&amp;lt;target name="build" description="Target that builds sample Web Service and client" depends="build.service" /&amp;gt;&lt;br /&gt;&amp;lt;target name="clean"&amp;gt;&lt;br /&gt; &amp;lt;delete dir="${ear.dir}" /&amp;gt;&lt;br /&gt; &amp;lt;delete dir="${client.dir}" /&amp;gt;&lt;br /&gt;&amp;lt;/target&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;!--  Target that builds the Web Service--&amp;gt;&lt;br /&gt;&amp;lt;target name="build.service" description="Target that builds the Web Service"&amp;gt;&lt;br /&gt; &amp;lt;echo message="${java.class.path}" /&amp;gt;&lt;br /&gt; &amp;lt;mkdir dir="${ear.dir}" /&amp;gt;&lt;br /&gt;&lt;br /&gt; &amp;lt;jwsc srcdir="./src/com/blogspot/aoj/restws/" destdir="${ear.dir}" fork="true" keepGenerated="true" deprecation="${deprecation}" debug="${debug}" verbose="false"&amp;gt;&lt;br /&gt;  &amp;lt;jws file="${ws.file}.java" explode="true" type="JAXWS" &amp;gt;&lt;br /&gt;   &amp;lt;descriptor file="./resources/web.xml" /&amp;gt;&lt;br /&gt;  &amp;lt;/jws&amp;gt;&lt;br /&gt; &amp;lt;/jwsc&amp;gt;&lt;br /&gt;&amp;lt;/target&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;!--  Target that deploys the Web Service--&amp;gt;&lt;br /&gt;&amp;lt;target name="deploy" description="Target that deploys the Web Service"&amp;gt;&lt;br /&gt; &amp;lt;wldeploy action="deploy" source="${ear.dir}" user="${wls.username}" password="${wls.password}" verbose="true" failonerror="${failondeploy}" adminurl="t3://${wls.hostname}:${wls.port}" targets="${wls.server.name}" /&amp;gt;&lt;br /&gt;&amp;lt;/target&amp;gt;&lt;br /&gt;&amp;lt;/project&amp;gt;&lt;/pre&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-weight: bold;"&gt;build.properties&lt;/span&gt;&lt;pre&gt;bea.home=c:/bea&lt;br /&gt;wl.home=${bea.home}/wlserver_10.3&lt;br /&gt;wls.hostname=localhost&lt;br /&gt;wls.port=7001&lt;br /&gt;wls.username=weblogic&lt;br /&gt;wls.password=password&lt;br /&gt;wls.server.name=AdminServer&lt;br /&gt;debuglevel=lines,vars,source&lt;br /&gt;sourceVersion=1.6&lt;/pre&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/18313336-8089916138218384018?l=java-x.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://java-x.blogspot.com/feeds/8089916138218384018/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://java-x.blogspot.com/2010/07/implementing-rest-webservice-using-jax.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/18313336/posts/default/8089916138218384018'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/18313336/posts/default/8089916138218384018'/><link rel='alternate' type='text/html' href='http://java-x.blogspot.com/2010/07/implementing-rest-webservice-using-jax.html' title='Implementing REST WebService using JAX-WS'/><author><name>Abhilash Vuyyuru</name><uri>https://profiles.google.com/107328798079149921932</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh6.googleusercontent.com/-NxAgT10-nrk/AAAAAAAAAAI/AAAAAAAAB0o/Gz3VMjlEXb0/s512-c/photo.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-18313336.post-8884396945079421071</id><published>2009-07-29T13:49:00.002-04:00</published><updated>2009-07-29T13:54:54.085-04:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='spring'/><category scheme='http://www.blogger.com/atom/ns#' term='xml'/><category scheme='http://www.blogger.com/atom/ns#' term='aop'/><category scheme='http://www.blogger.com/atom/ns#' term='example/sample code'/><title type='text'>Spring AOP with XML-based Configuration</title><content type='html'>The previous post described how to use&lt;a href="spring-aop-with-aspecj-annotations.html"&gt; AspectJ annotations to implement Spring AOP&lt;/a&gt;. However, many programmers prefer to put configuration outside code. In such case Spring support for AOP through XML declaration of AOP components comes in handy. In this post we'll see how the same application can be implemented using &lt;span style="font-style: italic;"&gt;Schema-based AOP support&lt;/span&gt; in Spring. An example of using pointcuts  method parameters is also shown. Here's the code, followed by the explanations.&lt;br /&gt;&lt;br /&gt;The Simple application ...&lt;span class="fullpost"&gt;&lt;ul&gt;&lt;li&gt;&lt;span style="font-weight: bold;"&gt;Search Engine Interface : &lt;/span&gt;&lt;pre&gt;&lt;div class="java"&gt;&lt;code class="java"&gt;&lt;span class="java2"&gt;/*&lt;br /&gt;* Author: Abhi Vuyyuru&lt;br /&gt;*/&lt;br /&gt;&lt;/span&gt;&lt;span class="java4"&gt;package &lt;/span&gt;&lt;span class="java10"&gt;search;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span class="java4"&gt;import &lt;/span&gt;&lt;span class="java10"&gt;java.util.List;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span class="java4"&gt;public interface &lt;/span&gt;&lt;span class="java10"&gt;SearchEngine &lt;/span&gt;&lt;span class="java8"&gt;{&lt;br /&gt;&lt;/span&gt;&lt;span class="java4"&gt;public &lt;/span&gt;&lt;span class="java10"&gt;List&amp;lt;String&amp;gt; search&lt;/span&gt;&lt;span class="java8"&gt;(&lt;/span&gt;&lt;span class="java10"&gt;String prefix&lt;/span&gt;&lt;span class="java8"&gt;)&lt;/span&gt;&lt;span class="java10"&gt;;&lt;br /&gt;&lt;/span&gt;&lt;span class="java8"&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/div&gt;&lt;/pre&gt;SearchEngine.java&lt;/li&gt;&lt;li&gt;Search Engine Class : &lt;pre&gt;&lt;div class="java"&gt;&lt;code class="java"&gt;&lt;span class="java2"&gt;/*&lt;br /&gt;* Author: Abhi Vuyyuru&lt;br /&gt;*/&lt;/span&gt;&lt;span class="java4"&gt;package &lt;/span&gt;&lt;span class="java10"&gt;search;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span class="java4"&gt;import &lt;/span&gt;&lt;span class="java10"&gt;java.util.ArrayList;&lt;br /&gt;&lt;/span&gt;&lt;span class="java4"&gt;import &lt;/span&gt;&lt;span class="java10"&gt;java.util.List;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span class="java4"&gt;public class &lt;/span&gt;&lt;span class="java10"&gt;SearchEngineImpl &lt;/span&gt;&lt;span class="java4"&gt;implements &lt;/span&gt;&lt;span class="java10"&gt;SearchEngine &lt;/span&gt;&lt;span class="java8"&gt;{&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span class="java4"&gt;public &lt;/span&gt;&lt;span class="java10"&gt;List&amp;lt;String&amp;gt; search&lt;/span&gt;&lt;span class="java8"&gt;(&lt;/span&gt;&lt;span class="java10"&gt;String prefix&lt;/span&gt;&lt;span class="java8"&gt;) {&lt;br /&gt;&lt;/span&gt;&lt;span class="java10"&gt;System.out.println&lt;/span&gt;&lt;span class="java8"&gt;(&lt;/span&gt;&lt;span class="java5"&gt;"In search implementation"&lt;/span&gt;&lt;span class="java8"&gt;)&lt;/span&gt;&lt;span class="java10"&gt;;&lt;br /&gt;List&amp;lt;String&amp;gt; list = &lt;/span&gt;&lt;span class="java4"&gt;new &lt;/span&gt;&lt;span class="java10"&gt;ArrayList&amp;lt;String&amp;gt;&lt;/span&gt;&lt;span class="java8"&gt;()&lt;/span&gt;&lt;span class="java10"&gt;;&lt;br /&gt;list.add&lt;/span&gt;&lt;span class="java8"&gt;(&lt;/span&gt;&lt;span class="java10"&gt;prefix + &lt;/span&gt;&lt;span class="java5"&gt;" result 1"&lt;/span&gt;&lt;span class="java8"&gt;)&lt;/span&gt;&lt;span class="java10"&gt;;&lt;br /&gt;list.add&lt;/span&gt;&lt;span class="java8"&gt;(&lt;/span&gt;&lt;span class="java10"&gt;prefix + &lt;/span&gt;&lt;span class="java5"&gt;" result 2"&lt;/span&gt;&lt;span class="java8"&gt;)&lt;/span&gt;&lt;span class="java10"&gt;;&lt;br /&gt;&lt;/span&gt;&lt;span class="java4"&gt;return &lt;/span&gt;&lt;span class="java10"&gt;list;&lt;br /&gt;&lt;/span&gt;&lt;span class="java8"&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/div&gt;&lt;/pre&gt;SearchEngineImpl.java&lt;/li&gt;&lt;li&gt;Main method: &lt;pre&gt;&lt;div class="java"&gt;&lt;code class="java"&gt;&lt;span class="java2"&gt;/*&lt;br /&gt;* Author: Abhi Vuyyuru&lt;br /&gt;*/&lt;br /&gt;&lt;/span&gt;&lt;span class="java4"&gt;package &lt;/span&gt;&lt;span class="java10"&gt;main;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span class="java4"&gt;import &lt;/span&gt;&lt;span class="java10"&gt;java.util.List;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span class="java4"&gt;import &lt;/span&gt;&lt;span class="java10"&gt;org.springframework.context.ApplicationContext;&lt;br /&gt;&lt;/span&gt;&lt;span class="java4"&gt;import &lt;/span&gt;&lt;span class="java10"&gt;org.springframework.context.support.FileSystemXmlApplicationContext;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span class="java4"&gt;import &lt;/span&gt;&lt;span class="java10"&gt;search.SearchEngine;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span class="java4"&gt;public class &lt;/span&gt;&lt;span class="java10"&gt;AopTest &lt;/span&gt;&lt;span class="java8"&gt;{&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span class="java4"&gt;public static &lt;/span&gt;&lt;span class="java9"&gt;void &lt;/span&gt;&lt;span class="java10"&gt;main&lt;/span&gt;&lt;span class="java8"&gt;(&lt;/span&gt;&lt;span class="java10"&gt;String&lt;/span&gt;&lt;span class="java8"&gt;[] &lt;/span&gt;&lt;span class="java10"&gt;args&lt;/span&gt;&lt;span class="java8"&gt;) {&lt;br /&gt;   &lt;/span&gt;&lt;span class="java10"&gt;ApplicationContext ctx = &lt;/span&gt;&lt;span class="java4"&gt;new &lt;/span&gt;&lt;span class="java10"&gt;FileSystemXmlApplicationContext&lt;/span&gt;&lt;span class="java8"&gt;(&lt;br /&gt;           &lt;/span&gt;&lt;span class="java5"&gt;"applicationContext.xml"&lt;/span&gt;&lt;span class="java8"&gt;)&lt;/span&gt;&lt;span class="java10"&gt;;&lt;br /&gt;&lt;br /&gt;   SearchEngine searchEngine = &lt;/span&gt;&lt;span class="java8"&gt;(&lt;/span&gt;&lt;span class="java10"&gt;SearchEngine&lt;/span&gt;&lt;span class="java8"&gt;) &lt;/span&gt;&lt;span class="java10"&gt;ctx.getBean&lt;/span&gt;&lt;span class="java8"&gt;(&lt;/span&gt;&lt;span class="java5"&gt;"searchEngine"&lt;/span&gt;&lt;span class="java8"&gt;)&lt;/span&gt;&lt;span class="java10"&gt;;&lt;br /&gt;   List&amp;lt;String&amp;gt; searchResults = searchEngine.search&lt;/span&gt;&lt;span class="java8"&gt;(&lt;/span&gt;&lt;span class="java5"&gt;"search"&lt;/span&gt;&lt;span class="java8"&gt;)&lt;/span&gt;&lt;span class="java10"&gt;;&lt;br /&gt;    System.out.println&lt;/span&gt;&lt;span class="java8"&gt;(&lt;/span&gt;&lt;span class="java5"&gt;"Number of results : " &lt;/span&gt;&lt;span class="java10"&gt;+ searchResults.size&lt;/span&gt;&lt;span class="java8"&gt;())&lt;/span&gt;&lt;span class="java10"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span class="java8"&gt;}&lt;br /&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/div&gt;&lt;/pre&gt;AopTest.java&lt;/li&gt;&lt;/ul&gt;Spring AOP classes and configuration&lt;ul&gt;&lt;li&gt;Appication Conext : This the spring configuration file for the example.&lt;pre&gt;&amp;lt;?xml version="1.0" encoding="UTF-8"?&amp;gt;&lt;br /&gt;&amp;lt;beans xmlns="http://www.springframework.org/schema/beans"&lt;br /&gt;&amp;amp;nbspxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"&lt;br /&gt;&amp;amp;nbspxmlns:tx="http://www.springframework.org/schema/tx"&lt;br /&gt;&amp;amp;nbspxsi:schemaLocation="&lt;br /&gt;  &amp;amp;nbsphttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd&lt;br /&gt;  &amp;amp;nbsphttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd&lt;br /&gt;  &amp;amp;nbsphttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"&amp;gt;&lt;br /&gt; &amp;lt;!--&lt;br /&gt;  &amp;lt;aop:aspectj-autoproxy proxy-target-class="true"/&amp;gt; &amp;lt;bean&lt;br /&gt; &amp;amp;nbspid="adviceObject" class="aop.AspectJAdvice" /&amp;gt;&lt;br /&gt;&amp;amp;nbsp--&amp;gt;&lt;br /&gt; &amp;lt;aop:config&amp;gt;&lt;br /&gt;  &amp;lt;!-- Pointcut for every method invocation --&amp;gt;&lt;br /&gt;  &amp;lt;aop:pointcut id="searchEnginePc" expression="execution(* *(..))" /&amp;gt;&lt;br /&gt; &lt;br /&gt;  &amp;lt;!-- Pointcut for every method invocation with a String parameter --&amp;gt;&lt;br /&gt;  &amp;lt;aop:pointcut id="pointcutWithParam" expression="execution(* *(..)) and args(prefix)" /&amp;gt;&lt;br /&gt; &lt;br /&gt;  &amp;lt;!-- After returning advice --&amp;gt;&lt;br /&gt;  &amp;lt;aop:aspect id="springAspect" ref="adviceObject"&amp;gt;&lt;br /&gt; &lt;br /&gt;   &amp;lt;aop:around pointcut-ref="searchEnginePc" method="aroundAdvice" /&amp;gt;&lt;br /&gt; &lt;br /&gt;   &amp;lt;aop:after-returning pointcut-ref="searchEnginePc"&lt;br /&gt;   &amp;amp;nbspmethod="afterAdvice" /&amp;gt;&lt;br /&gt; &lt;br /&gt;   &amp;lt;aop:before pointcut-ref="pointcutWithParam" method="beforeAdvice" /&amp;gt;&lt;br /&gt;&lt;br /&gt;  &amp;lt;/aop:aspect&amp;gt;&lt;br /&gt; &amp;lt;/aop:config&amp;gt;&lt;br /&gt;&lt;br /&gt; &amp;lt;bean id="adviceObject" class="aop.SpringAdvice" /&amp;gt;&lt;br /&gt; &amp;lt;bean id="searchEngine" class="search.SearchEngineImpl" /&amp;gt;&lt;br /&gt;&amp;lt;/beans&amp;gt;&lt;/pre&gt;applicationContext.xml&lt;br /&gt;&lt;ul&gt;&lt;li&gt;This xml is used for the default Spring AOP implementation with Java dynamic proxies. Note that the main method is programmed to the SearchEngine interface rather than the concrete class.&lt;pre&gt;&lt;div class="java"&gt;&lt;code class="java"&gt;&lt;span class="java0"&gt;        &lt;/span&gt;&lt;span class="java10"&gt;SearchEngine searchEngine = &lt;/span&gt;&lt;span class="java8"&gt;(&lt;/span&gt;&lt;span class="java10"&gt;SearchEngine&lt;/span&gt;&lt;span class="java8"&gt;) &lt;/span&gt;&lt;span class="java10"&gt;ctx.getBean&lt;/span&gt;&lt;span class="java8"&gt;(&lt;/span&gt;&lt;span class="java5"&gt;"searchEngine"&lt;/span&gt;&lt;span class="java8"&gt;)&lt;/span&gt;&lt;span class="java10"&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/div&gt;&lt;/pre&gt;Hence, the AOP configuration was set to &lt;pre&gt;&amp;lt;aop:config &amp;gt;&lt;/pre&gt;If you have to use the concrete class in your application, as shown below &lt;pre&gt;&lt;div class="java"&gt;&lt;code class="java"&gt;&lt;span class="java0"&gt;        &lt;/span&gt;&lt;span class="java10"&gt;SearchEngineImpl searchEngine = &lt;/span&gt;&lt;span class="java8"&gt;(&lt;/span&gt;&lt;span class="java10"&gt;SearchEngineImpl&lt;/span&gt;&lt;span class="java8"&gt;) &lt;/span&gt;&lt;span class="java10"&gt;ctx.getBean&lt;/span&gt;&lt;span class="java8"&gt;(&lt;/span&gt;&lt;span class="java5"&gt;"searchEngine"&lt;/span&gt;&lt;span class="java8"&gt;)&lt;/span&gt;&lt;span class="java10"&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/div&gt;&lt;/pre&gt;You have to change the AOP configuration to the following&lt;pre&gt;&amp;lt;!-- proxy-target-class forces the use of CGLIB proxies, which allows proxying classes in addition to interfaces.--&amp;gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;&amp;lt;aop:config proxy-target-class="true"&amp;gt;&lt;/span&gt;&lt;/pre&gt;This will force the use of CGLIB proxies which allow proxying concrete classes too.&lt;/li&gt;&lt;li&gt;The aop-config element defines three different types of advice, around-advice, before advice, and after return advice.&lt;ul&gt;&lt;li&gt;&lt;span style="font-weight: bold;"&gt;Before Advice&lt;/span&gt;: Applied before a join point. This does not have the ability to prevent jointpoint (method execution) unless it throws an exception. The Before advice in the example uses a pointcut that uses arguments and can be used as a model for applying Advice for methods based on the parameter passed to the methods.&lt;/li&gt;&lt;li&gt;&lt;span style="font-weight: bold;"&gt;After Return Advice&lt;/span&gt;: Applied after the join points returns without exception.&lt;/li&gt;&lt;li&gt;&lt;span style="font-weight: bold;"&gt;Around Advice&lt;/span&gt;: Is applied around a join point. &lt;span style="font-weight: bold; color: rgb(51, 51, 255);"&gt;Note that the around advice is NOT called "before and after" the join point, but rather "around" the join point&lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;.&lt;/span&gt; If you look at the code below, the around advice is invoked when the join point is about to execute, but the around advice takes control of the execution. If you do not call the pjp.proceed() method in the following code, the join point will not be invoked. &lt;pre&gt;&lt;div class="java"&gt;&lt;code class="java"&gt;&lt;span class="java0"&gt;  &lt;/span&gt;&lt;span class="java2"&gt;/*&lt;br /&gt;* Applied around a any public method.&lt;br /&gt;*/&lt;br /&gt;&lt;/span&gt;&lt;span class="java4"&gt;public &lt;/span&gt;&lt;span class="java10"&gt;Object aroundAdvice&lt;/span&gt;&lt;span class="java8"&gt;(&lt;/span&gt;&lt;span class="java10"&gt;ProceedingJoinPoint pjp&lt;/span&gt;&lt;span class="java8"&gt;) &lt;/span&gt;&lt;span class="java4"&gt;throws &lt;/span&gt;&lt;span class="java10"&gt;Throwable &lt;/span&gt;&lt;span class="java8"&gt;{&lt;br /&gt;&lt;/span&gt;&lt;span class="java10"&gt;System.out.println&lt;/span&gt;&lt;span class="java8"&gt;(&lt;/span&gt;&lt;span class="java5"&gt;"Around advice before joinpoint"&lt;/span&gt;&lt;span class="java8"&gt;)&lt;/span&gt;&lt;span class="java10"&gt;;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Object obj = pjp.proceed&lt;/span&gt;&lt;/span&gt;&lt;span style="font-weight: bold;" class="java8"&gt;()&lt;/span&gt;&lt;span class="java10"&gt;&lt;span style="font-weight: bold;"&gt;;&lt;/span&gt;&lt;br /&gt;System.out.println&lt;/span&gt;&lt;span class="java8"&gt;(&lt;/span&gt;&lt;span class="java5"&gt;"Around advice after running"&lt;/span&gt;&lt;span class="java8"&gt;)&lt;/span&gt;&lt;span class="java10"&gt;;&lt;br /&gt;&lt;/span&gt;&lt;span class="java4"&gt;return &lt;/span&gt;&lt;span class="java10"&gt;obj;&lt;br /&gt;&lt;/span&gt;&lt;span class="java8"&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/div&gt;&lt;/pre&gt; Around advice has the ability to change the behavior of the join point, and even to stop the join point execution.&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;li style="font-weight: bold;"&gt;The Aspect:&lt;/li&gt;&lt;/ul&gt;&lt;pre&gt;&lt;div class="java"&gt;&lt;code class="java"&gt;&lt;span class="java2"&gt;/*&lt;br /&gt;* Author: Abhi Vuyyuru&lt;br /&gt;*/&lt;br /&gt;&lt;/span&gt;&lt;span class="java4"&gt;package &lt;/span&gt;&lt;span class="java10"&gt;aop;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span class="java4"&gt;import &lt;/span&gt;&lt;span class="java10"&gt;org.aspectj.lang.ProceedingJoinPoint;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span class="java4"&gt;public class &lt;/span&gt;&lt;span class="java10"&gt;SpringAdvice &lt;/span&gt;&lt;span class="java8"&gt;{&lt;br /&gt;  &lt;/span&gt;&lt;span class="java2"&gt;/*&lt;br /&gt;   * Applied around a any public method, based on XML configuration&lt;br /&gt;   */&lt;br /&gt;  &lt;/span&gt;&lt;span class="java4"&gt;public &lt;/span&gt;&lt;span class="java10"&gt;Object aroundAdvice&lt;/span&gt;&lt;span class="java8"&gt;(&lt;/span&gt;&lt;span class="java10"&gt;ProceedingJoinPoint pjp&lt;/span&gt;&lt;span class="java8"&gt;) &lt;/span&gt;&lt;span class="java4"&gt;throws &lt;/span&gt;&lt;span class="java10"&gt;Throwable &lt;/span&gt;&lt;span class="java8"&gt;{&lt;br /&gt;    &lt;/span&gt;&lt;span class="java10"&gt;System.out.println&lt;/span&gt;&lt;span class="java8"&gt;(&lt;/span&gt;&lt;span class="java5"&gt;"Around advice before joinpoint"&lt;/span&gt;&lt;span class="java8"&gt;)&lt;/span&gt;&lt;span class="java10"&gt;;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;    Object obj = pjp.proceed&lt;/span&gt;&lt;/span&gt;&lt;span style="font-weight: bold;" class="java8"&gt;()&lt;/span&gt;&lt;span class="java10"&gt;&lt;span style="font-weight: bold;"&gt;;&lt;/span&gt;&lt;br /&gt;    System.out.println&lt;/span&gt;&lt;span class="java8"&gt;(&lt;/span&gt;&lt;span class="java5"&gt;"Around advice after running"&lt;/span&gt;&lt;span class="java8"&gt;)&lt;/span&gt;&lt;span class="java10"&gt;;&lt;br /&gt;    &lt;/span&gt;&lt;span class="java4"&gt;return &lt;/span&gt;&lt;span class="java10"&gt;obj;&lt;br /&gt;  &lt;/span&gt;&lt;span class="java8"&gt;}&lt;br /&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span class="java2"&gt;/*&lt;br /&gt;   * Applied before the execution of any method which takes a String argument.&lt;br /&gt;   * Based on XML configuration&lt;br /&gt;   */&lt;br /&gt;  &lt;/span&gt;&lt;span class="java4"&gt;public &lt;/span&gt;&lt;span class="java9"&gt;void &lt;/span&gt;&lt;span class="java10"&gt;beforeAdvice&lt;/span&gt;&lt;span class="java8"&gt;(&lt;/span&gt;&lt;span class="java10"&gt;String prefix&lt;/span&gt;&lt;span class="java8"&gt;) {&lt;br /&gt;    &lt;/span&gt;&lt;span class="java10"&gt;System.out.println&lt;/span&gt;&lt;span class="java8"&gt;(&lt;/span&gt;&lt;span class="java5"&gt;"Before advice : " &lt;/span&gt;&lt;span class="java10"&gt;+ prefix&lt;/span&gt;&lt;span class="java8"&gt;)&lt;/span&gt;&lt;span class="java10"&gt;;&lt;br /&gt;  &lt;/span&gt;&lt;span class="java8"&gt;}&lt;br /&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span class="java2"&gt;/*&lt;br /&gt;   * Applied after returning, based on XML configuration&lt;br /&gt;   */&lt;br /&gt;  &lt;/span&gt;&lt;span class="java4"&gt;public &lt;/span&gt;&lt;span class="java9"&gt;void &lt;/span&gt;&lt;span class="java10"&gt;afterAdvice&lt;/span&gt;&lt;span class="java8"&gt;() {&lt;br /&gt;    &lt;/span&gt;&lt;span class="java10"&gt;System.out.println&lt;/span&gt;&lt;span class="java8"&gt;(&lt;/span&gt;&lt;span class="java5"&gt;"After Returning advice"&lt;/span&gt;&lt;span class="java8"&gt;)&lt;/span&gt;&lt;span class="java10"&gt;;&lt;br /&gt;  &lt;/span&gt;&lt;span class="java8"&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/div&gt;&lt;/pre&gt;AspectJAdvice.java&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/18313336-8884396945079421071?l=java-x.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://java-x.blogspot.com/feeds/8884396945079421071/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://java-x.blogspot.com/2009/07/spring-aop-with-xml-based-configuration.html#comment-form' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/18313336/posts/default/8884396945079421071'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/18313336/posts/default/8884396945079421071'/><link rel='alternate' type='text/html' href='http://java-x.blogspot.com/2009/07/spring-aop-with-xml-based-configuration.html' title='Spring AOP with XML-based Configuration'/><author><name>Abhilash Vuyyuru</name><uri>https://profiles.google.com/107328798079149921932</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh6.googleusercontent.com/-NxAgT10-nrk/AAAAAAAAAAI/AAAAAAAAB0o/Gz3VMjlEXb0/s512-c/photo.jpg'/></author><thr:total>5</thr:total></entry><entry><id>tag:blogger.com,1999:blog-18313336.post-8754536447442596240</id><published>2009-07-28T14:43:00.007-04:00</published><updated>2009-07-29T13:55:35.370-04:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='spring'/><category scheme='http://www.blogger.com/atom/ns#' term='aop'/><category scheme='http://www.blogger.com/atom/ns#' term='example/sample code'/><title type='text'>Spring AOP with AspectJ Annotations</title><content type='html'>This post describes how to use Spring AOP with AspectJ aspects using AspectJ annotations. An example of using pointcuts  method parameters is also shown. I assume that you are familiar with the basic AOP concepts, however, here are a couple of things that you must to know if you are new to AOP.&lt;ul&gt;&lt;li&gt;&lt;span style="font-weight: bold;"&gt;Cross-cutting concerns&lt;/span&gt;: Any part of the application that has implications through most of the major modules of the application may be termed as a cross-cutting concern. Coding such cross-cutting concerns into the business methods will not only increase code-complexity but also reduce re-usability. Application security and transaction management are the best examples of cross-cutting concerns.&lt;/li&gt;&lt;li&gt;&lt;span style="font-weight: bold;"&gt;Aspect&lt;/span&gt;: An aspect is the "&lt;span style="font-style: italic;"&gt;unit of modularity&lt;/span&gt;" of any of the cross-cutting concerns within the applications. In Spring AOP, aspects are implemented using regular Java class &lt;ul&gt;&lt;li&gt;with annotations AspectJ is enabled&lt;/li&gt;&lt;li&gt;declaratively, without annotations, in the application context.&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-weight: bold;"&gt;Join point and pointcut&lt;/span&gt;: join point is any point during the program execution of a program, such as the execution of a method or the handling of an exception. Spring AOP only supports methods as join points. A pointcut is a predicate which is used to identify join points.&lt;/li&gt;&lt;li&gt;&lt;span style="font-weight: bold;"&gt;Advice&lt;/span&gt;: Action applied at join points. Advice can be applied before, after  or around a join point. In Spring, advice is modeled as an interceptor. Multiple advices hare maintained as a chain of intercptors around the join point.&lt;/li&gt;&lt;/ul&gt;One important thing to note here is that, by default, Spring uses &lt;span style="font-weight: bold;"&gt;Java Dynamic Proxies&lt;/span&gt; to implement Aspect Oriented Programming support. This requires you to "&lt;span style="font-style: italic;"&gt;program to interfaces rather than classes&lt;/span&gt;" which is anyway a good programming practice. But if you have to code to concrete classes, &lt;span style="font-weight: bold;"&gt;you can force Spring AOP to use CGLIB proxies&lt;/span&gt;. How to do so is explained with the example. For an in-depth understanding of AOP concepts, refer to either AspectJ and/or Spring AOP documentation. Here's the code, followed by the explanations.&lt;br /&gt;&lt;br /&gt;The Simple application ...&lt;span class="fullpost"&gt;&lt;ul&gt;&lt;li&gt;Search Engine Interface : &lt;pre&gt;&lt;div class="java"&gt;&lt;code class="java"&gt;&lt;span class="java2"&gt;/*&lt;br /&gt;* Author: Abhi Vuyyuru&lt;br /&gt;*/&lt;br /&gt;&lt;/span&gt;&lt;span class="java4"&gt;package &lt;/span&gt;&lt;span class="java10"&gt;search;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span class="java4"&gt;import &lt;/span&gt;&lt;span class="java10"&gt;java.util.List;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span class="java4"&gt;public interface &lt;/span&gt;&lt;span class="java10"&gt;SearchEngine &lt;/span&gt;&lt;span class="java8"&gt;{&lt;br /&gt;&lt;/span&gt;&lt;span class="java4"&gt;public &lt;/span&gt;&lt;span class="java10"&gt;List&amp;lt;String&amp;gt; search&lt;/span&gt;&lt;span class="java8"&gt;(&lt;/span&gt;&lt;span class="java10"&gt;String prefix&lt;/span&gt;&lt;span class="java8"&gt;)&lt;/span&gt;&lt;span class="java10"&gt;;&lt;br /&gt;&lt;/span&gt;&lt;span class="java8"&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/div&gt;&lt;/pre&gt;SearchEngine.java&lt;/li&gt;&lt;li&gt;Search Engine Class : &lt;pre&gt;&lt;div class="java"&gt;&lt;code class="java"&gt;&lt;span class="java2"&gt;/*&lt;br /&gt;* Author: Abhi Vuyyuru&lt;br /&gt;*/&lt;/span&gt;&lt;span class="java4"&gt;package &lt;/span&gt;&lt;span class="java10"&gt;search;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span class="java4"&gt;import &lt;/span&gt;&lt;span class="java10"&gt;java.util.ArrayList;&lt;br /&gt;&lt;/span&gt;&lt;span class="java4"&gt;import &lt;/span&gt;&lt;span class="java10"&gt;java.util.List;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span class="java4"&gt;public class &lt;/span&gt;&lt;span class="java10"&gt;SearchEngineImpl &lt;/span&gt;&lt;span class="java4"&gt;implements &lt;/span&gt;&lt;span class="java10"&gt;SearchEngine &lt;/span&gt;&lt;span class="java8"&gt;{&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span class="java4"&gt;public &lt;/span&gt;&lt;span class="java10"&gt;List&amp;lt;String&amp;gt; search&lt;/span&gt;&lt;span class="java8"&gt;(&lt;/span&gt;&lt;span class="java10"&gt;String prefix&lt;/span&gt;&lt;span class="java8"&gt;) {&lt;br /&gt;&lt;/span&gt;&lt;span class="java10"&gt;System.out.println&lt;/span&gt;&lt;span class="java8"&gt;(&lt;/span&gt;&lt;span class="java5"&gt;"In search implementation"&lt;/span&gt;&lt;span class="java8"&gt;)&lt;/span&gt;&lt;span class="java10"&gt;;&lt;br /&gt;List&amp;lt;String&amp;gt; list = &lt;/span&gt;&lt;span class="java4"&gt;new &lt;/span&gt;&lt;span class="java10"&gt;ArrayList&amp;lt;String&amp;gt;&lt;/span&gt;&lt;span class="java8"&gt;()&lt;/span&gt;&lt;span class="java10"&gt;;&lt;br /&gt;list.add&lt;/span&gt;&lt;span class="java8"&gt;(&lt;/span&gt;&lt;span class="java10"&gt;prefix + &lt;/span&gt;&lt;span class="java5"&gt;" result 1"&lt;/span&gt;&lt;span class="java8"&gt;)&lt;/span&gt;&lt;span class="java10"&gt;;&lt;br /&gt;list.add&lt;/span&gt;&lt;span class="java8"&gt;(&lt;/span&gt;&lt;span class="java10"&gt;prefix + &lt;/span&gt;&lt;span class="java5"&gt;" result 2"&lt;/span&gt;&lt;span class="java8"&gt;)&lt;/span&gt;&lt;span class="java10"&gt;;&lt;br /&gt;&lt;/span&gt;&lt;span class="java4"&gt;return &lt;/span&gt;&lt;span class="java10"&gt;list;&lt;br /&gt;&lt;/span&gt;&lt;span class="java8"&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/div&gt;&lt;/pre&gt;SearchEngineImpl.java&lt;/li&gt;&lt;li&gt;Main method: &lt;pre&gt;&lt;div class="java"&gt;&lt;code class="java"&gt;&lt;span class="java2"&gt;/*&lt;br /&gt;* Author: Abhi Vuyyuru&lt;br /&gt;*/&lt;br /&gt;&lt;/span&gt;&lt;span class="java4"&gt;package &lt;/span&gt;&lt;span class="java10"&gt;main;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span class="java4"&gt;import &lt;/span&gt;&lt;span class="java10"&gt;java.util.List;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span class="java4"&gt;import &lt;/span&gt;&lt;span class="java10"&gt;org.springframework.context.ApplicationContext;&lt;br /&gt;&lt;/span&gt;&lt;span class="java4"&gt;import &lt;/span&gt;&lt;span class="java10"&gt;org.springframework.context.support.FileSystemXmlApplicationContext;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span class="java4"&gt;import &lt;/span&gt;&lt;span class="java10"&gt;search.SearchEngine;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span class="java4"&gt;public class &lt;/span&gt;&lt;span class="java10"&gt;AopTest &lt;/span&gt;&lt;span class="java8"&gt;{&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span class="java4"&gt;public static &lt;/span&gt;&lt;span class="java9"&gt;void &lt;/span&gt;&lt;span class="java10"&gt;main&lt;/span&gt;&lt;span class="java8"&gt;(&lt;/span&gt;&lt;span class="java10"&gt;String&lt;/span&gt;&lt;span class="java8"&gt;[] &lt;/span&gt;&lt;span class="java10"&gt;args&lt;/span&gt;&lt;span class="java8"&gt;) {&lt;br /&gt;    &lt;/span&gt;&lt;span class="java10"&gt;ApplicationContext ctx = &lt;/span&gt;&lt;span class="java4"&gt;new &lt;/span&gt;&lt;span class="java10"&gt;FileSystemXmlApplicationContext&lt;/span&gt;&lt;span class="java8"&gt;(&lt;br /&gt;            &lt;/span&gt;&lt;span class="java5"&gt;"applicationContext.xml"&lt;/span&gt;&lt;span class="java8"&gt;)&lt;/span&gt;&lt;span class="java10"&gt;;&lt;br /&gt;&lt;br /&gt;    SearchEngine searchEngine = &lt;/span&gt;&lt;span class="java8"&gt;(&lt;/span&gt;&lt;span class="java10"&gt;SearchEngine&lt;/span&gt;&lt;span class="java8"&gt;) &lt;/span&gt;&lt;span class="java10"&gt;ctx.getBean&lt;/span&gt;&lt;span class="java8"&gt;(&lt;/span&gt;&lt;span class="java5"&gt;"searchEngine"&lt;/span&gt;&lt;span class="java8"&gt;)&lt;/span&gt;&lt;span class="java10"&gt;;&lt;br /&gt;    List&amp;lt;String&amp;gt; searchResults = searchEngine.search&lt;/span&gt;&lt;span class="java8"&gt;(&lt;/span&gt;&lt;span class="java5"&gt;"search"&lt;/span&gt;&lt;span class="java8"&gt;)&lt;/span&gt;&lt;span class="java10"&gt;;&lt;br /&gt;     System.out.println&lt;/span&gt;&lt;span class="java8"&gt;(&lt;/span&gt;&lt;span class="java5"&gt;"Number of results : " &lt;/span&gt;&lt;span class="java10"&gt;+ searchResults.size&lt;/span&gt;&lt;span class="java8"&gt;())&lt;/span&gt;&lt;span class="java10"&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span class="java8"&gt;}&lt;br /&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/div&gt;&lt;/pre&gt;AopTest.java&lt;/li&gt;&lt;/ul&gt;Spring AOP classes and configuration&lt;ul&gt;&lt;li&gt;Appication Conext : This the spring configuration file for the example.&lt;pre&gt;&amp;lt;?xml version="1.0" encoding="UTF-8"?&amp;gt;&lt;br /&gt;&amp;lt;beans xmlns="http://www.springframework.org/schema/beans"&lt;br /&gt;xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"&lt;br /&gt;xmlns:tx="http://www.springframework.org/schema/tx"&lt;br /&gt;xsi:schemaLocation="&lt;br /&gt;  http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd&lt;br /&gt;  http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd&lt;br /&gt;  http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"&amp;gt;&lt;br /&gt;&lt;span style="font-weight: bold;" title="Enable AspectJ auto-proxy"&gt;&amp;lt;!--Enables AspectJ auto-proxying--&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;" title="Enable AspectJ auto-proxy"&gt;&amp;lt;aop:aspectj-autoproxy /&amp;gt;&lt;/span&gt;&lt;br /&gt;&amp;lt;bean id="adviceObject" class="aop.AspectJAdvice" /&amp;gt;&lt;br /&gt;&amp;lt;bean id="searchEngine" class="search.SearchEngineImpl" /&amp;gt;&lt;br /&gt;&amp;lt;/beans&amp;gt;&lt;/pre&gt;applicationContext.xml&lt;br /&gt;This xml is used for the default Spring AOP implementation with Java dynamic proxies. Note that the main method is programmed to the SearchEngine interface rather than the concrete class.&lt;pre&gt;&lt;div class="java"&gt;&lt;code class="java"&gt;&lt;span class="java0"&gt;        &lt;/span&gt;&lt;span class="java10"&gt;SearchEngine searchEngine = &lt;/span&gt;&lt;span class="java8"&gt;(&lt;/span&gt;&lt;span class="java10"&gt;SearchEngine&lt;/span&gt;&lt;span class="java8"&gt;) &lt;/span&gt;&lt;span class="java10"&gt;ctx.getBean&lt;/span&gt;&lt;span class="java8"&gt;(&lt;/span&gt;&lt;span class="java5"&gt;"searchEngine"&lt;/span&gt;&lt;span class="java8"&gt;)&lt;/span&gt;&lt;span class="java10"&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/div&gt;&lt;br /&gt;&lt;/pre&gt;Hence, the AOP configuration was set to &lt;pre&gt;&amp;lt;aop:aspectj-autoproxy /&amp;gt;&lt;/pre&gt;If you have to use the concrete class in your application, as shown below &lt;pre&gt;&lt;div class="java"&gt;&lt;code class="java"&gt;&lt;span class="java0"&gt;        &lt;/span&gt;&lt;span class="java10"&gt;SearchEngineImpl searchEngine = &lt;/span&gt;&lt;span class="java8"&gt;(&lt;/span&gt;&lt;span class="java10"&gt;SearchEngineImpl&lt;/span&gt;&lt;span class="java8"&gt;) &lt;/span&gt;&lt;span class="java10"&gt;ctx.getBean&lt;/span&gt;&lt;span class="java8"&gt;(&lt;/span&gt;&lt;span class="java5"&gt;"searchEngine"&lt;/span&gt;&lt;span class="java8"&gt;)&lt;/span&gt;&lt;span class="java10"&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/div&gt;&lt;br /&gt;&lt;/pre&gt;You have to change the AOP configuration to the following&lt;pre&gt;&amp;lt;!-- proxy-target-class forces the use of CGLIB proxies, which allows proxying classes in addition to interfaces.--&amp;gt;&lt;br /&gt;&lt;span style="font-weight: bold;" title="proxy-target-class forces the use of CGLIB proxies, which allows proxying classes in addition to interfaces."&gt;&amp;lt;aop:aspectj-autoproxy proxy-target-class="true" /&amp;gt;&lt;/span&gt;&lt;/pre&gt;This will force the use of CGLIB proxies which allow proxying concrete classes too.&lt;/li&gt;&lt;li&gt;The Aspect: This aspect defines a pointcut and three different types of advice, around-advice, before advice, and after return advice.&lt;ul&gt;&lt;li&gt;&lt;span style="font-weight: bold;"&gt;Before Advice&lt;/span&gt;: Applied before a join point. This does not have the ability to prevent jointpoint (method execution) unless it throws an exception.&lt;/li&gt;&lt;li&gt;&lt;span style="font-weight: bold;"&gt;After Return Advice&lt;/span&gt;: Applied after the join points returns without exception.&lt;/li&gt;&lt;li&gt;&lt;span style="font-weight: bold;"&gt;Around Advice&lt;/span&gt;: Is applied around a join point. &lt;span style="font-weight: bold;"&gt;Note that the around advice is NOT called "before and after" the join point, but rather "around" the join point&lt;/span&gt;. If you look at the code below, the around advice is invoked when the join point is about to execute, but the around advice takes control of the execution. If you do not call the pjp.proceed() method in the following code, the join point will not be invoked. &lt;pre&gt;&lt;div class="java"&gt;&lt;code class="java"&gt;&lt;span class="java0"&gt;  &lt;/span&gt;&lt;span class="java2"&gt;/*&lt;br /&gt;* Applied around a any public method.&lt;br /&gt;*/&lt;br /&gt;&lt;/span&gt;&lt;span class="java16"&gt;@Around&lt;/span&gt;&lt;span class="java8"&gt;(&lt;/span&gt;&lt;span class="java5"&gt;"execution(public * *(..))"&lt;/span&gt;&lt;span class="java8"&gt;)&lt;br /&gt;&lt;/span&gt;&lt;span class="java4"&gt;public &lt;/span&gt;&lt;span class="java10"&gt;Object aroundAdvice&lt;/span&gt;&lt;span class="java8"&gt;(&lt;/span&gt;&lt;span class="java10"&gt;ProceedingJoinPoint pjp&lt;/span&gt;&lt;span class="java8"&gt;) &lt;/span&gt;&lt;span class="java4"&gt;throws &lt;/span&gt;&lt;span class="java10"&gt;Throwable &lt;/span&gt;&lt;span class="java8"&gt;{&lt;br /&gt;&lt;/span&gt;&lt;span class="java10"&gt;System.out.println&lt;/span&gt;&lt;span class="java8"&gt;(&lt;/span&gt;&lt;span class="java5"&gt;"Around advice before joinpoint"&lt;/span&gt;&lt;span class="java8"&gt;)&lt;/span&gt;&lt;span class="java10"&gt;;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Object obj = pjp.proceed&lt;/span&gt;&lt;/span&gt;&lt;span style="font-weight: bold;" class="java8"&gt;()&lt;/span&gt;&lt;span class="java10"&gt;&lt;span style="font-weight: bold;"&gt;;&lt;/span&gt;&lt;br /&gt;System.out.println&lt;/span&gt;&lt;span class="java8"&gt;(&lt;/span&gt;&lt;span class="java5"&gt;"Around advice after running"&lt;/span&gt;&lt;span class="java8"&gt;)&lt;/span&gt;&lt;span class="java10"&gt;;&lt;br /&gt;&lt;/span&gt;&lt;span class="java4"&gt;return &lt;/span&gt;&lt;span class="java10"&gt;obj;&lt;br /&gt;&lt;/span&gt;&lt;span class="java8"&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/div&gt;&lt;/pre&gt;Around advice has the ability to change the behavior of the join point, and even to stop the join point execution.&lt;/li&gt;&lt;/ul&gt;&lt;pre&gt;&lt;div class="java"&gt;&lt;code class="java"&gt;&lt;span class="java2"&gt;/*&lt;br /&gt;* Author: Abhi Vuyyuru&lt;br /&gt;*/&lt;br /&gt;&lt;/span&gt;&lt;span class="java4"&gt;package &lt;/span&gt;&lt;span class="java10"&gt;aop;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span class="java4"&gt;import &lt;/span&gt;&lt;span class="java10"&gt;org.aspectj.lang.ProceedingJoinPoint;&lt;br /&gt;&lt;/span&gt;&lt;span class="java4"&gt;import &lt;/span&gt;&lt;span class="java10"&gt;org.aspectj.lang.annotation.AfterReturning;&lt;br /&gt;&lt;/span&gt;&lt;span class="java4"&gt;import &lt;/span&gt;&lt;span class="java10"&gt;org.aspectj.lang.annotation.Around;&lt;br /&gt;&lt;/span&gt;&lt;span class="java4"&gt;import &lt;/span&gt;&lt;span class="java10"&gt;org.aspectj.lang.annotation.Aspect;&lt;br /&gt;&lt;/span&gt;&lt;span class="java4"&gt;import &lt;/span&gt;&lt;span class="java10"&gt;org.aspectj.lang.annotation.Before;&lt;br /&gt;&lt;/span&gt;&lt;span class="java4"&gt;import &lt;/span&gt;&lt;span class="java10"&gt;org.aspectj.lang.annotation.Pointcut;&lt;br /&gt;&lt;/span&gt;&lt;span class="java16"&gt;@Aspect&lt;br /&gt;&lt;/span&gt;&lt;span class="java4"&gt;public class &lt;/span&gt;&lt;span class="java10"&gt;AspectJAdvice &lt;/span&gt;&lt;span class="java8"&gt;{&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span class="java2"&gt;/*&lt;br /&gt;* Applied around a any public method.&lt;br /&gt;*/&lt;br /&gt;&lt;/span&gt;&lt;span style="font-weight: bold;" class="java16"&gt;@Around&lt;/span&gt;&lt;span style="font-weight: bold;" class="java8"&gt;(&lt;/span&gt;&lt;span style="font-weight: bold;" class="java5"&gt;"execution(public * *(..))"&lt;/span&gt;&lt;span class="java8"&gt;&lt;span style="font-weight: bold;"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;span class="java4"&gt;public &lt;/span&gt;&lt;span class="java10"&gt;Object aroundAdvice&lt;/span&gt;&lt;span class="java8"&gt;(&lt;/span&gt;&lt;span class="java10"&gt;ProceedingJoinPoint pjp&lt;/span&gt;&lt;span class="java8"&gt;) &lt;/span&gt;&lt;span class="java4"&gt;throws &lt;/span&gt;&lt;span class="java10"&gt;Throwable &lt;/span&gt;&lt;span class="java8"&gt;{&lt;br /&gt;&lt;/span&gt;&lt;span class="java10"&gt;System.out.println&lt;/span&gt;&lt;span class="java8"&gt;(&lt;/span&gt;&lt;span class="java5"&gt;"Around advice before joinpoint"&lt;/span&gt;&lt;span class="java8"&gt;)&lt;/span&gt;&lt;span class="java10"&gt;;&lt;br /&gt;Object obj = pjp.proceed&lt;/span&gt;&lt;span class="java8"&gt;()&lt;/span&gt;&lt;span class="java10"&gt;;&lt;br /&gt;System.out.println&lt;/span&gt;&lt;span class="java8"&gt;(&lt;/span&gt;&lt;span class="java5"&gt;"Around advice after running"&lt;/span&gt;&lt;span class="java8"&gt;)&lt;/span&gt;&lt;span class="java10"&gt;;&lt;br /&gt;&lt;/span&gt;&lt;span class="java4"&gt;return &lt;/span&gt;&lt;span class="java10"&gt;obj;&lt;br /&gt;&lt;/span&gt;&lt;span class="java8"&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span class="java2"&gt;/*&lt;br /&gt;* Applied before the execution of any method which takes a String argument.&lt;br /&gt;*/&lt;br /&gt;&lt;/span&gt;&lt;span style="font-weight: bold;" class="java16"&gt;@Before&lt;/span&gt;&lt;span style="font-weight: bold;" class="java8"&gt;(&lt;/span&gt;&lt;span style="font-weight: bold;" class="java5"&gt;"execution(* *(..)) &amp;amp;&amp;amp;" &lt;/span&gt;&lt;span style="font-weight: bold;" class="java10"&gt;+ &lt;/span&gt;&lt;span style="font-weight: bold;" class="java5"&gt;"args(prefix)"&lt;/span&gt;&lt;span class="java8"&gt;&lt;span style="font-weight: bold;"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;span class="java4"&gt;public &lt;/span&gt;&lt;span class="java9"&gt;void &lt;/span&gt;&lt;span class="java10"&gt;beforeAdvice&lt;/span&gt;&lt;span class="java8"&gt;(&lt;/span&gt;&lt;span class="java10"&gt;String prefix&lt;/span&gt;&lt;span class="java8"&gt;) {&lt;br /&gt;&lt;/span&gt;&lt;span class="java10"&gt;System.out.println&lt;/span&gt;&lt;span class="java8"&gt;(&lt;/span&gt;&lt;span class="java5"&gt;"Before advice : " &lt;/span&gt;&lt;span class="java10"&gt;+ prefix&lt;/span&gt;&lt;span class="java8"&gt;)&lt;/span&gt;&lt;span class="java10"&gt;;&lt;br /&gt;&lt;/span&gt;&lt;span class="java8"&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span class="java2"&gt;/*&lt;br /&gt;* Applied after returning from the pointcut defined by anyPublicMethod&lt;br /&gt;*/&lt;br /&gt;&lt;/span&gt;&lt;span style="font-weight: bold;" class="java16"&gt;@AfterReturning&lt;/span&gt;&lt;span style="font-weight: bold;" class="java8"&gt;(&lt;/span&gt;&lt;span style="font-weight: bold;" class="java5"&gt;"anyPublicMethod()"&lt;/span&gt;&lt;span class="java8"&gt;&lt;span style="font-weight: bold;"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;span class="java4"&gt;public &lt;/span&gt;&lt;span class="java9"&gt;void &lt;/span&gt;&lt;span class="java10"&gt;afterAdvice&lt;/span&gt;&lt;span class="java8"&gt;() {&lt;br /&gt;&lt;/span&gt;&lt;span class="java10"&gt;System.out.println&lt;/span&gt;&lt;span class="java8"&gt;(&lt;/span&gt;&lt;span class="java5"&gt;"After Returning advice"&lt;/span&gt;&lt;span class="java8"&gt;)&lt;/span&gt;&lt;span class="java10"&gt;;&lt;br /&gt;&lt;/span&gt;&lt;span class="java8"&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span class="java2"&gt;/*&lt;br /&gt;* Defines a pointcut that matches any public method.&lt;br /&gt;*/&lt;br /&gt;&lt;/span&gt;&lt;span style="font-weight: bold;" class="java16"&gt;@Pointcut&lt;/span&gt;&lt;span style="font-weight: bold;" class="java8"&gt;(&lt;/span&gt;&lt;span style="font-weight: bold;" class="java5"&gt;"execution(public * *(..))"&lt;/span&gt;&lt;span class="java8"&gt;&lt;span style="font-weight: bold;"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;span class="java4"&gt;private &lt;/span&gt;&lt;span class="java9"&gt;void &lt;/span&gt;&lt;span class="java10"&gt;anyPublicMethod&lt;/span&gt;&lt;span class="java8"&gt;() {&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/div&gt;&lt;/pre&gt;AspectJAdvice.java&lt;br /&gt;&lt;ul&gt;&lt;li&gt;The pointcut here simply explains how a pointcut can be used instead of using predicates in the Advice annotations. Pointcuts come in handy when the advice predicates become too complex, in which case multiple pointcuts can be combined to form the proper predicate for matching the join point.&lt;/li&gt;&lt;li&gt;The Before advice in the example uses a predicate that uses arguments and can be used as a model for applying Advice for methods based on the parameter passed to the methods.&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/18313336-8754536447442596240?l=java-x.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://java-x.blogspot.com/feeds/8754536447442596240/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://java-x.blogspot.com/2009/07/spring-aop-with-aspecj-annotations.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/18313336/posts/default/8754536447442596240'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/18313336/posts/default/8754536447442596240'/><link rel='alternate' type='text/html' href='http://java-x.blogspot.com/2009/07/spring-aop-with-aspecj-annotations.html' title='Spring AOP with AspectJ Annotations'/><author><name>Abhilash Vuyyuru</name><uri>https://profiles.google.com/107328798079149921932</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh6.googleusercontent.com/-NxAgT10-nrk/AAAAAAAAAAI/AAAAAAAAB0o/Gz3VMjlEXb0/s512-c/photo.jpg'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-18313336.post-8303697830905533222</id><published>2009-07-15T10:52:00.002-04:00</published><updated>2009-07-15T11:17:02.892-04:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='testing'/><category scheme='http://www.blogger.com/atom/ns#' term='example/sample code'/><title type='text'>EasyMock for Unit Tests</title><content type='html'>One of the basic requirements for unit testing is the creation of mock objects. Easy mock is a library that helps to dynamically create mock objects. This way we can avoid much of the tedious work involved in manually coding mock objects. However, EasyMock itself is not the silver bullet, we still have to define expectations in test cases. Using EasyMock also means that you have make certain design changes (if your design follows the usual design principles you may not have to change much. Here's a sample Unit Test using EasyMock. You can download easymock from &lt;a href="http://www.easymock.org/"&gt;here.&lt;/a&gt;&lt;span class="fullpost"&gt;&lt;ul&gt;&lt;li&gt;Class Under Test: &lt;pre&gt;&lt;div class="java" align="left"&gt;&lt;br /&gt;&lt;table bg="" style="color: rgb(255, 255, 255);" border="0" cellpadding="3" cellspacing="0"&gt; &lt;tbody&gt;&lt;tr&gt;&lt;td align="left" valign="top" nowrap="nowrap"&gt;&lt;code&gt;&lt;br /&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;package &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;sample;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;import &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;java.text.DateFormat;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;import &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;java.text.ParseException;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;import &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;java.text.SimpleDateFormat;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;import &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;java.util.Date;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;public class &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;DateFormatter &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;  &lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;public static final &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;String DATE_FORMAT = &lt;/span&gt;&lt;span style="color: rgb(42, 0, 255);"&gt;"MM/dd/yyyy"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;  &lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;private &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;DateFormatterHelper helper = &lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;null&lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;  &lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;public &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;DateFormatter&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;() {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;  &lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;public &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;String convertToStandardFormat&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;String dateString, String format&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;) &lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;throws &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;ParseException &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;    &lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;if &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;dateString == &lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;null &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;|| dateString.equals&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(42, 0, 255);"&gt;""&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;))&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;      &lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;return &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(42, 0, 255);"&gt;""&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;dateString = dateString == &lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;null &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;? &lt;/span&gt;&lt;span style="color: rgb(42, 0, 255);"&gt;"" &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;: dateString;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;DateFormat df = helper.getDateFormat&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;format&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Date date = df.parse&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;dateString&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;SimpleDateFormat sdf = &lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;new &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;SimpleDateFormat&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;DATE_FORMAT&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;    &lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;return &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;sdf.format&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;date&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;  &lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;public &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;DateFormatterHelper getHelper&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;() {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;    &lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;return &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;helper;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;  &lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;public &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;void &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;setHelper&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;DateFormatterHelper helper&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;) {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;    &lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;this&lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;.helper = helper;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;}&lt;/span&gt;&lt;/code&gt; &lt;/td&gt; &lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;&lt;/pre&gt;DateFormatter.java&lt;/li&gt;&lt;li&gt;Helper Class and Interface: If you are to mock any class using EasyMock, it is required that you have an interface for the class. If you are following the old design principle "program to an interface, not an implementation", you are good here.&lt;pre&gt;&lt;div class="java" align="left"&gt;&lt;table bg="" style="color: rgb(255, 255, 255);" border="0" cellpadding="3" cellspacing="0"&gt; &lt;tbody&gt;&lt;tr&gt; &lt;td align="left" valign="top" nowrap="nowrap"&gt;  &lt;code&gt;&lt;br /&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;package &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;sample;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;import &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;java.text.DateFormat;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;public interface &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;DateFormatterHelper &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;  &lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;public &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;DateFormat getDateFormat&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;String format&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;}&lt;/span&gt;&lt;/code&gt; &lt;/td&gt; &lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;&lt;/pre&gt;DateFormatterHelper.java&lt;pre&gt;&lt;div class="java" align="left"&gt;&lt;br /&gt;&lt;table bg="" style="color: rgb(255, 255, 255);" border="0" cellpadding="3" cellspacing="0"&gt; &lt;tbody&gt;&lt;tr&gt; &lt;td align="left" valign="top" nowrap="nowrap"&gt;  &lt;code&gt;&lt;br /&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;package &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;sample;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;import &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;java.text.DateFormat;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;import &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;java.text.ParseException;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;import &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;java.text.SimpleDateFormat;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;import &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;java.util.Calendar;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;public class &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;DateFormatterHelperImpl &lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;implements &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;DateFormatterHelper &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;  &lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;public &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;DateFormatterHelperImpl&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;() {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;  &lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;public &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;DateFormat getDateFormat&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;String format&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;) {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;SimpleDateFormat sdf = &lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;new &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;SimpleDateFormat&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;format&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;sdf.setCalendar&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Calendar.getInstance&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;())&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;sdf.setLenient&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;false&lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;    &lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;return &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;sdf;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;  &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;  &lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;public static &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;void &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;main&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;String args&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;[]) {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;DateFormatterHelper helper = &lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;new &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;DateFormatterHelperImpl&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;()&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;    &lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;try &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;          &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;System.out.println&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;helper.getDateFormat&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(42, 0, 255);"&gt;"MM/dd/yyyy"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;.parse&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(42, 0, 255);"&gt;"11/27/2008"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;))&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;} &lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;catch &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;ParseException e&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;) {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;          &lt;/span&gt;&lt;span style="color: rgb(63, 127, 95);"&gt;// TODO Auto-generated catch block&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;          &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;e.printStackTrace&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;()&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;    &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;}&lt;/span&gt;&lt;/code&gt; &lt;/td&gt; &lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;&lt;/pre&gt;DateFormatterHelperImpl.java&lt;/li&gt;&lt;li&gt;The test class: &lt;pre&gt;&lt;div class="java" align="left"&gt;&lt;br /&gt;&lt;table bg="" style="color: rgb(255, 255, 255);" border="0" cellpadding="3" cellspacing="0"&gt; &lt;tbody&gt;&lt;tr&gt; &lt;td align="left" valign="top" nowrap="nowrap"&gt;  &lt;code&gt;&lt;br /&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;package &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;tests;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;import static &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;org.junit.Assert.fail;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;import &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;java.text.ParseException;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;import &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;java.text.SimpleDateFormat;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;import &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;org.easymock.EasyMock;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;import &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;org.junit.Assert;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;import &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;org.junit.Before;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;import &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;org.junit.Test;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;import &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;sample.DateFormatter;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;import &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;sample.DateFormatterHelper;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;public class &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;DateFormatterTests &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;  &lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;private &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;DateFormatter formatter = &lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;null&lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;DateFormatterHelper helper = &lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;null&lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;  &lt;/span&gt;&lt;span style="color: rgb(100, 100, 100);"&gt;@Before&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;  &lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;public &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;void &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;setUp&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;() &lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;throws &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Exception &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;helper = EasyMock.createMock&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;DateFormatterHelper.&lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;class&lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;formatter = &lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;new &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;DateFormatter&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;()&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;formatter.setHelper&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;helper&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;  &lt;/span&gt;&lt;span style="color: rgb(100, 100, 100);"&gt;@Test&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;  &lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;public &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;void &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;testConvertToStandardFormat&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;() {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;    &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;String formatted = &lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;null&lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;    &lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;try &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;      &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;EasyMock.expect&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;helper.getDateFormat&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;(&lt;/span&gt;&lt;span style="color: rgb(42, 0, 255); font-weight: bold;"&gt;"MM-dd-yyyy"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;))&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;.andReturn&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;(&lt;/span&gt;&lt;span style="color: rgb(127, 0, 85); font-weight: bold;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;SimpleDateFormat&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;(&lt;/span&gt;&lt;span style="color: rgb(42, 0, 255); font-weight: bold;"&gt;"MM-dd-yyyy"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;))&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255); font-weight: bold;"&gt;      &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;EasyMock.replay&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;helper&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;)&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255); font-weight: bold;"&gt;      &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;formatted = formatter.convertToStandardFormat&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(42, 0, 255);"&gt;"11-27-2008"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(42, 0, 255);"&gt;"MM-dd-yyyy"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;} &lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;catch &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;ParseException e&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;) {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;      &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;e.printStackTrace&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;()&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;      &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;fail&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(42, 0, 255);"&gt;"Exception"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Assert.assertEquals&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;formatted, &lt;/span&gt;&lt;span style="color: rgb(42, 0, 255);"&gt;"11/27/2008"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;}&lt;/span&gt;&lt;/code&gt; &lt;/td&gt; &lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;&lt;/pre&gt;DateFormatterTests.java&lt;br /&gt;Note that in the EasyMock.expect method line, we use the "andReturn" method. This is to define the expected behavior when the method under test is invoked by the testcase. As can be expected, the replay method simply replays the pre-defined behavior when the actual call is made on the mock object. &lt;/li&gt;&lt;/ul&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/18313336-8303697830905533222?l=java-x.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://java-x.blogspot.com/feeds/8303697830905533222/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://java-x.blogspot.com/2009/07/easymock-for-unit-tests.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/18313336/posts/default/8303697830905533222'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/18313336/posts/default/8303697830905533222'/><link rel='alternate' type='text/html' href='http://java-x.blogspot.com/2009/07/easymock-for-unit-tests.html' title='EasyMock for Unit Tests'/><author><name>Abhilash Vuyyuru</name><uri>https://profiles.google.com/107328798079149921932</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh6.googleusercontent.com/-NxAgT10-nrk/AAAAAAAAAAI/AAAAAAAAB0o/Gz3VMjlEXb0/s512-c/photo.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-18313336.post-8577289001250365871</id><published>2009-03-24T09:41:00.009-04:00</published><updated>2009-03-24T11:46:09.213-04:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='tips'/><category scheme='http://www.blogger.com/atom/ns#' term='web services'/><category scheme='http://www.blogger.com/atom/ns#' term='security'/><title type='text'>Invoking Web Services through a proxy using JAX-RPC and JAX-WS</title><content type='html'>Not very often, we face the possibility of invoking Web Services provided by external entities that are outside our network. Some companies solve this by configuring their network to allow some application servers to bypass proxy servers. Whatever be the case, when in development, developers have to to be able to invoke web services through proxies. This post will be describe how to&lt;ul&gt;&lt;li&gt;Invoking Web Services through a proxy using JAX-RPC&lt;/li&gt;&lt;li&gt;Invoking Web Services through a proxy using  JAX-WS&lt;/li&gt;&lt;/ul&gt; The solutions provided here are specific to Oracle Weblogic Server 10.3. I would suggest that you try the solutions provided on "&lt;a style="font-weight: bold;" href="http://java.sun.com/javase/6/docs/technotes/guides/net/proxies.html"&gt;Java Networking and Proxies&lt;/a&gt;", and only if they don't work (which happened to me), try the following.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;&lt;a href="http://java-x.blogspot.com/2009/03/invoking-web-services-through-proxy.html"&gt;There's More ...&lt;/a&gt;&lt;/span&gt;&lt;span class="fullpost"&gt;&lt;div class="post-inner-heading"&gt;Invoking Web Services through a proxy using JAX-RPC&lt;/div&gt;&lt;br /&gt;&lt;ol&gt;&lt;li&gt;Generate the Web Service Proxy classes using the following ant task.&lt;pre&gt;&lt;span style="font-weight: bold;"&gt;&amp;lt;taskdef name="clientgen" classname="weblogic.wsee.tools.anttasks.ClientGenTask" /&amp;gt;&lt;/span&gt;&lt;br /&gt;&amp;lt;target name="build-client"&amp;gt;&lt;br /&gt;&amp;lt;clientgen&lt;br /&gt;wsdl="[path_to_WSDL]"&lt;br /&gt;destDir="./src"&lt;br /&gt;packageName="com.my.client"&lt;br /&gt;&lt;span style="font-weight: bold;"&gt; type="JAXRPC"/&amp;gt;&lt;/span&gt;&lt;br /&gt;&amp;lt;/target&amp;gt;&lt;/pre&gt;&lt;/li&gt;&lt;li&gt;When the client classes are generated the type as JAXRPC, the generated classes will consist of Service, ServiceImpl, PortType and ProtTypeImpl etc files. To invoke the service you have to create the PortType from the Service. When working behind a proxy, you have to instantiate the ServiceImpl by using a constructor that takes in a weblogic.wsee.connection.transport.http.HttpTransportInfo.HttpTransportInfo object as a parameter as shown below.&lt;pre&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;  &lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;private &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;HttpTransportInfo getHttpInfo&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;() {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Proxy proxy = &lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;new &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Proxy&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Proxy.Type.HTTP, &lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;new &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;InetSocketAddress&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(42, 0, 255);"&gt;"proxyServer"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(153, 0, 0);"&gt;9090&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;))&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;HttpTransportInfo httpInfo = &lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;new &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;HttpTransportInfo&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;()&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;httpInfo.setProxy&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;proxy&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;httpInfo.setProxyUsername&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(42, 0, 255);"&gt;"proxyuser"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;.getBytes&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;())&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;httpInfo.setProxyPassword&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(42, 0, 255);"&gt;"proxypassword"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;.getBytes&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;())&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;    &lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;return &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;httpInfo;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;}&lt;/span&gt;&lt;/pre&gt;When using SSL, you can use the weblogic.wsee.connection.transport.https.HttpsTransportInfo class which can be created in the same way as above.&lt;/li&gt;&lt;/ol&gt;&lt;div class="post-inner-heading"&gt;Invoking Web Services through a proxy using JAX-WS&lt;/div&gt;&lt;ol&gt;&lt;li&gt;When using JAX-WS, you have to change the clientgen task's "type" attribute to JAXWS, as shown below&lt;pre&gt;&amp;lt;taskdef name="clientgen" classname="weblogic.wsee.tools.anttasks.ClientGenTask" /&amp;gt;&lt;br /&gt;&amp;lt;target name="build-client"&amp;gt;&lt;br /&gt;&amp;lt;clientgen&lt;br /&gt;wsdl="[path_to_WSDL]"&lt;br /&gt;destDir="./src"&lt;br /&gt;packageName="com.my.client"&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;type="JAXWS"/&amp;gt;&lt;/span&gt;&lt;br /&gt;&amp;lt;/target&amp;gt;&lt;/pre&gt;&lt;span style="font-weight: bold;"&gt;Make sure that the "path_to_WSDL" is to a local copy of the WSDL, as the properties which we set in the following steps are after the initialization of the service.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;Running clientgen with JAXWS will create classes of the type *Service and *ServiceSoap&lt;/li&gt;&lt;li&gt;Setting up the client for proxy server involves setting a couple of request paramters: Username and password as shown below.&lt;pre&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;MyService service = &lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;null&lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;service = &lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;new &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;MyService&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;()&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;MyServiceSoap port = service.getMyServiceSoap&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;()&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;BindingProvider bp = &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;BindingProvider&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;) &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;port;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Binding binding = bp.getBinding&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;()&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;    &lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;Map&amp;lt;String, Object&amp;gt; ctx = bp.getRequestContext&lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;()&lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold; color: rgb(255, 255, 255);"&gt;    &lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;ctx.put&lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;BindingProvider.USERNAME_PROPERTY, &lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(42, 0, 255);"&gt;"proxyuser"&lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;)&lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold; color: rgb(255, 255, 255);"&gt;    &lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;ctx.put&lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;BindingProvider.PASSWORD_PROPERTY, &lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(42, 0, 255);"&gt;"proxypassword"&lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;)&lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;&lt;/span&gt;&lt;/pre&gt;Note that we don't specify the Proxy Server here. JAX-WS sends authentication information for the proxy in request headers.&lt;/li&gt;&lt;/ol&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/18313336-8577289001250365871?l=java-x.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://java-x.blogspot.com/feeds/8577289001250365871/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://java-x.blogspot.com/2009/03/invoking-web-services-through-proxy.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/18313336/posts/default/8577289001250365871'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/18313336/posts/default/8577289001250365871'/><link rel='alternate' type='text/html' href='http://java-x.blogspot.com/2009/03/invoking-web-services-through-proxy.html' title='Invoking Web Services through a proxy using JAX-RPC and JAX-WS'/><author><name>Abhilash Vuyyuru</name><uri>https://profiles.google.com/107328798079149921932</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh6.googleusercontent.com/-NxAgT10-nrk/AAAAAAAAAAI/AAAAAAAAB0o/Gz3VMjlEXb0/s512-c/photo.jpg'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-18313336.post-3698064225821901068</id><published>2008-10-21T09:17:00.005-04:00</published><updated>2009-07-10T18:12:31.148-04:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='web services'/><category scheme='http://www.blogger.com/atom/ns#' term='example/sample code'/><title type='text'>RESTful Web Services</title><content type='html'>Representational State Transfer(REST), a software architecture style used in developing stateless web services. While this style may be used to describe any distributed framework that uses a simple protocol for data transmission and no other additional data transfer rules, the most common use of REST is on on the Web with HTTP being the only protocol used here. In REST each service (called "resource" in REST) is viewed as resource identified by a URL, and the only operations allowed are the HTTP - GET (Read), PUT (Update), POST(Create) and DELETE (Delete). You can find this style similar in SQL, thinking of a resource as equivalent to a Table. The main features and constraints of REST architectural style are: &lt;ul&gt;&lt;li&gt;&lt;b&gt;Client-Server&lt;/b&gt;: A clear separation concerns is the reason behind this constraint. Separating concerns between the Client and Server helps improve portability in the Client and Scalability of the server components.&lt;/li&gt;&lt;li&gt;&lt;span style="font-weight: bold;"&gt;Stateless&lt;/span&gt;: All REST resources are required to be stateless. The idea is that the application would be resilient enough to work between server restarts. However, it is not uncommon to see some RESTful web services save states between requests.&lt;/li&gt;&lt;li&gt;&lt;span style="font-weight: bold;"&gt;Caching&lt;/span&gt;: Caching is allowed, however it is required that "response to a request be implicitly or explicitly labeled as cacheable or non-cacheable"&lt;/li&gt;&lt;li&gt;As there is &lt;span style="font-weight: bold;"&gt;no interface definition&lt;/span&gt; (like in SOAP), it becomes mandatory for a Client and Server to have a mutual understanding of the messages being transmitted between them.&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;Given that every resource in a RESTful service is represented by a URL, it is easy to write a client for such a web service. The following is the code for a simple Web Service client for the flickr web services interface.&lt;br /&gt;&lt;a href="http://java-x.blogspot.com/2008/10/restful-web-services.html"&gt;&lt;span style="font-weight:bold;"&gt;There's More&lt;/span&gt;&lt;/a&gt;&lt;br /&gt;&lt;span class="fullpost"&gt;&lt;pre&gt;&lt;div class="java" align="left"&gt;&lt;table bg="" style="color: rgb(255, 255, 255);" border="0" cellpadding="3" cellspacing="0"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;!-- start source code --&gt;&lt;td valign="top" align="left" nowrap="nowrap"&gt;&lt;code&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;package &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;main;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;import &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;java.net.HttpURLConnection;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;import &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;java.net.InetSocketAddress;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;import &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;java.net.Proxy;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;import &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;java.net.SocketAddress;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;import &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;java.net.URL;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;import &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;javax.xml.parsers.DocumentBuilder;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;import &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;javax.xml.parsers.DocumentBuilderFactory;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;import &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;org.w3c.dom.Document;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;public class &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;FlickrClient &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;  &lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;public static &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;void &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;main&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;String&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;[] &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;args&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;) {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;String flickrURL = &lt;/span&gt;&lt;span style="color: rgb(42, 0, 255);"&gt;"http://api.flickr.com/services/rest/?method=flickr.test.echo&amp;amp;name=value&amp;amp;api_key=[yourflickrkey]";&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;    &lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;try &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;      &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;SocketAddress addr = &lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;new &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;InetSocketAddress&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(42, 0, 255);"&gt;"[proxy]"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(153, 0, 0);"&gt;9090&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;      &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Proxy proxy = &lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;new &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Proxy&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Proxy.Type.HTTP, addr&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;      &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;URL u = &lt;/span&gt;&lt;span style="color: rgb(127, 0, 85); font-weight: bold;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;URL&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;(&lt;/span&gt;&lt;span style="color: rgb(42, 0, 255); font-weight: bold;"&gt;"http://api.flickr.com/services/rest/?method=flickr.test.echo&amp;amp;name=value&amp;amp;api_key=[yourflickrkey]"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;)&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255); font-weight: bold;"&gt;      &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;HttpURLConnection uc = &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;HttpURLConnection&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;) &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;u.openConnection&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;proxy&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;      &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;uc.setRequestProperty&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(42, 0, 255);"&gt;"Accept"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(42, 0, 255);"&gt;"*/*"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;      &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;uc.setRequestProperty&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(42, 0, 255);"&gt;"Accept-Charset"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(42, 0, 255);"&gt;"ISO-8859-1,utf-8;q=0.7,*;q=0.7"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;      &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;uc.setRequestProperty&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(42, 0, 255);"&gt;"Accept-Language"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(42, 0, 255);"&gt;"en-us,en;q=0.5"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;      &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;uc.setRequestProperty&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(42, 0, 255);"&gt;"Keep-Alive"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(42, 0, 255);"&gt;"300"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;      &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;uc.setRequestProperty&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(42, 0, 255);"&gt;"ucection"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(42, 0, 255);"&gt;"keep-alive"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;      &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;String proxyUser = &lt;/span&gt;&lt;span style="color: rgb(42, 0, 255);"&gt;"[netUserId]"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;      &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;String proxyPassword = &lt;/span&gt;&lt;span style="color: rgb(42, 0, 255);"&gt;"[netPassword]"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;      &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;uc.setRequestProperty&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(42, 0, 255);"&gt;"Proxy-Authorization"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(42, 0, 255);"&gt;"NTLM " &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;+ &lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;new &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;sun.misc.BASE64Encoder&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;()&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;.encode&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;((&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;proxyUser + &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(42, 0, 255);"&gt;":" &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;+ proxyPassword&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;.getBytes&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;()))&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;      &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;()&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;.newDocumentBuilder&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;()&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;      &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Document doc = docBuilder.parse&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;uc.getInputStream&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;())&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;      &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;System.out.println&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;doc.getDocumentElement&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;()&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;.getTagName&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;())&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;      &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;System.out.println&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;()&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;} &lt;/span&gt;&lt;span style="color: rgb(127, 0, 85);"&gt;&lt;b&gt;catch &lt;/b&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Exception e&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;) {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;      &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;e.printStackTrace&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;()&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;}&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;&lt;/td&gt;&lt;br /&gt;&lt;/tr&gt;&lt;br /&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;/div&gt;&lt;/pre&gt;&lt;ul&gt;&lt;li&gt;Note that the request URL has a method parameter. &lt;blockquote&gt;http://api.flickr.com/services/rest/?method=flickr.test.echo&lt;/blockquote&gt;This is is not strictly adhering to the principles of REST, however it is not uncommon. Ideally the URL should be &lt;blockquote&gt;http://api.flickr.com/services/rest/flickrtestecho&lt;/blockquote&gt;You would make a GET request on this resource.&lt;/li&gt;&lt;/ul&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/18313336-3698064225821901068?l=java-x.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://java-x.blogspot.com/feeds/3698064225821901068/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://java-x.blogspot.com/2008/10/restful-web-services.html#comment-form' title='6 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/18313336/posts/default/3698064225821901068'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/18313336/posts/default/3698064225821901068'/><link rel='alternate' type='text/html' href='http://java-x.blogspot.com/2008/10/restful-web-services.html' title='RESTful Web Services'/><author><name>Abhilash Vuyyuru</name><uri>https://profiles.google.com/107328798079149921932</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh6.googleusercontent.com/-NxAgT10-nrk/AAAAAAAAAAI/AAAAAAAAB0o/Gz3VMjlEXb0/s512-c/photo.jpg'/></author><thr:total>6</thr:total></entry><entry><id>tag:blogger.com,1999:blog-18313336.post-8156737484591843334</id><published>2008-10-10T22:29:00.003-04:00</published><updated>2009-06-27T19:41:37.378-04:00</updated><title type='text'>Links</title><content type='html'>&lt;span class="fullpost"&gt;&lt;ul&gt;&lt;li&gt;&lt;a href="http://www.ibm.com/developerworks/"&gt;Developerworks&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.artima.com/lejava"&gt;Artima Java&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/18313336-8156737484591843334?l=java-x.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://java-x.blogspot.com/feeds/8156737484591843334/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://java-x.blogspot.com/2008/10/links.html#comment-form' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/18313336/posts/default/8156737484591843334'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/18313336/posts/default/8156737484591843334'/><link rel='alternate' type='text/html' href='http://java-x.blogspot.com/2008/10/links.html' title='Links'/><author><name>Abhilash Vuyyuru</name><uri>https://profiles.google.com/107328798079149921932</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh6.googleusercontent.com/-NxAgT10-nrk/AAAAAAAAAAI/AAAAAAAAB0o/Gz3VMjlEXb0/s512-c/photo.jpg'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-18313336.post-7890304459126866073</id><published>2008-07-29T18:47:00.008-04:00</published><updated>2008-07-29T20:24:18.710-04:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='example/sample code'/><title type='text'>Binary Search Tree in Java with Generics</title><content type='html'>This post is more about generics than the actual implementation of the binary search tree. I tried to implement some of the suggestions in the developerworks articles titled "Java theory and practice: Going wild with generics". The Binary Search Tree of this example uses the first two suggestions. The rest are equally helpful though.&lt;br /&gt;&lt;ol&gt;&lt;li&gt;A generic factory method that allows you to avoid redundantly specifying type parameters&lt;pre&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;public static&lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;T &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;extends &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;Comparable&amp;lt;? &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;super &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;T&amp;gt;&amp;gt; BinarySearchTree&amp;lt;T&amp;gt; createTestTree&lt;/span&gt;&lt;span style="color:#000000;"&gt;() &lt;/span&gt;&lt;/pre&gt;Can be used as shown below.&lt;pre&gt;&lt;span style="color:#000000;"&gt;BinarySearchTree&amp;lt;Test&amp;gt; tree = BinarySearchTree.createTestTree&lt;/span&gt;&lt;span style="color:#000000;"&gt;()&lt;/span&gt;&lt;span style="color:#000000;"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li&gt;By using bounded wildcards, we can now construct a tree with both the super and sub-class type objects.&lt;pre&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;public class &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;BinarySearchTree&amp;lt;T &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;extends &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;Comparable&amp;lt;? &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;super &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;T&amp;gt;&amp;gt; &lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li&gt;The get-put principle&lt;blockquote&gt;Use an extends wildcard when you only get values out of a structure, use a super wildcard when you only put values into a structure, and don't use a wildcard when you do both.&lt;/blockquote&gt;&lt;/li&gt;&lt;li&gt;Wildcard capture: Use a generic method as a capture helper to workaround  compiler's limitations dealing with wildcards. (refer to the article "Java theory and practice: Going wild with generics, Part 1")&lt;/li&gt;&lt;/ol&gt;Following is the complete &lt;a href="http://java-x.blogspot.com/2008/07/binary-search-tree-in-java-with.html"&gt;source code&lt;/a&gt;&lt;span class="fullpost"&gt;&lt;pre&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;package &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;trees;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;import &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;java.util.NoSuchElementException;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;public class &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;BinarySearchTree&amp;lt;T &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;extends &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;Comparable&amp;lt;? &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;super &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;T&amp;gt;&amp;gt; &lt;/span&gt;&lt;span style="color:#000000;"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;  &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;private &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;Entry&amp;lt;T&amp;gt; root;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;  &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;public &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;BinarySearchTree&lt;/span&gt;&lt;span style="color:#000000;"&gt;() {&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;    &lt;/span&gt;&lt;span style="color:#000000;"&gt;root = &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;null&lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;  &lt;/span&gt;&lt;span style="color:#000000;"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;  &lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;  &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;public static&lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;T &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;extends &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;Comparable&amp;lt;? &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;super &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;T&amp;gt;&amp;gt; BinarySearchTree&amp;lt;T&amp;gt; createTestTree&lt;/span&gt;&lt;span style="color:#000000;"&gt;() {&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;    &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;return new &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;BinarySearchTree&amp;lt;T&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;()&lt;/span&gt;&lt;span style="color:#000000;"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;  &lt;/span&gt;&lt;span style="color:#000000;"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;  &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;public &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;void &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;insert&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#000000;"&gt;T value&lt;/span&gt;&lt;span style="color:#000000;"&gt;) {&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;    &lt;/span&gt;&lt;span style="color:#000000;"&gt;root = insert&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#000000;"&gt;value, root&lt;/span&gt;&lt;span style="color:#000000;"&gt;)&lt;/span&gt;&lt;span style="color:#000000;"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;  &lt;/span&gt;&lt;span style="color:#000000;"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;  &lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;  &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;public &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;void &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;remove&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#000000;"&gt;T value&lt;/span&gt;&lt;span style="color:#000000;"&gt;) {&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;    &lt;/span&gt;&lt;span style="color:#000000;"&gt;root = remove&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#000000;"&gt;value, root&lt;/span&gt;&lt;span style="color:#000000;"&gt;)&lt;/span&gt;&lt;span style="color:#000000;"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;  &lt;/span&gt;&lt;span style="color:#000000;"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;  &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;public &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;boolean &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;contains&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#000000;"&gt;T value&lt;/span&gt;&lt;span style="color:#000000;"&gt;) {&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;    &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;return &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;valueOf&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#000000;"&gt;find&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#000000;"&gt;value, root&lt;/span&gt;&lt;span style="color:#000000;"&gt;)) &lt;/span&gt;&lt;span style="color:#000000;"&gt;!= &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;null&lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;  &lt;/span&gt;&lt;span style="color:#000000;"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;  &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;private &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;T valueOf&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#000000;"&gt;Entry&amp;lt;T&amp;gt; entry&lt;/span&gt;&lt;span style="color:#000000;"&gt;) {&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;    &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;return &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;entry == &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;null &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;? &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;null &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;: entry.element;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;  &lt;/span&gt;&lt;span style="color:#000000;"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;  &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;private &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;Entry&amp;lt;T&amp;gt; insert&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#000000;"&gt;T value, Entry&amp;lt;T&amp;gt; entry&lt;/span&gt;&lt;span style="color:#000000;"&gt;) {&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;    &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;if &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#000000;"&gt;entry == &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;null&lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;      &lt;/span&gt;&lt;span style="color:#000000;"&gt;entry = &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;new &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;Entry&amp;lt;T&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#000000;"&gt;value&lt;/span&gt;&lt;span style="color:#000000;"&gt;)&lt;/span&gt;&lt;span style="color:#000000;"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;    &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;else if &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#000000;"&gt;value.compareTo&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#000000;"&gt;entry.element&lt;/span&gt;&lt;span style="color:#000000;"&gt;) &lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt; &lt;/span&gt;&lt;span style="color:#990000;"&gt;0&lt;/span&gt;&lt;span style="color:#000000;"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;      &lt;/span&gt;&lt;span style="color:#000000;"&gt;entry.left = insert&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#000000;"&gt;value, entry.left&lt;/span&gt;&lt;span style="color:#000000;"&gt;)&lt;/span&gt;&lt;span style="color:#000000;"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;    &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;else if &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#000000;"&gt;value.compareTo&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#000000;"&gt;entry.element&lt;/span&gt;&lt;span style="color:#000000;"&gt;) &lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt; &lt;/span&gt;&lt;span style="color:#990000;"&gt;0&lt;/span&gt;&lt;span style="color:#000000;"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;      &lt;/span&gt;&lt;span style="color:#000000;"&gt;entry.right = insert&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#000000;"&gt;value, entry.right&lt;/span&gt;&lt;span style="color:#000000;"&gt;)&lt;/span&gt;&lt;span style="color:#000000;"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;    &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;else&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;      &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;throw new &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;RuntimeException&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#2a00ff;"&gt;"Duplicate Entry : " &lt;/span&gt;&lt;span style="color:#000000;"&gt;+ value.toString&lt;/span&gt;&lt;span style="color:#000000;"&gt;())&lt;/span&gt;&lt;span style="color:#000000;"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;    &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;return &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;entry;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;  &lt;/span&gt;&lt;span style="color:#000000;"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;  &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;private &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;Entry&amp;lt;T&amp;gt; remove&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#000000;"&gt;T value, Entry&amp;lt;T&amp;gt; entry&lt;/span&gt;&lt;span style="color:#000000;"&gt;) {&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;    &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;if &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#000000;"&gt;entry == &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;null&lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;      &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;throw new &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;NoSuchElementException&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#2a00ff;"&gt;"Entry not found : " &lt;/span&gt;&lt;span style="color:#000000;"&gt;+ value.toString&lt;/span&gt;&lt;span style="color:#000000;"&gt;())&lt;/span&gt;&lt;span style="color:#000000;"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;    &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;if &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#000000;"&gt;value.compareTo&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#000000;"&gt;entry.element&lt;/span&gt;&lt;span style="color:#000000;"&gt;) &lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt; &lt;/span&gt;&lt;span style="color:#990000;"&gt;0&lt;/span&gt;&lt;span style="color:#000000;"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;      &lt;/span&gt;&lt;span style="color:#000000;"&gt;entry.left = remove&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#000000;"&gt;value, entry.left&lt;/span&gt;&lt;span style="color:#000000;"&gt;)&lt;/span&gt;&lt;span style="color:#000000;"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;    &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;else if &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#000000;"&gt;value.compareTo&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#000000;"&gt;entry.element&lt;/span&gt;&lt;span style="color:#000000;"&gt;) &lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt; &lt;/span&gt;&lt;span style="color:#990000;"&gt;0&lt;/span&gt;&lt;span style="color:#000000;"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;      &lt;/span&gt;&lt;span style="color:#000000;"&gt;entry.right = remove&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#000000;"&gt;value, entry.right&lt;/span&gt;&lt;span style="color:#000000;"&gt;)&lt;/span&gt;&lt;span style="color:#000000;"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;    &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;else &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;      &lt;/span&gt;&lt;span style="color:#3f7f5f;"&gt;// Entry found.&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;      &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;if &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#000000;"&gt;entry.left != &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;null &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;amp;&amp;amp; entry.right != &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;null&lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;) {&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;        &lt;/span&gt;&lt;span style="color:#3f7f5f;"&gt;// Replace with in-order successor (the left-most child of the right subtree)&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;        &lt;/span&gt;&lt;span style="color:#000000;"&gt;entry.element = findMin&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#000000;"&gt;entry.right&lt;/span&gt;&lt;span style="color:#000000;"&gt;)&lt;/span&gt;&lt;span style="color:#000000;"&gt;.element;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;        &lt;/span&gt;&lt;span style="color:#000000;"&gt;entry.right = removeInorderSuccessor&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#000000;"&gt;entry.right&lt;/span&gt;&lt;span style="color:#000000;"&gt;)&lt;/span&gt;&lt;span style="color:#000000;"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;        &lt;/span&gt;&lt;span style="color:#3f7f5f;"&gt;// Replace with in-order predecessor (the right-most child of the left subtree)&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;        &lt;/span&gt;&lt;span style="color:#3f7f5f;"&gt;// entry.element = findMax(entry.left).element;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;        &lt;/span&gt;&lt;span style="color:#3f7f5f;"&gt;// entry.left = removeInorderPredecessor(entry.left);&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;      &lt;/span&gt;&lt;span style="color:#000000;"&gt;} &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;else&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;        &lt;/span&gt;&lt;span style="color:#000000;"&gt;entry = &lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#000000;"&gt;entry.left != &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;null&lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;) &lt;/span&gt;&lt;span style="color:#000000;"&gt;? entry.left : entry.right;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;    &lt;/span&gt;&lt;span style="color:#000000;"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;    &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;return &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;entry;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;  &lt;/span&gt;&lt;span style="color:#000000;"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;  &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;private &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;Entry&amp;lt;T&amp;gt; removeInorderSuccessor&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#000000;"&gt;Entry&amp;lt;T&amp;gt; entry&lt;/span&gt;&lt;span style="color:#000000;"&gt;) {&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;    &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;if &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#000000;"&gt;entry == &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;null&lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;      &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;throw new &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;NoSuchElementException&lt;/span&gt;&lt;span style="color:#000000;"&gt;()&lt;/span&gt;&lt;span style="color:#000000;"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;    &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;else if &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#000000;"&gt;entry.left != &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;null&lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;) {&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;      &lt;/span&gt;&lt;span style="color:#000000;"&gt;entry.left = removeInorderSuccessor&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#000000;"&gt;entry.left&lt;/span&gt;&lt;span style="color:#000000;"&gt;)&lt;/span&gt;&lt;span style="color:#000000;"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;      &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;return &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;entry;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;    &lt;/span&gt;&lt;span style="color:#000000;"&gt;} &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;else&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;      &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;return &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;entry.right;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;  &lt;/span&gt;&lt;span style="color:#000000;"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;  &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;private &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;Entry&amp;lt;T&amp;gt; removeInorderPredecessor&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#000000;"&gt;Entry&amp;lt;T&amp;gt; entry&lt;/span&gt;&lt;span style="color:#000000;"&gt;) {&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;    &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;if &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#000000;"&gt;entry == &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;null&lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;      &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;throw new &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;NoSuchElementException&lt;/span&gt;&lt;span style="color:#000000;"&gt;()&lt;/span&gt;&lt;span style="color:#000000;"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;    &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;else if &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#000000;"&gt;entry.right != &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;null&lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;) {&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;      &lt;/span&gt;&lt;span style="color:#000000;"&gt;entry.right = removeInorderPredecessor&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#000000;"&gt;entry.right&lt;/span&gt;&lt;span style="color:#000000;"&gt;)&lt;/span&gt;&lt;span style="color:#000000;"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;      &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;return &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;entry;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;    &lt;/span&gt;&lt;span style="color:#000000;"&gt;} &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;else&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;      &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;return &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;entry.left;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;  &lt;/span&gt;&lt;span style="color:#000000;"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;  &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;private &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;Entry&amp;lt;T&amp;gt; findMin&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#000000;"&gt;Entry&amp;lt;T&amp;gt; entry&lt;/span&gt;&lt;span style="color:#000000;"&gt;) {&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;    &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;if &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#000000;"&gt;entry != &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;null&lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;      &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;while &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#000000;"&gt;entry.left != &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;null&lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;        &lt;/span&gt;&lt;span style="color:#000000;"&gt;entry = entry.left;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;    &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;return &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;entry;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;  &lt;/span&gt;&lt;span style="color:#000000;"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;  &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;private &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;Entry&amp;lt;T&amp;gt; findMax&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#000000;"&gt;Entry&amp;lt;T&amp;gt; entry&lt;/span&gt;&lt;span style="color:#000000;"&gt;) {&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;    &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;if &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#000000;"&gt;entry != &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;null&lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;      &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;while &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#000000;"&gt;entry.right != &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;null&lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;        &lt;/span&gt;&lt;span style="color:#000000;"&gt;entry = entry.right;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;    &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;return &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;entry;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;  &lt;/span&gt;&lt;span style="color:#000000;"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;  &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;private &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;Entry&amp;lt;T&amp;gt; find&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#000000;"&gt;T value, Entry&amp;lt;T&amp;gt; entry&lt;/span&gt;&lt;span style="color:#000000;"&gt;) {&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;    &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;while &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#000000;"&gt;entry != &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;null&lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;) {&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;      &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;if &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#000000;"&gt;value.compareTo&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#000000;"&gt;entry.element&lt;/span&gt;&lt;span style="color:#000000;"&gt;) &lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt; &lt;/span&gt;&lt;span style="color:#990000;"&gt;0&lt;/span&gt;&lt;span style="color:#000000;"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;        &lt;/span&gt;&lt;span style="color:#000000;"&gt;entry = entry.left;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;      &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;else if &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#000000;"&gt;value.compareTo&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#000000;"&gt;entry.element&lt;/span&gt;&lt;span style="color:#000000;"&gt;) &lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt; &lt;/span&gt;&lt;span style="color:#990000;"&gt;0&lt;/span&gt;&lt;span style="color:#000000;"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;        &lt;/span&gt;&lt;span style="color:#000000;"&gt;entry = entry.right;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;      &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;else&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;        &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;return &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;entry;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;    &lt;/span&gt;&lt;span style="color:#000000;"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;    &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;return null&lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;  &lt;/span&gt;&lt;span style="color:#000000;"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;  &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;private &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;void &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;printInOrder&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#000000;"&gt;Entry&amp;lt;T&amp;gt; entry&lt;/span&gt;&lt;span style="color:#000000;"&gt;) {&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;    &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;if &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#000000;"&gt;entry != &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;null&lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;) {&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;      &lt;/span&gt;&lt;span style="color:#000000;"&gt;printInOrder&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#000000;"&gt;entry.left&lt;/span&gt;&lt;span style="color:#000000;"&gt;)&lt;/span&gt;&lt;span style="color:#000000;"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;      &lt;/span&gt;&lt;span style="color:#000000;"&gt;System.out.println&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#000000;"&gt;entry.element&lt;/span&gt;&lt;span style="color:#000000;"&gt;)&lt;/span&gt;&lt;span style="color:#000000;"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;      &lt;/span&gt;&lt;span style="color:#000000;"&gt;printInOrder&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#000000;"&gt;entry.right&lt;/span&gt;&lt;span style="color:#000000;"&gt;)&lt;/span&gt;&lt;span style="color:#000000;"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;    &lt;/span&gt;&lt;span style="color:#000000;"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;  &lt;/span&gt;&lt;span style="color:#000000;"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;  &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;public &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;void &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;printInOrder&lt;/span&gt;&lt;span style="color:#000000;"&gt;() {&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;    &lt;/span&gt;&lt;span style="color:#000000;"&gt;printInOrder&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#000000;"&gt;root&lt;/span&gt;&lt;span style="color:#000000;"&gt;)&lt;/span&gt;&lt;span style="color:#000000;"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;  &lt;/span&gt;&lt;span style="color:#000000;"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;  &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;private static class &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;Entry&amp;lt;T &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;extends &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;Comparable&amp;lt;? &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;super &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;T&amp;gt;&amp;gt; &lt;/span&gt;&lt;span style="color:#000000;"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;    &lt;/span&gt;&lt;span style="color:#000000;"&gt;T element;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;    &lt;/span&gt;&lt;span style="color:#000000;"&gt;Entry&amp;lt;T&amp;gt; left;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;    &lt;/span&gt;&lt;span style="color:#000000;"&gt;Entry&amp;lt;T&amp;gt; right;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;    &lt;/span&gt;&lt;span style="color:#000000;"&gt;Entry&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#000000;"&gt;T theElement&lt;/span&gt;&lt;span style="color:#000000;"&gt;) {&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;      &lt;/span&gt;&lt;span style="color:#000000;"&gt;element = theElement;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;      &lt;/span&gt;&lt;span style="color:#000000;"&gt;left = right = &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;null&lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;    &lt;/span&gt;&lt;span style="color:#000000;"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;  &lt;/span&gt;&lt;span style="color:#000000;"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;  &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;private static class &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;Test &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;implements &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;Comparable&amp;lt;Test&amp;gt; &lt;/span&gt;&lt;span style="color:#000000;"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;    &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;private &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;String value;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;    &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;public &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;Test&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#000000;"&gt;String value&lt;/span&gt;&lt;span style="color:#000000;"&gt;) {&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;      &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;this&lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;.value = value;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;    &lt;/span&gt;&lt;span style="color:#000000;"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;    &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;public &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;String toString&lt;/span&gt;&lt;span style="color:#000000;"&gt;() {&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;      &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;return &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;value;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;    &lt;/span&gt;&lt;span style="color:#000000;"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;    &lt;/span&gt;&lt;span style="color:#646464;"&gt;@Override&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;    &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;public &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;int &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;compareTo&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#000000;"&gt;Test o&lt;/span&gt;&lt;span style="color:#000000;"&gt;) {&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;      &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;return this&lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;.value.compareTo&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#000000;"&gt;o.toString&lt;/span&gt;&lt;span style="color:#000000;"&gt;())&lt;/span&gt;&lt;span style="color:#000000;"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;    &lt;/span&gt;&lt;span style="color:#000000;"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;  &lt;/span&gt;&lt;span style="color:#000000;"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;  &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;private static class &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;Test1 &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;extends &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;Test &lt;/span&gt;&lt;span style="color:#000000;"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;    &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;public &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;Test1&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#000000;"&gt;String value&lt;/span&gt;&lt;span style="color:#000000;"&gt;) {&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;      &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;super&lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#000000;"&gt;value&lt;/span&gt;&lt;span style="color:#000000;"&gt;)&lt;/span&gt;&lt;span style="color:#000000;"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;    &lt;/span&gt;&lt;span style="color:#000000;"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;  &lt;/span&gt;&lt;span style="color:#000000;"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;  &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;public static &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;void &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;main&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#000000;"&gt;String&lt;/span&gt;&lt;span style="color:#000000;"&gt;[] &lt;/span&gt;&lt;span style="color:#000000;"&gt;args&lt;/span&gt;&lt;span style="color:#000000;"&gt;) {&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;    &lt;/span&gt;&lt;span style="color:#000000;"&gt;BinarySearchTree&amp;lt;Test&amp;gt; tree = BinarySearchTree.createTestTree&lt;/span&gt;&lt;span style="color:#000000;"&gt;()&lt;/span&gt;&lt;span style="color:#000000;"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;    &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;int &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;size = &lt;/span&gt;&lt;span style="color:#990000;"&gt;20&lt;/span&gt;&lt;span style="color:#000000;"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;    &lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;for &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;int &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;i = &lt;/span&gt;&lt;span style="color:#990000;"&gt;0&lt;/span&gt;&lt;span style="color:#000000;"&gt;; i &amp;lt;= size; i++&lt;/span&gt;&lt;span style="color:#000000;"&gt;) {&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;      &lt;/span&gt;&lt;span style="color:#000000;"&gt;tree.insert&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;new &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;Test1&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#000000;"&gt;String.valueOf&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#000000;"&gt;i&lt;/span&gt;&lt;span style="color:#000000;"&gt;)))&lt;/span&gt;&lt;span style="color:#000000;"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;    &lt;/span&gt;&lt;span style="color:#000000;"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;    &lt;/span&gt;&lt;span style="color:#000000;"&gt;tree.insert&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;new &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;Test1&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#2a00ff;"&gt;"100"&lt;/span&gt;&lt;span style="color:#000000;"&gt;))&lt;/span&gt;&lt;span style="color:#000000;"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;    &lt;/span&gt;&lt;span style="color:#000000;"&gt;tree.remove&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;new &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;Test1&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#2a00ff;"&gt;"10"&lt;/span&gt;&lt;span style="color:#000000;"&gt;))&lt;/span&gt;&lt;span style="color:#000000;"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;    &lt;/span&gt;&lt;span style="color:#000000;"&gt;tree.remove&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;new &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;Test1&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#000000;"&gt;String.valueOf&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#990000;"&gt;15&lt;/span&gt;&lt;span style="color:#000000;"&gt;)))&lt;/span&gt;&lt;span style="color:#000000;"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;    &lt;/span&gt;&lt;span style="color:#000000;"&gt;tree.remove&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;new &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;Test1&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#000000;"&gt;String.valueOf&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#990000;"&gt;20&lt;/span&gt;&lt;span style="color:#000000;"&gt;)))&lt;/span&gt;&lt;span style="color:#000000;"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;    &lt;/span&gt;&lt;span style="color:#000000;"&gt;tree.printInOrder&lt;/span&gt;&lt;span style="color:#000000;"&gt;()&lt;/span&gt;&lt;span style="color:#000000;"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;    &lt;/span&gt;&lt;span style="color:#000000;"&gt;System.out.println&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#2a00ff;"&gt;"Contains (10) : " &lt;/span&gt;&lt;span style="color:#000000;"&gt;+ tree.contains&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;new &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;Test1&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#2a00ff;"&gt;"10"&lt;/span&gt;&lt;span style="color:#000000;"&gt;)))&lt;/span&gt;&lt;span style="color:#000000;"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;    &lt;/span&gt;&lt;span style="color:#000000;"&gt;System.out.println&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#2a00ff;"&gt;"Contains (11) : " &lt;/span&gt;&lt;span style="color:#000000;"&gt;+ tree.contains&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;b&gt;new &lt;/b&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;Test1&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#000000;"&gt;String.valueOf&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#990000;"&gt;11&lt;/span&gt;&lt;span style="color:#000000;"&gt;))))&lt;/span&gt;&lt;span style="color:#000000;"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;  &lt;/span&gt;&lt;span style="color:#000000;"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#000000;"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;span style="font-weight: bold;font-size:130%;" &gt;References&lt;/span&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;&lt;a href="http://www.ibm.com/developerworks/java/library/j-jtp04298.html"&gt;Java theory and practice: Going wild with generics, Part 1&lt;/a&gt;&lt;br /&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.ibm.com/developerworks/java/library/j-jtp07018.html"&gt;Java theory and practice: Going wild with generics, Part 2&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.ibm.com/developerworks/java/library/j-jtp01255.html"&gt;Java theory and practice: Generics gotchas&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/18313336-7890304459126866073?l=java-x.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://java-x.blogspot.com/feeds/7890304459126866073/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://java-x.blogspot.com/2008/07/binary-search-tree-in-java-with.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/18313336/posts/default/7890304459126866073'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/18313336/posts/default/7890304459126866073'/><link rel='alternate' type='text/html' href='http://java-x.blogspot.com/2008/07/binary-search-tree-in-java-with.html' title='Binary Search Tree in Java with Generics'/><author><name>Abhilash Vuyyuru</name><uri>https://profiles.google.com/107328798079149921932</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh6.googleusercontent.com/-NxAgT10-nrk/AAAAAAAAAAI/AAAAAAAAB0o/Gz3VMjlEXb0/s512-c/photo.jpg'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-18313336.post-7206627241755231542</id><published>2008-07-28T15:23:00.004-04:00</published><updated>2008-07-28T15:27:22.118-04:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='example/sample code'/><title type='text'>Cricular Linked List in Java</title><content type='html'>Had to implement a circular linked list recently. Here's my take, suggestions are welcome. The list can be parameterized for any type which has a meaningful equals method defined. The main method shows a sample usage of the Circular linked list with the String type. Only add, remove and size method are implemented. &lt;br/&gt;&lt;br /&gt;&lt;a href="http://java-x.blogspot.com/2008/07/cricular-linked-list-in-java.html"&gt;There's more ...&lt;/a&gt; &lt;span class="fullpost"&gt;&lt;br /&gt;&lt;pre&gt;&lt;font color="#7f0055"&gt;&lt;b&gt;package&amp;nbsp;&lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;algo;&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&lt;/font&gt;&lt;br /&gt;&lt;font color="#7f0055"&gt;&lt;b&gt;import&amp;nbsp;&lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;java.util.NoSuchElementException;&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&lt;/font&gt;&lt;br /&gt;&lt;font color="#7f0055"&gt;&lt;b&gt;public&amp;nbsp;class&amp;nbsp;&lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;CircularLinkedList&amp;lt;E&amp;gt;&amp;nbsp;&lt;/font&gt;&lt;font color="#000000"&gt;{&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&lt;/font&gt;&lt;font color="#7f0055"&gt;&lt;b&gt;private&amp;nbsp;&lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;Entry&amp;lt;E&amp;gt;&amp;nbsp;head;&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&lt;/font&gt;&lt;font color="#3f7f5f"&gt;//&amp;nbsp;Last&amp;nbsp;element&amp;nbsp;of&amp;nbsp;the&amp;nbsp;list.&amp;nbsp;tail.next&amp;nbsp;=&amp;nbsp;head&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&lt;/font&gt;&lt;font color="#7f0055"&gt;&lt;b&gt;private&amp;nbsp;&lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;Entry&amp;lt;E&amp;gt;&amp;nbsp;tail;&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&lt;/font&gt;&lt;font color="#7f0055"&gt;&lt;b&gt;private&amp;nbsp;&lt;/b&gt;&lt;/font&gt;&lt;font color="#7f0055"&gt;&lt;b&gt;int&amp;nbsp;&lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;size&amp;nbsp;=&amp;nbsp;&lt;/font&gt;&lt;font color="#990000"&gt;0&lt;/font&gt;&lt;font color="#000000"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&lt;/font&gt;&lt;font color="#3f5fbf"&gt;/**&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#3f5fbf"&gt;*&amp;nbsp;Class&amp;nbsp;to&amp;nbsp;hold&amp;nbsp;the&amp;nbsp;individual&amp;nbsp;elements.&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#3f5fbf"&gt;*&amp;nbsp;&lt;/font&gt;&lt;font color="#7f9fbf"&gt;@param&amp;nbsp;&lt;/font&gt;&lt;font color="#7f7f9f"&gt;&amp;lt;E&amp;gt;&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#3f5fbf"&gt;*/&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&lt;/font&gt;&lt;font color="#7f0055"&gt;&lt;b&gt;private&amp;nbsp;static&amp;nbsp;class&amp;nbsp;&lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;Entry&amp;lt;E&amp;gt;&amp;nbsp;&lt;/font&gt;&lt;font color="#000000"&gt;{&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#000000"&gt;E&amp;nbsp;element;&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#000000"&gt;Entry&amp;lt;E&amp;gt;&amp;nbsp;next;&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#000000"&gt;Entry&lt;/font&gt;&lt;font color="#000000"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;E&amp;nbsp;element,&amp;nbsp;Entry&amp;lt;E&amp;gt;&amp;nbsp;next&lt;/font&gt;&lt;font color="#000000"&gt;)&amp;nbsp;{&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#7f0055"&gt;&lt;b&gt;this&lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;.element&amp;nbsp;=&amp;nbsp;element;&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#7f0055"&gt;&lt;b&gt;this&lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;.next&amp;nbsp;=&amp;nbsp;next;&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#000000"&gt;}&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#000000"&gt;Entry&lt;/font&gt;&lt;font color="#000000"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;E&amp;nbsp;element&lt;/font&gt;&lt;font color="#000000"&gt;)&amp;nbsp;{&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#7f0055"&gt;&lt;b&gt;this&lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;.element&amp;nbsp;=&amp;nbsp;element;&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#000000"&gt;}&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&lt;/font&gt;&lt;font color="#000000"&gt;}&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&lt;/font&gt;&lt;font color="#7f0055"&gt;&lt;b&gt;public&amp;nbsp;&lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;CircularLinkedList&lt;/font&gt;&lt;font color="#000000"&gt;()&amp;nbsp;{&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#000000"&gt;head&amp;nbsp;=&amp;nbsp;&lt;/font&gt;&lt;font color="#7f0055"&gt;&lt;b&gt;null&lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&lt;/font&gt;&lt;font color="#000000"&gt;}&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&lt;/font&gt;&lt;font color="#3f5fbf"&gt;/**&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#3f5fbf"&gt;*&amp;nbsp;Remove&amp;nbsp;obj&amp;nbsp;from&amp;nbsp;the&amp;nbsp;circular&amp;nbsp;linked&amp;nbsp;list&amp;nbsp;and&amp;nbsp;return&amp;nbsp;the&amp;nbsp;removed&amp;nbsp;object&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#3f5fbf"&gt;*&amp;nbsp;&lt;/font&gt;&lt;font color="#7f9fbf"&gt;@param&amp;nbsp;&lt;/font&gt;&lt;font color="#3f5fbf"&gt;obj&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#3f5fbf"&gt;*&amp;nbsp;&lt;/font&gt;&lt;font color="#7f9fbf"&gt;@return&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#3f5fbf"&gt;*/&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&lt;/font&gt;&lt;font color="#7f0055"&gt;&lt;b&gt;public&amp;nbsp;&lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;E&amp;nbsp;remove&lt;/font&gt;&lt;font color="#000000"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;E&amp;nbsp;obj&lt;/font&gt;&lt;font color="#000000"&gt;)&amp;nbsp;{&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#7f0055"&gt;&lt;b&gt;if&amp;nbsp;&lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;head&amp;nbsp;==&amp;nbsp;&lt;/font&gt;&lt;font color="#7f0055"&gt;&lt;b&gt;null&amp;nbsp;&lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;||&amp;nbsp;tail&amp;nbsp;==&amp;nbsp;&lt;/font&gt;&lt;font color="#7f0055"&gt;&lt;b&gt;null&lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;)&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#7f0055"&gt;&lt;b&gt;throw&amp;nbsp;new&amp;nbsp;&lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;NoSuchElementException&lt;/font&gt;&lt;font color="#000000"&gt;()&lt;/font&gt;&lt;font color="#000000"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#000000"&gt;Entry&amp;lt;E&amp;gt;&amp;nbsp;current&amp;nbsp;=&amp;nbsp;head,&amp;nbsp;temp&amp;nbsp;=&amp;nbsp;head,&amp;nbsp;found&amp;nbsp;=&amp;nbsp;&lt;/font&gt;&lt;font color="#7f0055"&gt;&lt;b&gt;null&lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#7f0055"&gt;&lt;b&gt;if&amp;nbsp;&lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;obj.equals&lt;/font&gt;&lt;font color="#000000"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;head.element&lt;/font&gt;&lt;font color="#000000"&gt;))&amp;nbsp;{&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#7f0055"&gt;&lt;b&gt;if&amp;nbsp;&lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;head.next&amp;nbsp;==&amp;nbsp;head&lt;/font&gt;&lt;font color="#000000"&gt;)&amp;nbsp;{&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#000000"&gt;found&amp;nbsp;=&amp;nbsp;head;&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#000000"&gt;head&amp;nbsp;=&amp;nbsp;&lt;/font&gt;&lt;font color="#7f0055"&gt;&lt;b&gt;null&lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#000000"&gt;tail&amp;nbsp;=&amp;nbsp;&lt;/font&gt;&lt;font color="#7f0055"&gt;&lt;b&gt;null&lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#000000"&gt;size--;&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#7f0055"&gt;&lt;b&gt;return&amp;nbsp;&lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;found.element;&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#000000"&gt;}&amp;nbsp;&lt;/font&gt;&lt;font color="#7f0055"&gt;&lt;b&gt;else&amp;nbsp;&lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;{&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#000000"&gt;found&amp;nbsp;=&amp;nbsp;head;&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#000000"&gt;temp&amp;nbsp;=&amp;nbsp;tail;&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#000000"&gt;}&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#000000"&gt;}&amp;nbsp;&lt;/font&gt;&lt;font color="#7f0055"&gt;&lt;b&gt;else&amp;nbsp;&lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;{&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#000000"&gt;current&amp;nbsp;=&amp;nbsp;head.next;&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#7f0055"&gt;&lt;b&gt;while&amp;nbsp;&lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;current&amp;nbsp;!=&amp;nbsp;head&lt;/font&gt;&lt;font color="#000000"&gt;)&amp;nbsp;{&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#7f0055"&gt;&lt;b&gt;if&amp;nbsp;&lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;current.element.equals&lt;/font&gt;&lt;font color="#000000"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;obj&lt;/font&gt;&lt;font color="#000000"&gt;))&amp;nbsp;{&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#000000"&gt;found&amp;nbsp;=&amp;nbsp;current;&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#7f0055"&gt;&lt;b&gt;break&lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#000000"&gt;}&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#000000"&gt;temp&amp;nbsp;=&amp;nbsp;current;&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#000000"&gt;current&amp;nbsp;=&amp;nbsp;current.next;&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#000000"&gt;}&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#000000"&gt;}&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#7f0055"&gt;&lt;b&gt;if&amp;nbsp;&lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;found&amp;nbsp;==&amp;nbsp;&lt;/font&gt;&lt;font color="#7f0055"&gt;&lt;b&gt;null&lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;)&amp;nbsp;{&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#7f0055"&gt;&lt;b&gt;throw&amp;nbsp;new&amp;nbsp;&lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;NoSuchElementException&lt;/font&gt;&lt;font color="#000000"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;obj.toString&lt;/font&gt;&lt;font color="#000000"&gt;())&lt;/font&gt;&lt;font color="#000000"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#000000"&gt;}&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#000000"&gt;E&amp;nbsp;result&amp;nbsp;=&amp;nbsp;found.element;&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#000000"&gt;temp.next&amp;nbsp;=&amp;nbsp;found.next;&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#000000"&gt;found.next&amp;nbsp;=&amp;nbsp;&lt;/font&gt;&lt;font color="#7f0055"&gt;&lt;b&gt;null&lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#000000"&gt;found.element&amp;nbsp;=&amp;nbsp;&lt;/font&gt;&lt;font color="#7f0055"&gt;&lt;b&gt;null&lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#000000"&gt;size--;&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#7f0055"&gt;&lt;b&gt;return&amp;nbsp;&lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;result;&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&lt;/font&gt;&lt;font color="#000000"&gt;}&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&lt;/font&gt;&lt;font color="#3f5fbf"&gt;/**&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#3f5fbf"&gt;*&amp;nbsp;Add&amp;nbsp;obj&amp;nbsp;to&amp;nbsp;the&amp;nbsp;circular&amp;nbsp;linked&amp;nbsp;list.&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#3f5fbf"&gt;*&amp;nbsp;&lt;/font&gt;&lt;font color="#7f9fbf"&gt;@param&amp;nbsp;&lt;/font&gt;&lt;font color="#3f5fbf"&gt;obj&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#3f5fbf"&gt;*/&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&lt;/font&gt;&lt;font color="#7f0055"&gt;&lt;b&gt;public&amp;nbsp;&lt;/b&gt;&lt;/font&gt;&lt;font color="#7f0055"&gt;&lt;b&gt;void&amp;nbsp;&lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;add&lt;/font&gt;&lt;font color="#000000"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;E&amp;nbsp;obj&lt;/font&gt;&lt;font color="#000000"&gt;)&amp;nbsp;{&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#000000"&gt;Entry&amp;nbsp;e&amp;nbsp;=&amp;nbsp;&lt;/font&gt;&lt;font color="#7f0055"&gt;&lt;b&gt;new&amp;nbsp;&lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;Entry&lt;/font&gt;&lt;font color="#000000"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;obj&lt;/font&gt;&lt;font color="#000000"&gt;)&lt;/font&gt;&lt;font color="#000000"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#7f0055"&gt;&lt;b&gt;if&amp;nbsp;&lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;head&amp;nbsp;==&amp;nbsp;&lt;/font&gt;&lt;font color="#7f0055"&gt;&lt;b&gt;null&lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;)&amp;nbsp;{&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#000000"&gt;size++;&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#000000"&gt;head&amp;nbsp;=&amp;nbsp;e;&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#000000"&gt;head.next&amp;nbsp;=&amp;nbsp;head;&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#000000"&gt;tail&amp;nbsp;=&amp;nbsp;head;&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#7f0055"&gt;&lt;b&gt;return&lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#000000"&gt;}&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#000000"&gt;size++;&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#000000"&gt;e.next&amp;nbsp;=&amp;nbsp;head;&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#000000"&gt;head&amp;nbsp;=&amp;nbsp;e;&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#000000"&gt;tail.next&amp;nbsp;=&amp;nbsp;head;&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&lt;/font&gt;&lt;font color="#000000"&gt;}&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&lt;/font&gt;&lt;font color="#3f5fbf"&gt;/**&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#3f5fbf"&gt;*&amp;nbsp;Size&amp;nbsp;of&amp;nbsp;the&amp;nbsp;list.&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#3f5fbf"&gt;*&amp;nbsp;&lt;/font&gt;&lt;font color="#7f9fbf"&gt;@return&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#3f5fbf"&gt;*/&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&lt;/font&gt;&lt;font color="#7f0055"&gt;&lt;b&gt;public&amp;nbsp;&lt;/b&gt;&lt;/font&gt;&lt;font color="#7f0055"&gt;&lt;b&gt;int&amp;nbsp;&lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;size&lt;/font&gt;&lt;font color="#000000"&gt;()&amp;nbsp;{&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#7f0055"&gt;&lt;b&gt;return&amp;nbsp;&lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;size;&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&lt;/font&gt;&lt;font color="#000000"&gt;}&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&lt;/font&gt;&lt;font color="#7f0055"&gt;&lt;b&gt;public&amp;nbsp;static&amp;nbsp;&lt;/b&gt;&lt;/font&gt;&lt;font color="#7f0055"&gt;&lt;b&gt;void&amp;nbsp;&lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;main&lt;/font&gt;&lt;font color="#000000"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;String&lt;/font&gt;&lt;font color="#000000"&gt;[]&amp;nbsp;&lt;/font&gt;&lt;font color="#000000"&gt;args&lt;/font&gt;&lt;font color="#000000"&gt;)&amp;nbsp;{&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#000000"&gt;CircularLinkedList&amp;lt;String&amp;gt;&amp;nbsp;list&amp;nbsp;=&amp;nbsp;&lt;/font&gt;&lt;font color="#7f0055"&gt;&lt;b&gt;new&amp;nbsp;&lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;CircularLinkedList&amp;lt;String&amp;gt;&lt;/font&gt;&lt;font color="#000000"&gt;()&lt;/font&gt;&lt;font color="#000000"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#000000"&gt;list.add&lt;/font&gt;&lt;font color="#000000"&gt;(&lt;/font&gt;&lt;font color="#2a00ff"&gt;&amp;#34;One&amp;#34;&lt;/font&gt;&lt;font color="#000000"&gt;)&lt;/font&gt;&lt;font color="#000000"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#000000"&gt;list.add&lt;/font&gt;&lt;font color="#000000"&gt;(&lt;/font&gt;&lt;font color="#2a00ff"&gt;&amp;#34;Two&amp;#34;&lt;/font&gt;&lt;font color="#000000"&gt;)&lt;/font&gt;&lt;font color="#000000"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#000000"&gt;list.add&lt;/font&gt;&lt;font color="#000000"&gt;(&lt;/font&gt;&lt;font color="#2a00ff"&gt;&amp;#34;Three&amp;#34;&lt;/font&gt;&lt;font color="#000000"&gt;)&lt;/font&gt;&lt;font color="#000000"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#000000"&gt;list.add&lt;/font&gt;&lt;font color="#000000"&gt;(&lt;/font&gt;&lt;font color="#2a00ff"&gt;&amp;#34;Four&amp;#34;&lt;/font&gt;&lt;font color="#000000"&gt;)&lt;/font&gt;&lt;font color="#000000"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#000000"&gt;System.out.println&lt;/font&gt;&lt;font color="#000000"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;list.remove&lt;/font&gt;&lt;font color="#000000"&gt;(&lt;/font&gt;&lt;font color="#2a00ff"&gt;&amp;#34;Three&amp;#34;&lt;/font&gt;&lt;font color="#000000"&gt;))&lt;/font&gt;&lt;font color="#000000"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#000000"&gt;System.out.println&lt;/font&gt;&lt;font color="#000000"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;list.remove&lt;/font&gt;&lt;font color="#000000"&gt;(&lt;/font&gt;&lt;font color="#2a00ff"&gt;&amp;#34;Two&amp;#34;&lt;/font&gt;&lt;font color="#000000"&gt;))&lt;/font&gt;&lt;font color="#000000"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#000000"&gt;System.out.println&lt;/font&gt;&lt;font color="#000000"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;list.remove&lt;/font&gt;&lt;font color="#000000"&gt;(&lt;/font&gt;&lt;font color="#2a00ff"&gt;&amp;#34;One&amp;#34;&lt;/font&gt;&lt;font color="#000000"&gt;))&lt;/font&gt;&lt;font color="#000000"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#000000"&gt;System.out.println&lt;/font&gt;&lt;font color="#000000"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;list.remove&lt;/font&gt;&lt;font color="#000000"&gt;(&lt;/font&gt;&lt;font color="#2a00ff"&gt;&amp;#34;Four&amp;#34;&lt;/font&gt;&lt;font color="#000000"&gt;))&lt;/font&gt;&lt;font color="#000000"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#000000"&gt;System.out.println&lt;/font&gt;&lt;font color="#000000"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;list.remove&lt;/font&gt;&lt;font color="#000000"&gt;(&lt;/font&gt;&lt;font color="#2a00ff"&gt;&amp;#34;Four&amp;#34;&lt;/font&gt;&lt;font color="#000000"&gt;))&lt;/font&gt;&lt;font color="#000000"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&lt;/font&gt;&lt;br /&gt;&lt;font color="#ffffff"&gt;&amp;nbsp;&lt;/font&gt;&lt;font color="#000000"&gt;}&lt;/font&gt;&lt;br /&gt;&lt;font color="#000000"&gt;}&lt;/font&gt;&lt;/pre&gt;&lt;span style="font-weight:bold;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/18313336-7206627241755231542?l=java-x.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://java-x.blogspot.com/feeds/7206627241755231542/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://java-x.blogspot.com/2008/07/cricular-linked-list-in-java.html#comment-form' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/18313336/posts/default/7206627241755231542'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/18313336/posts/default/7206627241755231542'/><link rel='alternate' type='text/html' href='http://java-x.blogspot.com/2008/07/cricular-linked-list-in-java.html' title='Cricular Linked List in Java'/><author><name>Abhilash Vuyyuru</name><uri>https://profiles.google.com/107328798079149921932</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh6.googleusercontent.com/-NxAgT10-nrk/AAAAAAAAAAI/AAAAAAAAB0o/Gz3VMjlEXb0/s512-c/photo.jpg'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-18313336.post-1615115084247427785</id><published>2008-07-14T14:12:00.004-04:00</published><updated>2008-07-14T15:19:09.029-04:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='patterns'/><category scheme='http://www.blogger.com/atom/ns#' term='code quality'/><category scheme='http://www.blogger.com/atom/ns#' term='tips'/><title type='text'>Detecting Code Smells With Eclipse and CheckStyle</title><content type='html'>In a new article "&lt;a href="http://www.ibm.com/developerworks/library/j-ap07088/"&gt;Automation for the people: Continual refactoring&lt;/a&gt;" as a part of the "Automation for the people" series, Paul Duvall discusses the use of static code analysis tools to identify code smells and suggested refactorings. The article shows how to &lt;blockquote&gt;&lt;ul&gt;&lt;li&gt;Reduce &lt;b&gt;conditional complexity&lt;/b&gt; code smells by measuring &lt;b&gt;cyclomatic complexity&lt;/b&gt; using CheckStyle and providing refactorings such as &lt;b&gt;Replace Conditional with Polymorphism&lt;/b&gt;&lt;/li&gt;&lt;li&gt;Remove &lt;b&gt;duplicated code&lt;/b&gt; code smells by assessing code duplication using CheckStyle and providing refactorings such as &lt;b&gt;Pull Up Method&lt;/b&gt;&lt;/li&gt;&lt;li&gt;Thin &lt;b&gt;large class&lt;/b&gt;  code smells by counting &lt;b&gt;source lines of code&lt;/b&gt; using PMD (or JavaNCSS) and providing refactorings such as &lt;b&gt;Extract Method&lt;/b&gt;&lt;/li&gt;&lt;li&gt;Wipe out &lt;b&gt;too many imports&lt;/b&gt; code smells by determining a class's &lt;b&gt;efferent coupling&lt;/b&gt; using CheckStyle (or JDepend) and providing refactorings such as &lt;b&gt;Move Method&lt;/b&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/blockquote&gt;&lt;a href="http://java-x.blogspot.com/2008/07/detecting-code-smells-with-eclipse-and.html"&gt;&lt;span style="font-weight:bold;"&gt;There's More&lt;/span&gt;&lt;/a&gt;&lt;span class="fullpost"&gt;The following is a short list of static code analysis tools available for Java&lt;ul&gt;&lt;li&gt;&lt;a href="http://checkstyle.sourceforge.net/index.html"&gt;CheckStyle&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://pmd.sourceforge.net/"&gt;PMD&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://findbugs.sourceforge.net/"&gt;FindBugs&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.kclee.de/clemens/java/javancss/"&gt;JavaNCSS&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://clarkware.com/software/JDepend.html"&gt;JDepend&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;This post describes how to identify common code smells using CheckStyle and Eclipse. Checkstyle has a useful eclipse &lt;a href="http://eclipse-cs.sourceforge.net/"&gt;plugin&lt;/a&gt;. Installing the plugin is simple. In Eclipse Ganymede, &lt;ol&gt;&lt;li&gt;Go to  &lt;em&gt;Help-&amp;gt;Software Updates-&amp;gt;Software Updates-&amp;gt;Available Software&lt;/em&gt;&lt;/li&gt;&lt;li&gt;Click on Add Site, and add http://eclipse-cs.sourceforge.net/update to the sites list&lt;/li&gt;&lt;li&gt;Select the new site and click Install&lt;/li&gt;&lt;/ol&gt;Once CheckStyle plugin is install, running the tool is quite simple. Usage is well documented in the &lt;a href="http://eclipse-cs.sourceforge.net/getting_started.html"&gt;plugin site&lt;/a&gt;. The following are some Code smells which can be detected using Checkstyle, along with the suggested refactorings (from the &lt;a href="http://www.industriallogic.com/papers/smellstorefactorings.pdf"&gt;"Smells to Refactorings Quick Reference Guide"&lt;/a&gt;). The description of the refactorings can be found at &lt;a href="http://www.refactoring.com/catalog/"&gt;refactoring.com&lt;/a&gt; and &lt;a href="http://www.industriallogic.com/xp/refactoring/catalog.html"&gt;refactoring to patterns catalog&lt;/a&gt;. The center column shows the CheckStyle configuration option in the plugin GUI.&lt;table border="1px"&gt;&lt;tr&gt;&lt;td width="20%"&gt;Conditional complexity&lt;/td&gt;&lt;td width="40%"&gt;Metrics-&amp;gt;Cyclomatic Complexity&lt;/td&gt;&lt;td width="40%"&gt;&lt;ul&gt;&lt;li&gt;Introduce Null Object&lt;/li&gt;&lt;li&gt;Move Embellishment to Decorator&lt;/li&gt;&lt;li&gt;Replace Condidtional Logic with Strategy&lt;/li&gt;&lt;li&gt;Replace State-Altering Conditionals with State&lt;/li&gt;&lt;li&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;td width="20%"&gt;Duplicated code&lt;/td&gt;&lt;td width="40%"&gt;Duplicates-&amp;gt;Strict Duplicate Code&lt;/td&gt;&lt;td width="40%"&gt;&lt;ul&gt;&lt;li&gt;Chain Constructors&lt;/li&gt;&lt;li&gt;Extract Composite&lt;/li&gt;&lt;li&gt;Extract Method&lt;/li&gt;&lt;li&gt;Extract Class&lt;/li&gt;&lt;li&gt;Form Template Method&lt;/li&gt;&lt;li&gt;Introduce Null Object&lt;/li&gt;&lt;li&gt;Introduce Polymorphic Creation with Factory Method&lt;/li&gt;&lt;li&gt;Pull Up Method&lt;/li&gt;&lt;li&gt;Pull Up Field&lt;/li&gt;&lt;li&gt;Replace One/Many Distinctions with Composite&lt;/li&gt;&lt;li&gt;Substitue Algorithm&lt;/li&gt;&lt;li&gt;Unify Interfaces with Adapter&lt;/li&gt;&lt;/ul&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;td width="20%"&gt;Long method&lt;/td&gt;&lt;td width="40%"&gt;Size Violations-&amp;gt;Maximum Method Length&lt;/td&gt;&lt;td width="40%"&gt;&lt;ul&gt;&lt;li&gt;Extract Method&lt;/li&gt;&lt;li&gt;Compose Method&lt;/li&gt;&lt;li&gt;Introduce Parameter Object&lt;/li&gt;&lt;li&gt;Move Accumulation to Collecting Parameter&lt;/li&gt;&lt;li&gt;Move Accumulation to Visitor&lt;/li&gt;&lt;li&gt;Decompose Conditional&lt;/li&gt;&lt;li&gt;Preserve Whole Object&lt;/li&gt;&lt;li&gt;Replace Conditional Dispatcher with Command&lt;/li&gt;&lt;li&gt;Replace Conditional Logic with Strategy&lt;/li&gt;&lt;li&gt;Replace Method with Method Object&lt;/li&gt;&lt;li&gt;Replace Temp with Query&lt;/li&gt;&lt;/ul&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;td width="20%"&gt;&lt;/td&gt;&lt;td width="40%"&gt;&lt;/td&gt;&lt;td width="40%"&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/18313336-1615115084247427785?l=java-x.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://java-x.blogspot.com/feeds/1615115084247427785/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://java-x.blogspot.com/2008/07/detecting-code-smells-with-eclipse-and.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/18313336/posts/default/1615115084247427785'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/18313336/posts/default/1615115084247427785'/><link rel='alternate' type='text/html' href='http://java-x.blogspot.com/2008/07/detecting-code-smells-with-eclipse-and.html' title='Detecting Code Smells With Eclipse and CheckStyle'/><author><name>Abhilash Vuyyuru</name><uri>https://profiles.google.com/107328798079149921932</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh6.googleusercontent.com/-NxAgT10-nrk/AAAAAAAAAAI/AAAAAAAAB0o/Gz3VMjlEXb0/s512-c/photo.jpg'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-18313336.post-3970608147650253309</id><published>2008-07-08T16:07:00.004-04:00</published><updated>2008-07-08T16:22:21.020-04:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='tips'/><category scheme='http://www.blogger.com/atom/ns#' term='web services'/><title type='text'>Web Service Best Practices</title><content type='html'>&lt;a href="http://www.ibm.com/developerworks/blogs/page/woolf?entry=web_services_best_practices"&gt;Bobby Woolf&lt;/a&gt; at IBM has a list of articles with best practices for working with Web Services. Most of these links are IBM resources.&lt;blockquote&gt;&lt;ul&gt;&lt;li&gt;"Best practices for Web services" series: &lt;a href="http://www.ibm.com/developerworks/webservices/library/ws-best1/"&gt;Part 1&lt;/a&gt; through &lt;a href="http://www.ibm.com/developerworks/webservices/library/ws-best12/"&gt;Part 12&lt;/a&gt; (developerWorks)&lt;/li&gt;&lt;li&gt;"&lt;a href="http://www.ibm.com/developerworks/websphere/techjournal/0310_brown/brown.html"&gt;Web Services Architectures and Best Practices&lt;/a&gt;" (developerWorks)&lt;/li&gt;&lt;li&gt;"&lt;a href="http://www.ibm.com/developerworks/webservices/library/ws-version/"&gt;Best practices for Web services versioning&lt;/a&gt;" (developerWorks)&lt;/li&gt;&lt;li&gt;"&lt;a href="http://www.ibm.com/developerworks/edu/ws-dw-wsbestprofil-i.html"&gt;Best Practices and Web services Profiles&lt;/a&gt;" (developerWorks)&lt;/li&gt;&lt;li&gt;"&lt;a href="http://publib.boulder.ibm.com/infocenter/wasinfo/v6r1/topic/com.ibm.websphere.nd.multiplatform.doc/info/ae/ae/rwbs_perfbestpractices.html"&gt;Web services performance best practices&lt;/a&gt;" (WAS InfoCenter)&lt;/li&gt;&lt;li&gt;"&lt;a href="http://publib.boulder.ibm.com/infocenter/wasinfo/v6r1/topic/com.ibm.websphere.nd.multiplatform.doc/info/ae/ae/rwbs_migrate.html"&gt;Web services migration best practices&lt;/a&gt;" (WAS InfoCenter)&lt;/li&gt;&lt;li&gt;&lt;em&gt;&lt;a href="http://www.redbooks.ibm.com/abstracts/sg246461.html"&gt;WebSphere Version 6 Web Services Handbook Development and Deployment&lt;/a&gt;&lt;/em&gt; (Redbook)&lt;/li&gt;&lt;li&gt;&lt;em&gt;&lt;a href="http://www.redbooks.ibm.com/abstracts/sg247257.html"&gt;Web Services Handbook for WebSphere Application Server 6.1&lt;/a&gt;&lt;/em&gt; (Redbook)&lt;/li&gt;&lt;li&gt;&lt;em&gt;&lt;a href="http://www.redbooks.ibm.com/abstracts/redp4365.html"&gt;IBM WebSphere DataPower SOA Appliances Part III: XML Security Guide&lt;/a&gt;&lt;/em&gt; (Redbook)&lt;/li&gt;&lt;li&gt;"&lt;a href="http://www.ibm.com/developerworks/library/ws-soapjms1/"&gt;Web services with SOAP over JMS in IBM WebSphere Process Server or IBM WebSphere Enterprise Service Bus&lt;/a&gt;" (developerWorks)&lt;/li&gt;&lt;li&gt;"&lt;a href="http://publib.boulder.ibm.com/infocenter/wasinfo/v6r1/topic/com.ibm.websphere.nd.multiplatform.doc/info/ae/ae/rwbs_soapjms.html"&gt;Web services SOAP/JMS protocol&lt;/a&gt;" (WAS InfoCenter)&lt;/li&gt;&lt;li&gt;"&lt;a href="http://publib.boulder.ibm.com/infocenter/radhelp/v7r0m0/topic/com.ibm.etools.webservice.was.creation.ejb.ui.doc/concepts/chttpjms.html"&gt;HTTP and JMS transport methods&lt;/a&gt;" (RAD InfoCenter)&lt;/li&gt;&lt;/ul&gt;&lt;/blockquote&gt;To these I'd just like to add &lt;a href="http://www.xml.com/pub/a/2004/08/11/rest.html"&gt;Implementing REST Web Services: Best Practices and Guidelines&lt;/a&gt;, which I find useful for implementing REST based web services.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/18313336-3970608147650253309?l=java-x.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://java-x.blogspot.com/feeds/3970608147650253309/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://java-x.blogspot.com/2008/07/web-service-best-practices.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/18313336/posts/default/3970608147650253309'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/18313336/posts/default/3970608147650253309'/><link rel='alternate' type='text/html' href='http://java-x.blogspot.com/2008/07/web-service-best-practices.html' title='Web Service Best Practices'/><author><name>Abhilash Vuyyuru</name><uri>https://profiles.google.com/107328798079149921932</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh6.googleusercontent.com/-NxAgT10-nrk/AAAAAAAAAAI/AAAAAAAAB0o/Gz3VMjlEXb0/s512-c/photo.jpg'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-18313336.post-8254720464427665665</id><published>2008-04-17T09:35:00.008-04:00</published><updated>2009-06-27T19:45:27.739-04:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='hibernate'/><category scheme='http://www.blogger.com/atom/ns#' term='persistence'/><category scheme='http://www.blogger.com/atom/ns#' term='spring'/><category scheme='http://www.blogger.com/atom/ns#' term='example/sample code'/><title type='text'>Integrating Spring and Hibernate: Transactions</title><content type='html'>In the previous post, I described different ways in which spring and hibernate can&lt;br /&gt;be integrated. In this post I will describe how to use Spring's transaction features&lt;br /&gt;in hibernate. The following methods of transaction management with spring and hibernate are discussed.&lt;ol&gt;&lt;li&gt;Declarative Transaction Mangement with AOP Interceptors&lt;/li&gt;&lt;li&gt;Schema-based Declarative Transaction Management&lt;/li&gt;&lt;li&gt;Schema-based Declarative Transaction Management with Annotations&lt;/li&gt;&lt;li&gt;Programmatic Transaction Management&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;The easiest way to check if transactions are working for this example is to remove the transaction declarations for the getStockQuote() method. Removing transactions will cause the following exception&lt;pre&gt;org.hibernate.LazyInitializationException: could not initialize proxy - no Session&lt;/pre&gt;&lt;br /&gt;&lt;a href="http://java-x.blogspot.com/2008/04/integrating-spring-and-hibernate_17.html"&gt;There's More&lt;/a&gt; ...&lt;span class="fullpost"&gt;&lt;br /&gt;For this example, start off with the following as described in the "&lt;a href="http://java-x.blogspot.com/2008/04/integrating-spring-and-hibernate.html"&gt;Integrating Spring and Hibernate&lt;/a&gt;" post.&lt;br /&gt;&lt;ol&gt;&lt;li&gt;The bean StockQuoteBean&lt;/li&gt;&lt;li&gt;The Portfolio classes : PortfolioDAO, PortfolioDAOSupport and PortfolioDAOTemplate&lt;/li&gt;&lt;li&gt;The hibernate mapping file stockquote.hbm.xml&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;The following main class can be used:&lt;pre&gt;package springhibernate;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;import org.springframework.context.ApplicationContext;&lt;br /&gt;import org.springframework.context.support.ClassPathXmlApplicationContext;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;public class SpringHibernateTest {&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;public static void main(String[] args) {&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;  IPortfolioService portfolioService = (IPortfolioService) ctx.getBean("portfolioService");&lt;/span&gt;&lt;br /&gt;System.out.println("Portfolio Service type : " + portfolioService.getClass());&lt;br /&gt;portfolioService.getStockQuote("123");&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;}&lt;/pre&gt;&lt;center&gt;SpringHibernateTest.java&lt;/center&gt;&lt;br /&gt;Note that here we use an Interface IPortfolioService, and also note the change to ApplicationContext instead of a BeanFactory.&lt;br /&gt;&lt;span style="font-style: italic;"&gt;The reason for using ApplicationContext is enable the use of AOP for declarative transaction management.&lt;/span&gt; Also we are not expecting any return to the main class as all the execution is expected to happen in the transaction context.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="font-weight: bold;"&gt;The Portfolio Service Interface:&lt;/span&gt;&lt;/span&gt;&lt;pre&gt;package springhibernate;&lt;br /&gt;&lt;br /&gt;import beans.StockQuoteBean;&lt;br /&gt;&lt;br /&gt;public interface IPortfolioService {&lt;br /&gt;&lt;br /&gt;public void getStockQuote(String id);&lt;br /&gt;&lt;br /&gt;public void updateStockQuote(StockQuoteBean stockQuoteBean);&lt;br /&gt;&lt;br /&gt;}&lt;/pre&gt;&lt;center&gt;IPortfolioService.java&lt;/center&gt;&lt;br /&gt;&lt;div class="post-inner-heading"&gt;Declarative Transaction Management&lt;/div&gt;&lt;br /&gt;Declarative transaction management in Spring has the advantage of being less invasive. There is no need for changing application code when using declarative transactions. All you have to do is to modify the application context.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;The Service class&lt;/span&gt; will be same for all the modes of Declarative transaction management described below.&lt;pre&gt;package springhibernate;&lt;br /&gt;&lt;br /&gt;import beans.StockQuoteBean;&lt;br /&gt;import dao.PortfolioDAO;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;public class PortfolioService implements IPortfolioService {&lt;br /&gt;private PortfolioDAO portfolioDAO;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;public void getStockQuote(String id) {&lt;br /&gt;StockQuoteBean result = portfolioDAO.getStockQuote(id);&lt;br /&gt;System.out.println("Result in Service : " + result.getStockSymbol());&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public void updateStockQuote(StockQuoteBean stockQuoteBean) {&lt;br /&gt;portfolioDAO.updateStockQuote(stockQuoteBean);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public PortfolioDAO getPortfolioDAO() {&lt;br /&gt;return portfolioDAO;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public void setPortfolioDAO(PortfolioDAO portfolioDAO) {&lt;br /&gt;this.portfolioDAO = portfolioDAO;&lt;br /&gt;System.out.println("Setting portfolio DAO to : " + portfolioDAO.getClass());&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;/pre&gt;&lt;center&gt;PortfolioService.java&lt;/center&gt;&lt;br /&gt;&lt;div class="post-inner-heading"&gt;Declarative Transaction Management with AOP Interceptor&lt;/div&gt;&lt;br /&gt;In this method, you have to define a proxy for the bean that will be made transactional.&lt;pre&gt;&amp;lt;?xml version="1.0" encoding="UTF-8"?&amp;gt;&lt;br /&gt;&amp;lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&lt;br /&gt;xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"&lt;br /&gt;xsi:schemaLocation="&lt;br /&gt; http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd&lt;br /&gt; http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd&lt;br /&gt; http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;bean id="portfolioDAOTemplate" class="dao.PortfolioDAOTemplate"&amp;gt;&lt;br /&gt;&amp;lt;property name="hibernateTemplate" ref="hibernateTemplate" /&amp;gt;&lt;br /&gt;&amp;lt;/bean&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;bean id="portfolioDAOSupport" class="dao.PortfolioDAOSupport"&amp;gt;&lt;br /&gt;&amp;lt;property name="hibernateTemplate" ref="hibernateTemplate" /&amp;gt;&lt;br /&gt;&amp;lt;/bean&amp;gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt; &amp;lt;bean id="myPortfolioService" class="springhibernate.PortfolioService"&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;  &amp;lt;property name="portfolioDAO" ref="portfolioDAOTemplate"&amp;gt;&amp;lt;/property&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt; &amp;lt;/bean&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt; &amp;lt;bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;  &amp;lt;property name="sessionFactory" ref="sessionFactory" /&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt; &amp;lt;/bean&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt; &amp;lt;bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor"&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;  &amp;lt;property name="transactionManager"&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;   &amp;lt;ref bean="transactionManager" /&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;  &amp;lt;/property&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;  &amp;lt;property name="transactionAttributeSource"&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;   &amp;lt;value&amp;gt;springhibernate.PortfolioService.*=PROPAGATION_REQUIRED&amp;lt;/value&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;  &amp;lt;/property&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt; &amp;lt;/bean&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt; &amp;lt;bean id="hibernateInterceptor" class="org.springframework.orm.hibernate3.HibernateInterceptor"&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;  &amp;lt;property name="sessionFactory" ref="sessionFactory" /&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt; &amp;lt;/bean&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt; &amp;lt;bean id="portfolioService" class="org.springframework.aop.framework.ProxyFactoryBean"&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;  &amp;lt;property name="target" ref="myPortfolioService"&amp;gt;&amp;lt;/property&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;  &amp;lt;property name="interceptorNames"&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;   &amp;lt;list&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;    &amp;lt;value&amp;gt;transactionInterceptor&amp;lt;/value&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;    &amp;lt;value&amp;gt;hibernateInterceptor&amp;lt;/value&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;   &amp;lt;/list&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;  &amp;lt;/property&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt; &amp;lt;/bean&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"&amp;gt;&lt;br /&gt;&amp;lt;property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" /&amp;gt;&lt;br /&gt;&amp;lt;property name="url" value="jdbc:oracle:thin:@localhost:1521/xe" /&amp;gt;&lt;br /&gt;&amp;lt;property name="username" value="appUser" /&amp;gt;&lt;br /&gt;&amp;lt;property name="password" value="password" /&amp;gt;&lt;br /&gt;&amp;lt;/bean&amp;gt;&lt;br /&gt;&amp;lt;bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"&amp;gt;&lt;br /&gt;&amp;lt;property name="dataSource" ref="dataSource" /&amp;gt;&lt;br /&gt;&amp;lt;property name="mappingResources"&amp;gt;&lt;br /&gt;&amp;lt;list&amp;gt;&lt;br /&gt; &amp;lt;value&amp;gt;stockquote.hbm.xml&amp;lt;/value&amp;gt;&lt;br /&gt;&amp;lt;/list&amp;gt;&lt;br /&gt;&amp;lt;/property&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;property name="hibernateProperties"&amp;gt;&lt;br /&gt;&amp;lt;props&amp;gt;&lt;br /&gt; &amp;lt;prop key="hibernate.dialect"&amp;gt;org.hibernate.dialect.Oracle9Dialect&amp;lt;/prop&amp;gt;&lt;br /&gt; &amp;lt;prop key="hibernate.show_sql"&amp;gt;true&amp;lt;/prop&amp;gt;&lt;br /&gt; &amp;lt;prop key="hibernate.generate_statistics"&amp;gt;true&amp;lt;/prop&amp;gt;&lt;br /&gt;&amp;lt;/props&amp;gt;&lt;br /&gt;&amp;lt;/property&amp;gt;&lt;br /&gt;&amp;lt;/bean&amp;gt;&lt;br /&gt;&amp;lt;bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"&amp;gt;&lt;br /&gt;&amp;lt;property name="sessionFactory" ref="sessionFactory" /&amp;gt;&lt;br /&gt;&amp;lt;/bean&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;/beans&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;center&gt;applicationContext.xml&lt;/center&gt;&lt;br /&gt;Note that the transactionInterceptor and hibernateTransactionInterceptor are declared. While transactionInterceptor is used to set the transaction properties, the hibernateTransactionInterceptor will manage the hibernate transactions.&lt;br /&gt;&lt;br /&gt;&lt;div class="post-inner-heading"&gt;Schema-based Declarative Transaction Management&lt;/div&gt;&lt;br /&gt;&lt;pre&gt;&amp;lt;?xml version="1.0" encoding="UTF-8"?&amp;gt;&lt;br /&gt;&amp;lt;beans  xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&lt;br /&gt;xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"&lt;br /&gt;xsi:schemaLocation="&lt;br /&gt; http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd&lt;br /&gt; http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd&lt;br /&gt; http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;bean id="portfolioDAOTemplate" class="dao.PortfolioDAOTemplate"&amp;gt;&lt;br /&gt;&amp;lt;property name="hibernateTemplate" ref="hibernateTemplate" /&amp;gt;&lt;br /&gt;&amp;lt;/bean&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;bean id="portfolioDAOSupport" class="dao.PortfolioDAOSupport"&amp;gt;&lt;br /&gt;&amp;lt;property name="hibernateTemplate" ref="hibernateTemplate" /&amp;gt;&lt;br /&gt;&amp;lt;/bean&amp;gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt; &amp;lt;bean id="portfolioService" class="springhibernate.PortfolioService"&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;  &amp;lt;property name="portfolioDAO" ref="portfolioDAOTemplate"&amp;gt;&amp;lt;/property&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt; &amp;lt;/bean&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt; &amp;lt;bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;  &amp;lt;property name="sessionFactory" ref="sessionFactory" /&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt; &amp;lt;/bean&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt; &amp;lt;aop:config&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;  &amp;lt;aop:pointcut id="serviceMethods" expression="execution(* springhibernate.IPortfolioService.*(..))" /&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;  &amp;lt;aop:advisor  advice-ref="txAdvice" pointcut-ref="serviceMethods" /&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt; &amp;lt;/aop:config&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt; &lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt; &amp;lt;tx:advice id="txAdvice"  transaction-manager="transactionManager" &amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;  &amp;lt;tx:attributes&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;   &amp;lt;tx:method name="*" propagation="REQUIRES_NEW" /&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;  &amp;lt;/tx:attributes&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt; &amp;lt;/tx:advice&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"&amp;gt;&lt;br /&gt;&amp;lt;property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" /&amp;gt;&lt;br /&gt;&amp;lt;property name="url" value="jdbc:oracle:thin:@localhost:1521/xe" /&amp;gt;&lt;br /&gt;&amp;lt;property name="username" value="appUser" /&amp;gt;&lt;br /&gt;&amp;lt;property name="password" value="password" /&amp;gt;&lt;br /&gt;&amp;lt;/bean&amp;gt;&lt;br /&gt;&amp;lt;bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"&amp;gt;&lt;br /&gt;&amp;lt;property name="dataSource" ref="dataSource" /&amp;gt;&lt;br /&gt;&amp;lt;property name="mappingResources"&amp;gt;&lt;br /&gt;&amp;lt;list&amp;gt;&lt;br /&gt; &amp;lt;value&amp;gt;stockquote.hbm.xml&amp;lt;/value&amp;gt;&lt;br /&gt;&amp;lt;/list&amp;gt;&lt;br /&gt;&amp;lt;/property&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;property name="hibernateProperties"&amp;gt;&lt;br /&gt;&amp;lt;props&amp;gt;&lt;br /&gt; &amp;lt;prop key="hibernate.dialect"&amp;gt;org.hibernate.dialect.Oracle9Dialect&amp;lt;/prop&amp;gt;&lt;br /&gt; &amp;lt;prop key="hibernate.show_sql"&amp;gt;true&amp;lt;/prop&amp;gt;&lt;br /&gt; &amp;lt;prop key="hibernate.generate_statistics"&amp;gt;true&amp;lt;/prop&amp;gt;&lt;br /&gt;&amp;lt;/props&amp;gt;&lt;br /&gt;&amp;lt;/property&amp;gt;&lt;br /&gt;&amp;lt;/bean&amp;gt;&lt;br /&gt;&amp;lt;bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"&amp;gt;&lt;br /&gt;&amp;lt;property name="sessionFactory" ref="sessionFactory" /&amp;gt;&lt;br /&gt;&amp;lt;/bean&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;/beans&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;center&gt;applicationContext.xml&lt;/center&gt;Using Schema-based declarative transaction management is a lot simpler and a lot cleaner. However it is &lt;span style="font-style: italic;"&gt;adviced that this method not be used in conjunction with explicit auto-proxying using BeanNameAutoProxyCreator, as it might raise issues like advice not being woven etc. &lt;/span&gt;Note that the AOP pointcut is defined to be all methods on the IPortfolioService inteface.&lt;br /&gt;&lt;div class="post-inner-heading"&gt;&lt;br /&gt;Schema-based Declarative Transaction Management with Annotations &lt;/div&gt;&lt;br /&gt;&lt;pre&gt;&amp;lt;?xml version="1.0" encoding="UTF-8"?&amp;gt;&lt;br /&gt;&amp;lt;beans  xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&lt;br /&gt;xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"&lt;br /&gt;xsi:schemaLocation="&lt;br /&gt; http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd&lt;br /&gt; http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd&lt;br /&gt; http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;bean id="portfolioDAOTemplate" class="dao.PortfolioDAOTemplate"&amp;gt;&lt;br /&gt;&amp;lt;property name="hibernateTemplate" ref="hibernateTemplate" /&amp;gt;&lt;br /&gt;&amp;lt;/bean&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;bean id="portfolioDAOSupport" class="dao.PortfolioDAOSupport"&amp;gt;&lt;br /&gt;&amp;lt;property name="hibernateTemplate" ref="hibernateTemplate" /&amp;gt;&lt;br /&gt;&amp;lt;/bean&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;bean id="portfolioService" class="springhibernate.PortfolioService"&amp;gt;&lt;br /&gt;&amp;lt;property name="portfolioDAO" ref="portfolioDAOTemplate"&amp;gt;&amp;lt;/property&amp;gt;&lt;br /&gt;&amp;lt;/bean&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"&amp;gt;&lt;br /&gt;&amp;lt;property name="sessionFactory" ref="sessionFactory" /&amp;gt;&lt;br /&gt;&amp;lt;/bean&amp;gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt; &amp;lt;tx:annotation-driven/&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"&amp;gt;&lt;br /&gt;&amp;lt;property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" /&amp;gt;&lt;br /&gt;&amp;lt;property name="url" value="jdbc:oracle:thin:@localhost:1521/xe" /&amp;gt;&lt;br /&gt;&amp;lt;property name="username" value="appUser" /&amp;gt;&lt;br /&gt;&amp;lt;property name="password" value="password" /&amp;gt;&lt;br /&gt;&amp;lt;/bean&amp;gt;&lt;br /&gt;&amp;lt;bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"&amp;gt;&lt;br /&gt;&amp;lt;property name="dataSource" ref="dataSource" /&amp;gt;&lt;br /&gt;&amp;lt;property name="mappingResources"&amp;gt;&lt;br /&gt;&amp;lt;list&amp;gt;&lt;br /&gt; &amp;lt;value&amp;gt;stockquote.hbm.xml&amp;lt;/value&amp;gt;&lt;br /&gt;&amp;lt;/list&amp;gt;&lt;br /&gt;&amp;lt;/property&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;property name="hibernateProperties"&amp;gt;&lt;br /&gt;&amp;lt;props&amp;gt;&lt;br /&gt; &amp;lt;prop key="hibernate.dialect"&amp;gt;org.hibernate.dialect.Oracle9Dialect&amp;lt;/prop&amp;gt;&lt;br /&gt; &amp;lt;prop key="hibernate.show_sql"&amp;gt;true&amp;lt;/prop&amp;gt;&lt;br /&gt; &amp;lt;prop key="hibernate.generate_statistics"&amp;gt;true&amp;lt;/prop&amp;gt;&lt;br /&gt;&amp;lt;/props&amp;gt;&lt;br /&gt;&amp;lt;/property&amp;gt;&lt;br /&gt;&amp;lt;/bean&amp;gt;&lt;br /&gt;&amp;lt;bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"&amp;gt;&lt;br /&gt;&amp;lt;property name="sessionFactory" ref="sessionFactory" /&amp;gt;&lt;br /&gt;&amp;lt;/bean&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;/beans&amp;gt;&lt;/pre&gt;&lt;center&gt;applicationContext.xml&lt;/center&gt;When using annotations, you only have to declare the transaction manager and add &amp;lt;tx:annotation-driven/&amp;gt; to the application context. If the transaction manager is named "transactionManager" then you don't have to declare a transaction-manager attribute for &amp;lt;tx:annotation-driven/&amp;gt; as it happens to be the default value for that attribute.&lt;br /&gt;Also we have to declare the pointcuts in the Service class itself. Note that as annotations are not inherited, declaring the annotations has to be done at the class level.&lt;pre style="font-weight: bold;"&gt;@Transactional&lt;br /&gt;public class PortfolioService implements IPortfolioService {&lt;br /&gt;private PortfolioDAO portfolioDAO;...&lt;/pre&gt;&lt;br /&gt;&lt;div class="post-inner-heading"&gt;Programmatic Transaction Management&lt;/div&gt;&lt;br /&gt;For programmatic transaction management in spring, you will need a PlatformTransctionManger in your bean which will be used to create a TransactionTemplate. The TransactionTemplate is used in the same way the HibernateTemplate was used in previous example. Additionally you will have to create a HibernateTransactionManager as shown in the above example.&lt;pre&gt;package springhibernate;&lt;br /&gt;&lt;br /&gt;import org.springframework.transaction.PlatformTransactionManager;&lt;br /&gt;import org.springframework.transaction.TransactionStatus;&lt;br /&gt;import org.springframework.transaction.support.TransactionCallbackWithoutResult;&lt;br /&gt;import org.springframework.transaction.support.TransactionTemplate;&lt;br /&gt;&lt;br /&gt;import beans.StockQuoteBean;&lt;br /&gt;import dao.PortfolioDAO;&lt;br /&gt;&lt;br /&gt;public class PortfolioServiceTransaction implements IPortfolioService{&lt;br /&gt;private PortfolioDAO portfolioDAO;&lt;br /&gt;&lt;br /&gt;private PlatformTransactionManager transactionManager;&lt;br /&gt;&lt;br /&gt;public void getStockQuote(final String id) {&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;  TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager);&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;  transactionTemplate.execute(new TransactionCallbackWithoutResult() {&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;   public void doInTransactionWithoutResult(TransactionStatus status) {&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;    StockQuoteBean result = portfolioDAO.getStockQuote(id);&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;    System.out.println("Symbol in transaction " + result.getStockSymbol());&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;   }&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;  });&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt; public void updateStockQuote(final StockQuoteBean stockQuoteBean) {&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;  TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager);&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;  transactionTemplate.execute(new TransactionCallbackWithoutResult() {&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;   public void doInTransactionWithoutResult(TransactionStatus status) {&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;    portfolioDAO.updateStockQuote(stockQuoteBean);&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;   }&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;  });&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;  &lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt; }&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;public PortfolioDAO getPortfolioDAO() {&lt;br /&gt;return portfolioDAO;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public void setTransactionManager(PlatformTransactionManager transactionManager) {&lt;br /&gt;this.transactionManager = transactionManager;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public void setPortfolioDAO(PortfolioDAO portfolioDAO) {&lt;br /&gt;this.portfolioDAO = portfolioDAO;&lt;br /&gt;System.out.println("Setting portfolio DAO to : " + portfolioDAO.getClass());&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;center&gt;PortfolioService.java&lt;/center&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/18313336-8254720464427665665?l=java-x.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://java-x.blogspot.com/feeds/8254720464427665665/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://java-x.blogspot.com/2008/04/integrating-spring-and-hibernate_17.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/18313336/posts/default/8254720464427665665'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/18313336/posts/default/8254720464427665665'/><link rel='alternate' type='text/html' href='http://java-x.blogspot.com/2008/04/integrating-spring-and-hibernate_17.html' title='Integrating Spring and Hibernate: Transactions'/><author><name>Abhilash Vuyyuru</name><uri>https://profiles.google.com/107328798079149921932</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh6.googleusercontent.com/-NxAgT10-nrk/AAAAAAAAAAI/AAAAAAAAB0o/Gz3VMjlEXb0/s512-c/photo.jpg'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-18313336.post-8001726398514930547</id><published>2008-04-14T20:44:00.006-04:00</published><updated>2009-06-27T19:45:27.739-04:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='hibernate'/><category scheme='http://www.blogger.com/atom/ns#' term='persistence'/><category scheme='http://www.blogger.com/atom/ns#' term='spring'/><category scheme='http://www.blogger.com/atom/ns#' term='example/sample code'/><title type='text'>Integrating Spring and Hibernate</title><content type='html'>This post applies to integrating&lt;span style="font-weight: bold;"&gt; Spring framework 2.5.3 and Hibernate 3.0&lt;/span&gt;.&lt;br /&gt;&lt;br /&gt;The Spring framework provides extensive support for data access through the use of support classes (JdbcDaoSupport, JdbcTemplate etc.), and extensive exception hierarchy to wrap any platform specific SQLException into an exception in the spring exception hierarchy. Additionally Spring framework also provides good support for integrating with ORM technologies like Hibernate and iBatis etc. This post will show how to integrate Spring framework with Hibernate ORM. &lt;a href="http://java-x.blogspot.com/2008/04/integrating-spring-and-hibernate.html"&gt;There's more ...&lt;/a&gt;&lt;span class="fullpost"&gt;&lt;ol&gt;&lt;li&gt;&lt;span style="font-weight: bold;"&gt;Create the bean&lt;/span&gt;: The bean here represents a simple stock quote&lt;pre&gt;package beans;&lt;br /&gt;&lt;br /&gt;public class StockQuoteBean {&lt;br /&gt;private String quoteId;&lt;br /&gt;&lt;br /&gt;private String stockSymbol;&lt;br /&gt;&lt;br /&gt;private String name;&lt;br /&gt;&lt;br /&gt;public String getQuoteId() {&lt;br /&gt;return quoteId;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public void setQuoteId(String quoteId) {&lt;br /&gt;this.quoteId = quoteId;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public String getStockSymbol() {&lt;br /&gt;return stockSymbol;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public void setStockSymbol(String stockSymbol) {&lt;br /&gt;this.stockSymbol = stockSymbol;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public String getName() {&lt;br /&gt;return name;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public void setName(String name) {&lt;br /&gt;this.name = name;&lt;br /&gt;}&lt;br /&gt;}&lt;/pre&gt;&lt;center&gt;StockQuoteBean.java&lt;/center&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-weight: bold;"&gt;Create a Hibernate Mapping&lt;/span&gt; file for the bean: &lt;pre&gt;&amp;lt;?xml version="1.0"?&amp;gt;&lt;br /&gt;&amp;lt;!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"&lt;br /&gt;"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"&amp;gt;&lt;br /&gt;&amp;lt;hibernate-mapping&amp;gt;&lt;br /&gt;&amp;lt;class name="beans.StockQuoteBean" table="STOCK_QUOTES" &lt;span style="font-weight: bold;"&gt;lazy="false"&lt;/span&gt;&amp;gt;&lt;br /&gt;&amp;lt;id name="quoteId" column="quote_id"&amp;gt;&lt;br /&gt; &amp;lt;generator class="assigned" /&amp;gt;&lt;br /&gt;&amp;lt;/id&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;property name="stockSymbol"&amp;gt;&lt;br /&gt; &amp;lt;column name="stock_symbol" /&amp;gt;&lt;br /&gt;&amp;lt;/property&amp;gt;&lt;br /&gt;&amp;lt;property name="name"&amp;gt;&lt;br /&gt; &amp;lt;column name="name" /&amp;gt;&lt;br /&gt;&amp;lt;/property&amp;gt;&lt;br /&gt;&amp;lt;/class&amp;gt;&lt;br /&gt;&amp;lt;/hibernate-mapping&amp;gt;&lt;/pre&gt;&lt;center&gt;stockquote.hbm.xml&lt;/center&gt;&lt;br /&gt;The one important thing to note here is that in the declaration, a [&lt;span style="font-weight: bold;"&gt;lazy="false"&lt;/span&gt;] has been added to the mapping for the stockquote bean. The reason for this is that in hibernate 3, &lt;span style="font-weight: bold;"&gt;lazy initialization is turned on by default&lt;/span&gt;. This raises a problem when used with spring's HibernateCallback. The spring &lt;span style="font-weight: bold;"&gt;HibernateTemplate.execute() by default closes any open sessions upon completion&lt;/span&gt;. When used with lazy initialization you may get a &lt;span style="font-weight: bold;"&gt;LazyInitializationException &lt;/span&gt;like the following&lt;pre style="font-weight: bold;"&gt;org.hibernate.LazyInitializationException: could not initialize proxy - no Session&lt;/pre&gt;If you want to use lazy initialization with HibernateCallback, you will have to use this within a transaction context. The javadoc for HibernateTemplate specifies this explicitly&lt;pre&gt;Note that operations that return an Iterator (i.e. iterate) are supposed&lt;br /&gt;to be used within Spring-driven or JTA-driven transactions (with&lt;br /&gt;HibernateTransactionManager, JtaTransactionManager, or EJB CMT). Else, the&lt;br /&gt;Iterator won't be able to read results from its ResultSet anymore, as the&lt;br /&gt;underlying Hibernate Session will already have been closed.&lt;br /&gt;&lt;br /&gt;Lazy loading will also just work with an open Hibernate Session, either within a&lt;br /&gt;transaction or within OpenSessionInViewFilter/Interceptor. Furthermore, some&lt;br /&gt;operations just make sense within transactions, for example: contains, evict,&lt;br /&gt;lock, flush, clear.&lt;/pre&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-weight: bold;"&gt;The service class&lt;/span&gt;: The service class simply acts as an intermediary between the client and the DAO classes.&lt;pre&gt;package springhibernate;&lt;br /&gt;&lt;br /&gt;import beans.StockQuoteBean;&lt;br /&gt;import dao.PortfolioDAO;&lt;br /&gt;&lt;br /&gt;public class PortfolioService {&lt;br /&gt;private PortfolioDAO portfolioDAO;&lt;br /&gt;&lt;br /&gt;public StockQuoteBean getStockQuote(String id) {&lt;br /&gt;StockQuoteBean result = portfolioDAO.getStockQuote(id);&lt;br /&gt;return result;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public void updateStockQuote(StockQuoteBean stockQuoteBean) {&lt;br /&gt;portfolioDAO.updateStockQuote(stockQuoteBean);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public PortfolioDAO getPortfolioDAO() {&lt;br /&gt;return portfolioDAO;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public void setPortfolioDAO(PortfolioDAO portfolioDAO) {&lt;br /&gt;this.portfolioDAO = portfolioDAO;&lt;br /&gt;System.out.println("Setting portfolio DAO to : " + portfolioDAO.getClass());&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;/pre&gt;&lt;center&gt;PortfolioService.java&lt;/center&gt;&lt;/li&gt;&lt;li&gt;The DAO interface: &lt;pre&gt;package dao;&lt;br /&gt;&lt;br /&gt;import beans.StockQuoteBean;&lt;br /&gt;&lt;br /&gt;public interface PortfolioDAO {&lt;br /&gt;public StockQuoteBean getStockQuote(String id);&lt;br /&gt;public void updateStockQuote(StockQuoteBean bean);&lt;br /&gt;public StockQuoteBean getStockQuote_hibernateTemplate(String id);&lt;br /&gt;public void updateStockQuote_hibernateTemplate(StockQuoteBean bean);&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;center&gt;PortfolioDAO.java&lt;/center&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-weight: bold;"&gt;The DAO Classes&lt;/span&gt;: The DAO classes shows the different ways in which the Hibernate calls can be made using the Spring support classes. There are three primary ways in which these calls can be made&lt;ol&gt;&lt;li&gt;Using the HibernateCallback&lt;/li&gt;&lt;li&gt;Using the HibernateTemplate directly&lt;/li&gt;&lt;li&gt;Using the hibernate native calls using Session&lt;/li&gt;&lt;/ol&gt;Spring also provides two different ways to create the Data access objects that interact with Hibernate.&lt;ol&gt;&lt;li&gt;Using Composition, with &lt;span style="font-weight: bold;"&gt;HibernateTemplate&lt;/span&gt;&lt;/li&gt;&lt;li&gt;Using Inheritance by extending &lt;span style="font-weight: bold;"&gt;HibernateDaoSupport&lt;/span&gt;&lt;/li&gt;&lt;/ol&gt;All these methods will be explained when used in the following sections.&lt;ol&gt;&lt;li&gt;&lt;span style="font-weight: bold;"&gt;Using HibernateTemplate&lt;/span&gt;&lt;pre&gt;package dao;&lt;br /&gt;&lt;br /&gt;import java.sql.SQLException;&lt;br /&gt;import java.util.List;&lt;br /&gt;&lt;br /&gt;import org.hibernate.HibernateException;&lt;br /&gt;import org.hibernate.Session;&lt;br /&gt;import org.springframework.orm.hibernate3.HibernateCallback;&lt;br /&gt;import org.springframework.orm.hibernate3.HibernateTemplate;&lt;br /&gt;&lt;br /&gt;import beans.StockQuoteBean;&lt;br /&gt;&lt;br /&gt;public class PortfolioDAOTemplate implements PortfolioDAO{&lt;br /&gt;private HibernateTemplate hibernateTemplate;&lt;br /&gt;&lt;br /&gt;public PortfolioDAOTemplate() {&lt;br /&gt;System.out.println("Init transaction dao");&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;public StockQuoteBean getStockQuote(&lt;span style="font-weight: bold;"&gt;final String id&lt;/span&gt;) {&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;HibernateCallback callback = new HibernateCallback() {&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;   public Object doInHibernate(Session session) throws HibernateException, SQLException {&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;    return session.load(StockQuoteBean.class, id);&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;   }&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;  };&lt;/span&gt;&lt;br /&gt;return (StockQuoteBean) hibernateTemplate.execute(callback);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt; public void updateStockQuote(final StockQuoteBean StockQuoteBean) {&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;  HibernateCallback callback = new HibernateCallback() {&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;   public Object doInHibernate(Session session) throws HibernateException, SQLException {&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;    session.saveOrUpdate(StockQuoteBean);&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;    return null;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;   }&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;  };&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;  hibernateTemplate.execute(callback);&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;  &lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt; }&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt; public void updateStockQuote_hibernateTemplate(StockQuoteBean StockQuoteBean) {&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;  hibernateTemplate.update(StockQuoteBean);&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt; }&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt; public StockQuoteBean getStockQuote_hibernateTemplate(String id) {&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;  List&amp;lt;StockQuoteBean&amp;gt; transactions = hibernateTemplate.find("from beans.StockQuoteBean stockQuoteBean where stockQuoteBean.quoteId=?", id);&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;  return transactions.get(0);&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt; }&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;public HibernateTemplate getHibernateTemplate() {&lt;br /&gt;return hibernateTemplate;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {&lt;br /&gt;this.hibernateTemplate = hibernateTemplate;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;}&lt;/pre&gt;&lt;center&gt;PortfolioDAOTemplate&lt;/center&gt;&lt;br /&gt;This class shows how to use the HibernateTemplate to make calls to Hibernate. The getStockQuote() and updateStockQuote() methods use HibernateCallback class, note that when using HibernateCallback, it is necessary to either do it in a transactional context or turn off lazy initialization. While the getStockQuote_hibernateTemplate() and updateStockQuote_hibernateTemplate() make calls using hibernateTemplate directly. Also note that the parameters to the getStockQuote(), and updateStockQuote() methods are marked final.&lt;br /&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-weight: bold;"&gt;Using HibernateDaoSupport&lt;/span&gt;&lt;pre&gt;package dao;&lt;br /&gt;&lt;br /&gt;import java.util.List;&lt;br /&gt;&lt;br /&gt;import org.hibernate.Query;&lt;br /&gt;import org.springframework.orm.hibernate3.support.HibernateDaoSupport;&lt;br /&gt;&lt;br /&gt;import beans.StockQuoteBean;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;public class PortfolioDAOSupport extends HibernateDaoSupport implements PortfolioDAO {&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;public void updateStockQuote(StockQuoteBean stockQuoteBean) {&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;  Query query = getSession().createQuery("update beans.StockQuoteBean set stockSymbol=? where quoteId=?");&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;  query.setString(0, stockQuoteBean.getStockSymbol());&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;  query.setString(1, stockQuoteBean.getQuoteId());&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;  query.executeUpdate();&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt; }&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt; public StockQuoteBean getStockQuote(String id) {&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;  Query query = getSession().createQuery("from beans.StockQuoteBean stockQuoteBean where stockQuoteBean.quoteId=?");&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;  query.setString(0, id);&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;  List results = query.list();&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;  if(results == null || results.size() == 0) {&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;   throw new RuntimeException("No result");&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;  }&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;  return (StockQuoteBean)results.get(0);&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt; }&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;public void updateStockQuote_hibernateTemplate(StockQuoteBean StockQuoteBean) {&lt;br /&gt;getHibernateTemplate().update(StockQuoteBean);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;public StockQuoteBean getStockQuote_hibernateTemplate(String id) {&lt;br /&gt;List&amp;lt;StockQuoteBean&amp;gt; transactions = getHibernateTemplate().find("from beans.StockQuoteBean stockQuoteBean where stockQuoteBean.quoteId=?", id);&lt;br /&gt;return transactions.get(0);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;/pre&gt;&lt;center&gt;PortfolioDAOSupport&lt;/center&gt;&lt;br /&gt;This class uses HibernateDaoSupport to get instances of HibernateTemplate, and the Hibernate Session. The getStockQuote() and updateStockQuote() in this class make calls to hibernate session directly.&lt;/li&gt;&lt;/ol&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-weight: bold;"&gt;The application context&lt;/span&gt;&lt;pre&gt;&amp;lt;?xml version="1.0" encoding="UTF-8"?&amp;gt;&lt;br /&gt;&amp;lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&lt;br /&gt;xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"&lt;br /&gt;xsi:schemaLocation="&lt;br /&gt;http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd&lt;br /&gt;http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd&lt;br /&gt;http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"&amp;gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt; &amp;lt;bean id="portfolioDAOTemplate" class="dao.PortfolioDAOTemplate"&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;  &amp;lt;property name="hibernateTemplate" ref="hibernateTemplate" /&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt; &amp;lt;/bean&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt; &lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt; &amp;lt;bean id="portfolioDAOSupport" class="dao.PortfolioDAOSupport"&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;  &amp;lt;property name="hibernateTemplate" ref="hibernateTemplate" /&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt; &amp;lt;/bean&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;bean id="portfolioService" class="springhibernate.PortfolioService"&amp;gt;&lt;br /&gt;&amp;lt;property name="portfolioDAO" ref="portfolioDAOSupport"&amp;gt;&amp;lt;/property&amp;gt;&lt;br /&gt;&amp;lt;/bean&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"&amp;gt;&lt;br /&gt;&amp;lt;property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" /&amp;gt;&lt;br /&gt;&amp;lt;property name="url" value="jdbc:oracle:thin:@localhost:1521/xe" /&amp;gt;&lt;br /&gt;&amp;lt;property name="username" value="appUser" /&amp;gt;&lt;br /&gt;&amp;lt;property name="password" value="password" /&amp;gt;&lt;br /&gt;&amp;lt;/bean&amp;gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;&amp;lt;bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;  &amp;lt;property name="dataSource" ref="dataSource" /&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;  &amp;lt;property name="mappingResources"&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;   &amp;lt;list&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;    &amp;lt;value&amp;gt;stockquote.hbm.xml&amp;lt;/value&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;   &amp;lt;/list&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;  &amp;lt;/property&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;  &amp;lt;property name="hibernateProperties"&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;   &amp;lt;props&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;    &amp;lt;prop key="hibernate.dialect"&amp;gt;org.hibernate.dialect.Oracle9Dialect&amp;lt;/prop&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;    &amp;lt;prop key="hibernate.show_sql"&amp;gt;true&amp;lt;/prop&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;    &amp;lt;prop key="hibernate.generate_statistics"&amp;gt;true&amp;lt;/prop&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;   &amp;lt;/props&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;  &amp;lt;/property&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt; &amp;lt;/bean&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt; &amp;lt;bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;  &amp;lt;property name="sessionFactory" ref="sessionFactory" /&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt; &amp;lt;/bean&amp;gt;&lt;/span&gt;&lt;br /&gt;&amp;lt;/beans&amp;gt;&lt;/pre&gt;&lt;center&gt;applicationContext.xml&lt;/center&gt;&lt;ul&gt;&lt;li&gt;The SessionFactory is defined with the datasource and mapping-resources. The hibernate specific properties are defined under the hibernateProperties property.&lt;/li&gt;&lt;li&gt;The HibernateTemplate uses are reference to the SessionFactory.&lt;/li&gt;&lt;li&gt;The HibernateTemplate is used as a reference to the DAO classes.&lt;/li&gt;&lt;li&gt;The porfolioService bean in uses a reference to the PortfolioDAO, which can be switched between the dao.PortfolioDAOSupport and dao.PortfolioDAOTemplate beans&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;li&gt;The main class&lt;pre&gt;package springhibernate;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;import org.springframework.beans.factory.BeanFactory;&lt;br /&gt;import org.springframework.beans.factory.xml.XmlBeanFactory;&lt;br /&gt;import org.springframework.core.io.FileSystemResource;&lt;br /&gt;import org.springframework.core.io.Resource;&lt;br /&gt;&lt;br /&gt;import beans.StockQuoteBean;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;public class SpringHibernateTest {&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;public static void main(String[] args) {&lt;br /&gt;Resource resource = new FileSystemResource("applicationContext.xml");&lt;br /&gt;BeanFactory factory = new XmlBeanFactory(resource);&lt;br /&gt;&lt;br /&gt;PortfolioService portfolioService = (PortfolioService) factory.getBean("portfolioService");&lt;br /&gt;&lt;br /&gt;StockQuoteBean result = portfolioService.getStockQuote("123");&lt;br /&gt;System.out.println(result.getStockSymbol());&lt;br /&gt;&lt;br /&gt;empResult.setStockSymbol("GOOG");&lt;br /&gt;portfolioService.updateStockQuote(result);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;}&lt;/pre&gt;&lt;center&gt;SpringHibernateTest.java&lt;/center&gt;&lt;/li&gt;&lt;li&gt;Necessary JAR files: &lt;ul&gt;&lt;li&gt;commons-logging-1.1.1.jar&lt;/li&gt;&lt;li&gt;hibernate3.jar&lt;/li&gt;&lt;li&gt;dom4j-1.6.1.jar&lt;/li&gt;&lt;li&gt;ojdbc14.jar&lt;/li&gt;&lt;li&gt;commons-collections-3.2.jar&lt;/li&gt;&lt;li&gt;log4j-1.2.15.jar&lt;/li&gt;&lt;li&gt;commons-dbcp.jar&lt;/li&gt;&lt;li&gt;commons-pool.jar&lt;/li&gt;&lt;li&gt;spring.jar&lt;/li&gt;&lt;li&gt;cglib-nodep-2.1_3.jar&lt;/li&gt;&lt;li&gt;antlr-2.7.6.jar&lt;/li&gt;&lt;li&gt;jta.jar&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;/ol&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/18313336-8001726398514930547?l=java-x.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://java-x.blogspot.com/feeds/8001726398514930547/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://java-x.blogspot.com/2008/04/integrating-spring-and-hibernate.html#comment-form' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/18313336/posts/default/8001726398514930547'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/18313336/posts/default/8001726398514930547'/><link rel='alternate' type='text/html' href='http://java-x.blogspot.com/2008/04/integrating-spring-and-hibernate.html' title='Integrating Spring and Hibernate'/><author><name>Abhilash Vuyyuru</name><uri>https://profiles.google.com/107328798079149921932</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh6.googleusercontent.com/-NxAgT10-nrk/AAAAAAAAAAI/AAAAAAAAB0o/Gz3VMjlEXb0/s512-c/photo.jpg'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-18313336.post-4672241494540843662</id><published>2008-04-11T14:18:00.003-04:00</published><updated>2008-04-11T14:52:36.569-04:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='struts'/><category scheme='http://www.blogger.com/atom/ns#' term='example/sample code'/><title type='text'>Struts 2 Custom Validators</title><content type='html'>Struts 2 allows the use of Custom validators through the &lt;span style="font-weight: bold;"&gt;@CustomValidator&lt;/span&gt; annotation. The @CustomValidator annotation takes two mandatory parameters, type and message&lt;ul&gt;&lt;li&gt;&lt;span style="font-weight: bold;"&gt;type&lt;/span&gt;: Refers to the "name" given to the validator in the validators.xml file.&lt;/li&gt;&lt;li&gt;&lt;span style="font-weight: bold;"&gt;message&lt;/span&gt;: Message to be displayed when this validator fails.&lt;/li&gt;&lt;/ul&gt; A custom validator can be implemented by extending the &lt;span style="font-weight: bold;"&gt;FieldValidatorSupport&lt;/span&gt;, or alternatively the &lt;span style="font-weight: bold;"&gt;ValidatorSupport &lt;/span&gt;classes. The following sample builds on the example built in the previous post, &lt;a href="http://java-x.blogspot.com/2008/04/struts-2-validation-annotations.html"&gt;Struts 2 Validaton: Annotations&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;To run this sample, follow these steps... &lt;a href="http://java-x.blogspot.com/2008/04/struts-2-custom-validators.html"&gt;There's More&lt;/a&gt;&lt;br /&gt;&lt;span class="fullpost"&gt;&lt;br /&gt;&lt;ol&gt;&lt;li&gt;Create a simple struts project as described in the previous example, &lt;a href="http://java-x.blogspot.com/2008/04/struts-2-validation-annotations.html"&gt;Struts 2 Validation : Annotations&lt;/a&gt;&lt;/li&gt;&lt;li&gt;Create the new validator by extending the FieldValidatorSupport class&lt;pre&gt;package validators;&lt;br /&gt;&lt;br /&gt;import com.opensymphony.xwork2.validator.ValidationException;&lt;br /&gt;import com.opensymphony.xwork2.validator.validators.FieldValidatorSupport;&lt;br /&gt;&lt;br /&gt;public class NumberFieldValidator extends FieldValidatorSupport {&lt;br /&gt;&lt;br /&gt; public void validate(Object object) throws ValidationException {&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;       String fieldName = getFieldName();&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;       Object value = this.getFieldValue(fieldName, object);&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;     if (!(value instanceof String)) {&lt;br /&gt;         return;&lt;br /&gt;     }&lt;br /&gt;  &lt;br /&gt;     String str = ((String) value).trim();&lt;br /&gt;     if (str.length() == 0) {&lt;br /&gt;         return;&lt;br /&gt;     }&lt;br /&gt;  &lt;br /&gt;     try {&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;        Double.parseDouble(str);&lt;/span&gt;&lt;br /&gt;     }catch(NumberFormatException nfe) {&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;        addFieldError(fieldName, object);&lt;/span&gt;&lt;br /&gt;      return;&lt;br /&gt;     }&lt;br /&gt;     try {&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;        Integer.parseInt(str);&lt;/span&gt;&lt;br /&gt;     }catch(NumberFormatException nfe) {&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;        addFieldError(fieldName, object);&lt;/span&gt;&lt;br /&gt;      return;&lt;br /&gt;     }&lt;br /&gt;  &lt;br /&gt; }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;center&gt;NumberFieldValidator.java&lt;/center&gt;&lt;ul&gt;&lt;li&gt;The custom validator may extend the FieldValidatorSupport or the ValidatorSupport classes.&lt;/li&gt;&lt;li&gt;The FieldValidatorSupport class extends ValidatorSupport to add field specific information to the Field.&lt;/li&gt;&lt;li&gt;The is numeric check is performed by trying to convert the input string to Integer or Double and catching any exception.&lt;/li&gt;&lt;li&gt;The &lt;span style="font-weight: bold;"&gt;addFieldError &lt;/span&gt;method is used add any failed validations to the list of errors to be displayed.&lt;/li&gt;&lt;li&gt;The getFieldName and getFieldValue methods are implemented in the superclasses to retrieve the field name and field value for the field beign validated.&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;li&gt;Declare the new custom validator in the validators.xml file. The validators.xml file must be in the classpath of the application.&lt;pre&gt;&amp;lt;?xml version="1.0" encoding="UTF-8"?&amp;gt;&lt;br /&gt;&amp;lt;!DOCTYPE validators PUBLIC&lt;br /&gt;     "-//OpenSymphony Group//XWork Validator Config 1.0//EN"&lt;br /&gt;     "http://www.opensymphony.com/xwork/xwork-validator-config-1.0.dtd"&amp;gt;&lt;br /&gt;&amp;lt;validators&amp;gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt; &amp;lt;validator name="numericField" class="validators.NumberFieldValidator"/&amp;gt;&lt;/span&gt;&lt;br /&gt;&amp;lt;/validators&amp;gt;&lt;/pre&gt;&lt;center&gt;validators.xml&lt;/center&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Note: The name attribute of the validator must match the type attribute of the @CustomValidator Annotation used in the Action class.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;Add the additional check to the Action class setPrice() method.&lt;pre&gt; @RequiredStringValidator(type = ValidatorType.FIELD, message = "Price Required")&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;@CustomValidator(type = "numericField", message = "Price must be a number")&lt;/span&gt;&lt;br /&gt;public void setPrice(String price) {&lt;br /&gt; this.price = price;&lt;br /&gt;}&lt;/pre&gt;&lt;center&gt;AddTransactionAction.java&lt;/center&gt;&lt;br /&gt;Note: the type attribute of the @CustomValidator Annotation must match the name of the validator as defined in the validators.xml file.&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/18313336-4672241494540843662?l=java-x.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://java-x.blogspot.com/feeds/4672241494540843662/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://java-x.blogspot.com/2008/04/struts-2-custom-validators.html#comment-form' title='7 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/18313336/posts/default/4672241494540843662'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/18313336/posts/default/4672241494540843662'/><link rel='alternate' type='text/html' href='http://java-x.blogspot.com/2008/04/struts-2-custom-validators.html' title='Struts 2 Custom Validators'/><author><name>Abhilash Vuyyuru</name><uri>https://profiles.google.com/107328798079149921932</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh6.googleusercontent.com/-NxAgT10-nrk/AAAAAAAAAAI/AAAAAAAAB0o/Gz3VMjlEXb0/s512-c/photo.jpg'/></author><thr:total>7</thr:total></entry><entry><id>tag:blogger.com,1999:blog-18313336.post-2082069039796316292</id><published>2008-04-11T13:08:00.006-04:00</published><updated>2008-04-11T13:24:39.738-04:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='struts'/><category scheme='http://www.blogger.com/atom/ns#' term='example/sample code'/><title type='text'>Struts 2 Validation : Annotations</title><content type='html'>In a previous post, I described how to use validations in Struts 2, using XML validation rules. This post will show how to use Annotation based validation in Struts 2. For this example I used the add transaction part of google's portfolio manager (noticed that they do not have validations over there). Struts 2 provides a number of validators for XML based validation rules. All of them have respective annotations defined and can be used in place of XML validation rules. In the example, we will use the &lt;span style="font-weight:bold;"&gt;@RequiredStringValidator&lt;/span&gt;, &lt;span style="font-weight:bold;"&gt;@RegexFieldValidator&lt;/span&gt; and also see how to &lt;span style="font-weight:bold;"&gt;parameterize messages&lt;/span&gt; when using annotations.&lt;br /&gt;&lt;br /&gt;Follow these steps to implement the example ... &lt;a href="http://java-x.blogspot.com/2008/04/struts-2-validation-annotations.html"&gt;There's more&lt;/a&gt;&lt;br /&gt;&lt;span class="fullpost"&gt;&lt;br /&gt;&lt;ol&gt;&lt;li&gt;Create a dynamic web project in Eclipse.&lt;/li&gt;&lt;li&gt;Copy the following jar files into the WEB-INF/lib directory, all these files are available with sturts download.&lt;ul&gt;&lt;li&gt;struts2-core-2.0.11.1.jar&lt;/li&gt;&lt;li&gt;xwork-2.0.4.jar&lt;/li&gt;&lt;li&gt;freemarker-2.3.8.jar&lt;/li&gt;&lt;li&gt;commons-logging-1.1.1.jar&lt;/li&gt;&lt;li&gt;ognl-2.6.11.jar&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;li&gt;Update your web deployment desciptor to include the sturts filter dispatcher.&lt;pre&gt;&amp;lt;?xml version="1.0" encoding="UTF-8"?&amp;gt;&lt;br /&gt;&amp;lt;web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&lt;br /&gt; xmlns="http://java.sun.com/xml/ns/javaee"&lt;br /&gt; xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"&lt;br /&gt; xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"&lt;br /&gt; id="WebApp_ID" version="2.5"&amp;gt;&lt;br /&gt; &amp;lt;display-name&amp;gt;struts2Validation&amp;lt;/display-name&amp;gt;&lt;br /&gt; &amp;lt;filter&amp;gt;&lt;br /&gt;  &amp;lt;filter-name&amp;gt;struts2&amp;lt;/filter-name&amp;gt;&lt;br /&gt;  &amp;lt;filter-class&amp;gt;&lt;br /&gt;   org.apache.struts2.dispatcher.FilterDispatcher&lt;br /&gt;  &amp;lt;/filter-class&amp;gt;&lt;br /&gt; &amp;lt;/filter&amp;gt;&lt;br /&gt; &amp;lt;filter-mapping&amp;gt;&lt;br /&gt;  &amp;lt;filter-name&amp;gt;struts2&amp;lt;/filter-name&amp;gt;&lt;br /&gt;  &amp;lt;url-pattern&amp;gt;/*&amp;lt;/url-pattern&amp;gt;&lt;br /&gt; &amp;lt;/filter-mapping&amp;gt;&lt;br /&gt;&lt;br /&gt; &amp;lt;welcome-file-list&amp;gt;&lt;br /&gt;  &amp;lt;welcome-file&amp;gt;index.html&amp;lt;/welcome-file&amp;gt;&lt;br /&gt; &amp;lt;/welcome-file-list&amp;gt;&lt;br /&gt;&amp;lt;/web-app&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;center&gt;WEB-INF/web.xml&lt;/center&gt;&lt;/li&gt;&lt;li&gt;Create the input JSP &lt;pre&gt;&amp;lt;%@ taglib prefix="s" uri="/struts-tags"%&amp;gt;&lt;br /&gt;&amp;lt;html&amp;gt;&lt;br /&gt;&amp;lt;head&amp;gt;&lt;br /&gt;&amp;lt;title&amp;gt;Add Transaction&amp;lt;/title&amp;gt;&lt;br /&gt;&amp;lt;/head&amp;gt;&lt;br /&gt;&amp;lt;body&amp;gt;&lt;br /&gt;&amp;lt;s:form action="addTransaction" validate="true" method="post"&amp;gt;&lt;br /&gt;&amp;lt;s:fielderror /&amp;gt;&lt;br /&gt;&amp;lt;table&amp;gt;&lt;br /&gt; &amp;lt;tr&amp;gt;&lt;br /&gt;  &amp;lt;td&amp;gt;Symbol&amp;lt;/td&amp;gt;&lt;br /&gt;  &amp;lt;td&amp;gt;&amp;lt;s:textfield name="symbol"&amp;gt;&amp;lt;/s:textfield&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt; &amp;lt;/tr&amp;gt;&lt;br /&gt; &amp;lt;tr&amp;gt;&lt;br /&gt;  &amp;lt;td&amp;gt;Type&amp;lt;/td&amp;gt;&lt;br /&gt;  &amp;lt;td&amp;gt;&amp;lt;s:select name="type" list="#{'':'Select One', 'BUY':'Buy','SELL':'Sell','BUY_COVER':'Buy to Cover','SELL_SHORT':'Sell Short'}"&amp;gt;&lt;br /&gt;  &amp;lt;/s:select&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt; &amp;lt;/tr&amp;gt;&lt;br /&gt; &amp;lt;tr&amp;gt;&lt;br /&gt;  &amp;lt;td&amp;gt;Date&amp;lt;/td&amp;gt;&lt;br /&gt;  &amp;lt;td&amp;gt;&amp;lt;s:textfield name="date" &amp;gt;&amp;lt;/s:textfield&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt; &amp;lt;/tr&amp;gt;&lt;br /&gt; &amp;lt;tr&amp;gt;&lt;br /&gt;  &amp;lt;td&amp;gt;Number of Shares&amp;lt;/td&amp;gt;&lt;br /&gt;  &amp;lt;td&amp;gt;&amp;lt;s:textfield name="numberOfShares"&amp;gt;&amp;lt;/s:textfield&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt; &amp;lt;/tr&amp;gt;&lt;br /&gt; &amp;lt;tr&amp;gt;&lt;br /&gt;  &amp;lt;td&amp;gt;Price&amp;lt;/td&amp;gt;&lt;br /&gt;  &amp;lt;td&amp;gt;&amp;lt;s:textfield name="price"&amp;gt;&amp;lt;/s:textfield&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt; &amp;lt;/tr&amp;gt;&lt;br /&gt; &amp;lt;tr&amp;gt;&lt;br /&gt;  &amp;lt;td&amp;gt;Commission&amp;lt;/td&amp;gt;&lt;br /&gt;  &amp;lt;td&amp;gt;&amp;lt;s:textfield name="comission"&amp;gt;&amp;lt;/s:textfield&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt; &amp;lt;/tr&amp;gt;&lt;br /&gt; &amp;lt;tr&amp;gt;&lt;br /&gt;  &amp;lt;td&amp;gt;Notes&amp;lt;/td&amp;gt;&lt;br /&gt;  &amp;lt;td&amp;gt;&amp;lt;s:textfield name="notes"&amp;gt;&amp;lt;/s:textfield&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt; &amp;lt;/tr&amp;gt;&lt;br /&gt; &amp;lt;tr&amp;gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;  &amp;lt;td colspan="2"&amp;gt; &amp;lt;s:submit name="submit" value="submit"&amp;gt;&amp;lt;/s:submit&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;  &amp;lt;s:submit name="submitNoValidate" value="submit without validation" method="noValidation"&amp;gt;&amp;lt;/s:submit&amp;gt; &amp;lt;/td&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;br /&gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;&amp;lt;/table&amp;gt;&lt;br /&gt;&amp;lt;/s:form&amp;gt;&lt;br /&gt;&amp;lt;/body&amp;gt;&lt;br /&gt;&amp;lt;/html&amp;gt;&lt;/pre&gt;&lt;center&gt;transactions.jsp&lt;/center&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Note&lt;/span&gt;: The method="noValidation" indicates to struts that on submission, the noValidation() method will be invoked on the AddTransactionAction class.&lt;/li&gt;&lt;li&gt;Create the output JSP&lt;pre&gt;&amp;lt;%@ taglib prefix="s" uri="/struts-tags"%&amp;gt;&lt;br /&gt;&amp;lt;html&amp;gt;&lt;br /&gt;&amp;lt;head&amp;gt;&lt;br /&gt;&amp;lt;title&amp;gt;Transaction Added&amp;lt;/title&amp;gt;&lt;br /&gt;&amp;lt;/head&amp;gt;&lt;br /&gt;&amp;lt;body&amp;gt;&lt;br /&gt;&amp;lt;s:bean name="actions.AddTransactionAction" id="addTransaction" &amp;gt;&amp;lt;/s:bean&amp;gt;&lt;br /&gt;&amp;lt;table&amp;gt;&lt;br /&gt; &amp;lt;tr&amp;gt;&lt;br /&gt;  &amp;lt;td colspan="2"&amp;gt;The following transaction has been added to your portfolio&amp;lt;/td&amp;gt;&lt;br /&gt; &amp;lt;/tr&amp;gt;&lt;br /&gt; &amp;lt;tr&amp;gt;&lt;br /&gt;  &amp;lt;td&amp;gt;Symbol&amp;lt;/td&amp;gt;&lt;br /&gt;  &amp;lt;td&amp;gt;&amp;lt;s:label name="symbol" value="%{symbol}" /&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt; &amp;lt;/tr&amp;gt;&lt;br /&gt; &amp;lt;tr&amp;gt;&lt;br /&gt;  &amp;lt;td&amp;gt;Type&amp;lt;/td&amp;gt;&lt;br /&gt;  &amp;lt;td&amp;gt;&amp;lt;s:label name="type" value="%{type}"  /&amp;gt;&lt;br /&gt;&amp;lt;/td&amp;gt;&lt;br /&gt; &amp;lt;/tr&amp;gt;&lt;br /&gt; &amp;lt;tr&amp;gt;&lt;br /&gt;  &amp;lt;td&amp;gt;Date&amp;lt;/td&amp;gt;&lt;br /&gt;  &amp;lt;td&amp;gt;&amp;lt;s:label name="date" value="%{date}"/&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt; &amp;lt;/tr&amp;gt;&lt;br /&gt; &amp;lt;tr&amp;gt;&lt;br /&gt;  &amp;lt;td&amp;gt;Number of Shares&amp;lt;/td&amp;gt;&lt;br /&gt;  &amp;lt;td&amp;gt;&amp;lt;s:label name="numberOfShares" value="%{numberOfShares}"&amp;gt;&amp;lt;/s:label&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt; &amp;lt;/tr&amp;gt;&lt;br /&gt; &amp;lt;tr&amp;gt;&lt;br /&gt;  &amp;lt;td&amp;gt;Price&amp;lt;/td&amp;gt;&lt;br /&gt;  &amp;lt;td&amp;gt;&amp;lt;s:label name="price" value="%{price}"&amp;gt;&amp;lt;/s:label&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt; &amp;lt;/tr&amp;gt;&lt;br /&gt; &amp;lt;tr&amp;gt;&lt;br /&gt;  &amp;lt;td&amp;gt;Commission&amp;lt;/td&amp;gt;&lt;br /&gt;  &amp;lt;td&amp;gt;&amp;lt;s:label name="comission" value="%{comission}"&amp;gt;&amp;lt;/s:label&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt; &amp;lt;/tr&amp;gt;&lt;br /&gt; &amp;lt;tr&amp;gt;&lt;br /&gt;  &amp;lt;td&amp;gt;Notes&amp;lt;/td&amp;gt;&lt;br /&gt;  &amp;lt;td&amp;gt;&amp;lt;s:label name="notes" value="%{notes}"&amp;gt;&amp;lt;/s:label&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;&amp;lt;/table&amp;gt;&lt;br /&gt;&amp;lt;/body&amp;gt;&lt;br /&gt;&amp;lt;/html&amp;gt;&lt;/pre&gt;&lt;center&gt;done.jsp&lt;/center&gt;&lt;/li&gt;&lt;li&gt;Create the Action class &lt;pre&gt;package actions;&lt;br /&gt;&lt;br /&gt;import org.apache.struts2.interceptor.validation.SkipValidation;&lt;br /&gt;&lt;br /&gt;import com.opensymphony.xwork2.ActionSupport;&lt;br /&gt;import com.opensymphony.xwork2.validator.annotations.RegexFieldValidator;&lt;br /&gt;import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;&lt;br /&gt;import com.opensymphony.xwork2.validator.annotations.ValidatorType;&lt;br /&gt;import com.opensymphony.xwork2.validator.annotations.Validation;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;@Validation&lt;/span&gt;&lt;br /&gt;public class AddTransactionAction extends ActionSupport {&lt;br /&gt; &lt;br /&gt; private String symbol;&lt;br /&gt; private String type;&lt;br /&gt; private String date;&lt;br /&gt; private String numberOfShares;&lt;br /&gt; private String price;&lt;br /&gt; private String comission;&lt;br /&gt; private String notes;&lt;br /&gt;&lt;br /&gt; &lt;br /&gt; public String execute() throws Exception {&lt;br /&gt;  System.out.println("In Execute");&lt;br /&gt;  &lt;br /&gt;  return SUCCESS;&lt;br /&gt; }&lt;br /&gt; &lt;br /&gt; &lt;span style="font-weight: bold;" title="validations can be be skipped using the @SkipValidation annotation on a method."&gt;@SkipValidation&lt;/span&gt;&lt;br /&gt; public String noValidation() throws Exception {&lt;br /&gt;  System.out.println("In Novalidation");&lt;br /&gt;  return SUCCESS;&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt;&lt;br /&gt; public String getSymbol() {&lt;br /&gt;  return symbol;&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; &lt;span style="font-weight: bold;"&gt;@RequiredStringValidator(type = ValidatorType.FIELD, message = "Symbol Required")&lt;/span&gt;&lt;br /&gt; public void setSymbol(String symbol) {&lt;br /&gt;  this.symbol = symbol;&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt;&lt;br /&gt; public String getType() {&lt;br /&gt;  return type;&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; &lt;span style="font-weight: bold;"&gt;@RequiredStringValidator(type = ValidatorType.FIELD, message = "Type Required")&lt;/span&gt;&lt;br /&gt; public void setType(String type) {&lt;br /&gt;  this.type = type;&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt;&lt;br /&gt; public String getDate() {&lt;br /&gt;  return date;&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt; @RequiredStringValidator(type = ValidatorType.FIELD, message = "Date Required")&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;" title="the key attribute is used to denote the message key from the properties files."&gt; @RegexFieldValidator(type=ValidatorType.FIELD, message="",key="date.error.message", expression = "[0-9][0-9]/[0-9][0-9]/[1-9][0-9][0-9][0-9]")&lt;/span&gt;&lt;br /&gt; public void setDate(String date) {&lt;br /&gt;  this.date = date;&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt;&lt;br /&gt; public String getNumberOfShares() {&lt;br /&gt;  return numberOfShares;&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt; @RequiredStringValidator(type = ValidatorType.FIELD, message = " Number of Shares Required")&lt;/span&gt;&lt;br /&gt; public void setNumberOfShares(String numberOfShares) {&lt;br /&gt;  this.numberOfShares = numberOfShares;&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt;&lt;br /&gt; public String getPrice() {&lt;br /&gt;  return price;&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt; @RequiredStringValidator(type = ValidatorType.FIELD, message = "Price Required")&lt;/span&gt;&lt;br /&gt; public void setPrice(String price) {&lt;br /&gt;  this.price = price;&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt;&lt;br /&gt; public String getComission() {&lt;br /&gt;  return comission;&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt; @RequiredStringValidator(type = ValidatorType.FIELD, message = "Comission Required")&lt;/span&gt;&lt;br /&gt; public void setComission(String comission) {&lt;br /&gt;  this.comission = comission;&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt;&lt;br /&gt; public String getNotes() {&lt;br /&gt;  return notes;&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt;&lt;br /&gt; public void setNotes(String notes) {&lt;br /&gt;  this.notes = notes;&lt;br /&gt; }&lt;br /&gt;}&lt;/pre&gt;&lt;center&gt;AddTransactionAction.java&lt;/center&gt;&lt;br /&gt;Note:&lt;ul&gt;&lt;li&gt;The annotation &lt;span style="font-weight: bold;"&gt;@Validation&lt;/span&gt; is used to indicate that the current action might need validation. The validations on a method level can be skipped using the &lt;span style="font-weight: bold;"&gt;@SkipValidation&lt;/span&gt; annotation on the method.&lt;/li&gt;&lt;li&gt;The method noValidations() uses the &lt;span style="font-weight: bold;"&gt;@SkipValidation&lt;/span&gt; annotation, you can see this when you click on "Submit without validation" in the JSP&lt;/li&gt;&lt;li&gt;The &lt;span style="font-weight: bold;"&gt;@RequiredStringValidator&lt;/span&gt; annotation is used to indicate a Required Strint similar to the following xml rule&lt;pre&gt;&amp;lt;validators&amp;gt;&lt;br /&gt;&amp;lt;field name="numberOfShares"&amp;gt;&lt;br /&gt;&amp;lt;field-validator type="requiredstring"&amp;gt;&lt;br /&gt;    &amp;lt;message&amp;gt;Number of Share is required&amp;lt;/message&amp;gt;&lt;br /&gt;&amp;lt;/field-validator&amp;gt;&lt;br /&gt;&amp;lt;/field&amp;gt;&lt;br /&gt;&amp;lt;/validators&amp;gt;&lt;/pre&gt;&lt;/li&gt;&lt;li&gt;On the date field, I used a &lt;span style="font-weight: bold;"&gt;@RegexFieldValidator&lt;/span&gt; annotation, so that the date field will be mandated to have a given format.&lt;/li&gt;&lt;li&gt;&lt;span style="font-weight: bold;"&gt;Parameterized messages&lt;/span&gt;: You will notice that the message attribute of the @RegexFieldValidator is set to an empty string, while the key is set to a value. This is due to the fact that the message attribute is mandatory, and the &lt;span style="font-weight: bold;"&gt;key attribute is used to denote the message key from the properties files&lt;/span&gt;. &lt;span style="font-weight: bold;"&gt;The parameters can be retrieved in the properties files using the ${date} notation where the "date" variable is expected to available in the value stack&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;li&gt;Create a definition for the action in struts.xml&lt;pre&gt;&amp;lt;?xml version="1.0" encoding="UTF-8" ?&amp;gt;&lt;br /&gt;&amp;lt;!DOCTYPE struts PUBLIC&lt;br /&gt;"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"&lt;br /&gt;"http://struts.apache.org/dtds/struts-2.0.dtd"&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;struts&amp;gt;&lt;br /&gt;&amp;lt;package name="struts2Validation" extends="struts-default"&amp;gt;&lt;br /&gt; &amp;lt;action name="addTransaction"&lt;br /&gt;  class="actions.AddTransactionAction"&amp;gt;&lt;br /&gt;  &lt;span style="font-weight: bold;"&gt;&amp;lt;result name="success"&amp;gt;done.jsp&amp;lt;/result&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="font-weight: bold;"&gt;&amp;lt;result name="input"&amp;gt;transactions.jsp&amp;lt;/result&amp;gt;&lt;/span&gt;&lt;br /&gt; &amp;lt;/action&amp;gt;&lt;br /&gt;&amp;lt;/package&amp;gt;&lt;br /&gt;&amp;lt;/struts&amp;gt;&lt;/pre&gt;&lt;center&gt;struts.xml&lt;/center&gt;&lt;br /&gt;Note: The result with name "input" is because the validator returns the result to input when validation fails.&lt;/li&gt;&lt;li&gt;Create the properties file for messages&lt;pre&gt;date.error.message=Date &lt;span style="font-weight: bold;"&gt;${date}&lt;/span&gt; is not properly formatted.&lt;/pre&gt;&lt;center&gt;package.properties&lt;/center&gt;&lt;br /&gt;Note: In the properties file,  ${date} is used to retrieve the "date" value from the value stack, this is the way Struts 2 supports parameterization.&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Create the struts.properties file to set the theme to simple theme, so that you have more control how the UI components are laid out.&lt;pre&gt;struts.ui.theme=simple&lt;/pre&gt;&lt;center&gt;struts.properties&lt;/center&gt;&lt;/li&gt;&lt;/ol&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/18313336-2082069039796316292?l=java-x.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://java-x.blogspot.com/feeds/2082069039796316292/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://java-x.blogspot.com/2008/04/struts-2-validation-annotations.html#comment-form' title='8 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/18313336/posts/default/2082069039796316292'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/18313336/posts/default/2082069039796316292'/><link rel='alternate' type='text/html' href='http://java-x.blogspot.com/2008/04/struts-2-validation-annotations.html' title='Struts 2 Validation : Annotations'/><author><name>Abhilash Vuyyuru</name><uri>https://profiles.google.com/107328798079149921932</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh6.googleusercontent.com/-NxAgT10-nrk/AAAAAAAAAAI/AAAAAAAAB0o/Gz3VMjlEXb0/s512-c/photo.jpg'/></author><thr:total>8</thr:total></entry><entry><id>tag:blogger.com,1999:blog-18313336.post-1613562107752120177</id><published>2008-04-01T14:48:00.003-04:00</published><updated>2008-04-01T14:56:40.221-04:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='struts'/><category scheme='http://www.blogger.com/atom/ns#' term='example/sample code'/><title type='text'>Integrating Struts 2.0 and tiles</title><content type='html'>I am currently evaluating some web frameworks for a pet project and was trying to implement Struts 2 with tiles. Neither the Sturts 2 website, nor the tiles website gave an easy way to integrated Struts 2 and tiles. It took me a while to get them to work together. This post describes a way I figured out how to integrate Struts 2 with tiles. Struts 2 provides a plugin for integrating tiles 2. This plugin is included in the complete bundle (struts-2.x.x.x-all.zip). The following are are the steps needed to integrate struts2 with tiles.&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;&lt;a href="http://java-x.blogspot.com/2008/04/integrating-struts-20-and-tiles.html"&gt;Skip to Sample Code&lt;/a&gt;&lt;/span&gt;&lt;span class="fullpost"&gt;&lt;ol&gt;&lt;li&gt;Download the struts complete bundle from the &lt;a href="http://sturts.apache.org/2.x"&gt;struts 2 website&lt;/a&gt;&lt;/li&gt;&lt;li&gt;Download tiles 2 from &lt;a href="http://tiles.apache.org/download.html"&gt;tiles 2 website&lt;/a&gt;&lt;/li&gt;&lt;li&gt;Download the tiles dependencies from the &lt;a href="http://commons.apache.org/"&gt;jakarta commons site&lt;/a&gt;&lt;ul&gt;&lt;li&gt;Commons BeanUtils 1.7.0 or above&lt;/li&gt;&lt;li&gt;Commons Digester 1.8 or above&lt;/li&gt;&lt;li&gt;Commons Logging 1.1 or above&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;li&gt;Create the layout page, and related files (except the layout, all the other files are basic jsps )&lt;pre&gt;&amp;lt;%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %&amp;gt;&lt;br /&gt;&amp;lt;html&amp;gt;&lt;br /&gt;&amp;lt;head&amp;gt;&lt;br /&gt;&amp;lt;title&amp;gt;Insert title here&amp;lt;/title&amp;gt;&lt;br /&gt;&amp;lt;/head&amp;gt;&lt;br /&gt;&amp;lt;body&amp;gt;&lt;br /&gt;&amp;lt;table width="100%" height="100%"&amp;gt;&lt;br /&gt; &amp;lt;tr height="20%"&amp;gt;&lt;br /&gt;  &amp;lt;td colspan="2" align="center" bgcolor="skyblue"&amp;gt;&lt;br /&gt;  &lt;span style="font-weight: bold;"&gt;&amp;lt;tiles:insertAttribute name="header" /&amp;gt;&amp;lt;/td&amp;gt;&lt;/span&gt;&lt;br /&gt; &amp;lt;/tr&amp;gt;&lt;br /&gt; &amp;lt;tr&amp;gt;&lt;br /&gt;  &lt;span style="font-weight: bold;"&gt;&amp;lt;td bgcolor="cyan" width="75%"&amp;gt;&amp;lt;tiles:insertAttribute name="body" /&amp;gt;&amp;lt;/td&amp;gt;&lt;/span&gt;&lt;br /&gt; &amp;lt;/tr&amp;gt;&lt;br /&gt; &amp;lt;tr height="20%"&amp;gt;&lt;br /&gt;  &lt;span style="font-weight: bold;"&gt;&amp;lt;td colspan="2" align="center" bgcolor="skyblue"&amp;gt;&amp;lt;tiles:insertAttribute name="footer" /&amp;gt;&amp;lt;/td&amp;gt;&lt;/span&gt;&lt;br /&gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;&amp;lt;/table&amp;gt;&lt;br /&gt;&amp;lt;/body&amp;gt;&lt;br /&gt;&amp;lt;/html&amp;gt;&lt;/pre&gt;&lt;/li&gt;&lt;li&gt;Create the HelloWorld Action class&lt;pre&gt;package example;&lt;br /&gt;&lt;br /&gt;import com.opensymphony.xwork2.ActionSupport;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;public class HelloWorld extends ActionSupport {&lt;/span&gt;&lt;br /&gt;&lt;br /&gt; public String execute() throws Exception {&lt;br /&gt;  System.out.println("Hello World");&lt;br /&gt;  return SUCCESS;&lt;br /&gt; }&lt;br /&gt;}&lt;/pre&gt;&lt;/li&gt;&lt;li&gt;Configure the Web Deployment descriptor by adding a &lt;span style="font-weight: bold;"&gt;tiles listener&lt;/span&gt; to the web.xml file of your web application.&lt;pre&gt;&amp;lt;?xml version="1.0" encoding="UTF-8"?&amp;gt;&lt;br /&gt;&amp;lt;web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&lt;br /&gt; xmlns="http://java.sun.com/xml/ns/javaee"&lt;br /&gt; xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"&lt;br /&gt; xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"&lt;br /&gt; id="WebApp_ID" version="2.5"&amp;gt;&lt;br /&gt; &amp;lt;display-name&amp;gt;tilesTest&amp;lt;/display-name&amp;gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt; &amp;lt;listener&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;  &amp;lt;listener-class&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;   org.apache.struts2.tiles.StrutsTilesListener&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;  &amp;lt;/listener-class&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt; &amp;lt;/listener&amp;gt;&lt;/span&gt;&lt;br /&gt; &amp;lt;filter&amp;gt;&lt;br /&gt;  &amp;lt;filter-name&amp;gt;struts2&amp;lt;/filter-name&amp;gt;&lt;br /&gt;  &amp;lt;filter-class&amp;gt;&lt;br /&gt;   org.apache.struts2.dispatcher.FilterDispatcher&lt;br /&gt;  &amp;lt;/filter-class&amp;gt;&lt;br /&gt; &amp;lt;/filter&amp;gt;&lt;br /&gt; &amp;lt;filter-mapping&amp;gt;&lt;br /&gt;  &amp;lt;filter-name&amp;gt;struts2&amp;lt;/filter-name&amp;gt;&lt;br /&gt;  &amp;lt;url-pattern&amp;gt;/*&amp;lt;/url-pattern&amp;gt;&lt;br /&gt; &amp;lt;/filter-mapping&amp;gt;&lt;br /&gt; &amp;lt;welcome-file-list&amp;gt;&lt;br /&gt;  &amp;lt;welcome-file&amp;gt;index.html&amp;lt;/welcome-file&amp;gt;&lt;br /&gt; &amp;lt;/welcome-file-list&amp;gt;&lt;br /&gt;&amp;lt;/web-app&amp;gt;&lt;/pre&gt;&lt;/li&gt;&lt;li&gt;Configure struts to work with tiles, this can be done by either&lt;ol&gt;&lt;li&gt;Extending the sturts package from "tiles-default"&lt;pre style="font-weight: bold;"&gt;&amp;lt;package name="tilesTest" extends="tiles-default"&amp;gt;&lt;/pre&gt;&lt;/li&gt;&lt;span style="font-weight: bold;"&gt;OR&lt;/span&gt;&lt;li&gt;Declaring a new "result-type", tiles, that will map to "&lt;span style="font-weight: bold;"&gt;org.apache.struts2.views.tiles.TilesResult&lt;/span&gt;"&lt;pre style="font-weight: bold;"&gt;&amp;lt;result-types&amp;gt;&lt;br /&gt;&amp;lt;result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult" /&amp;gt;&lt;br /&gt;&amp;lt;/result-types&amp;gt;&lt;/pre&gt;&lt;/li&gt;&lt;/ol&gt;&lt;/li&gt;&lt;li&gt;Set the type of the results in the package to "tiles"&lt;pre style="font-weight: bold;"&gt;&amp;lt;result name="success" type="tiles"&amp;gt;helloworld.home&amp;lt;/result&amp;gt;&lt;/pre&gt;&lt;br /&gt; struts.xml&lt;pre&gt;&amp;lt;?xml version="1.0" encoding="UTF-8" ?&amp;gt;&lt;br /&gt;&amp;lt;!DOCTYPE struts PUBLIC&lt;br /&gt; "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"&lt;br /&gt; "http://struts.apache.org/dtds/struts-2.0.dtd"&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;struts&amp;gt;&lt;br /&gt; &amp;lt;package name="tilesTest" extends="struts-default"&amp;gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;  &amp;lt;result-types&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;   &amp;lt;result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult" /&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;  &amp;lt;/result-types&amp;gt;&lt;/span&gt;&lt;br /&gt;  &amp;lt;action name="helloWorld" class="example.HelloWorld"&amp;gt;&lt;br /&gt;   &lt;span style="font-weight: bold;"&gt;&amp;lt;result name="success" type="tiles"&amp;gt;helloworld.home&amp;lt;/result&amp;gt;&lt;/span&gt;&lt;br /&gt;  &amp;lt;/action&amp;gt;&lt;br /&gt; &amp;lt;/package&amp;gt;&lt;br /&gt;&amp;lt;/struts&amp;gt;&lt;/pre&gt;&lt;span style="font-weight: bold;"&gt;Note that the result "helloword.home" must match the definition name in tiles.xml file.&lt;/span&gt;&lt;/li&gt;&lt;br /&gt; &lt;li&gt;Create definitions for tiles in WEB-INF/tiles.xml file.&lt;pre&gt;&amp;lt;!DOCTYPE tiles-definitions PUBLIC&lt;br /&gt;    "-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"&lt;br /&gt;    "http://tiles.apache.org/dtds/tiles-config_2_0.dtd"&amp;gt;&lt;br /&gt;&amp;lt;tiles-definitions&amp;gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt; &amp;lt;definition name="helloworld.home" template="/layouts/layout.jsp"&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;  &amp;lt;put-attribute name="header" value="/layouts/header.jsp" /&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;  &amp;lt;put-attribute name="body" value="/index.html" /&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;  &amp;lt;put-attribute name="footer" value="/layouts/footer.jsp" /&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt; &amp;lt;/definition&amp;gt;&lt;/span&gt;&lt;br /&gt;&amp;lt;/tiles-definitions&amp;gt;&lt;/pre&gt;&lt;/li&gt;&lt;li&gt;The following is a list of jar files used for this example (copied to the WEB-INF/lib directory)&lt;ul&gt;&lt;li&gt;commons-beanutils.jar&lt;/li&gt;&lt;li&gt;commons-digester-1.8.jar&lt;/li&gt;&lt;li&gt;commons-logging-1.1.1.jar&lt;/li&gt;&lt;li&gt;freemarker-2.3.8.jar&lt;/li&gt;&lt;li&gt;ognl-2.6.11.jar&lt;/li&gt;&lt;li&gt;struts2-core-2.0.11.1.jar&lt;/li&gt;&lt;li&gt;struts2-tiles-plugin-2.0.11.1.jar&lt;/li&gt;&lt;li&gt;tiles-api-2.0.5.jar&lt;/li&gt;&lt;li&gt;tiles-core-2.0.5.jar&lt;/li&gt;&lt;li&gt;tiles-jsp-2.0.5.jar&lt;/li&gt;&lt;li&gt;xwork-2.0.4.jar&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;li&gt;This example was implemented on&lt;span style="font-weight: bold;"&gt; tomcat 6.0.16, with Java 5 update 11&lt;/span&gt;&lt;/li&gt;&lt;/ol&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/18313336-1613562107752120177?l=java-x.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://java-x.blogspot.com/feeds/1613562107752120177/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://java-x.blogspot.com/2008/04/integrating-struts-20-and-tiles.html#comment-form' title='8 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/18313336/posts/default/1613562107752120177'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/18313336/posts/default/1613562107752120177'/><link rel='alternate' type='text/html' href='http://java-x.blogspot.com/2008/04/integrating-struts-20-and-tiles.html' title='Integrating Struts 2.0 and tiles'/><author><name>Abhilash Vuyyuru</name><uri>https://profiles.google.com/107328798079149921932</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh6.googleusercontent.com/-NxAgT10-nrk/AAAAAAAAAAI/AAAAAAAAB0o/Gz3VMjlEXb0/s512-c/photo.jpg'/></author><thr:total>8</thr:total></entry><entry><id>tag:blogger.com,1999:blog-18313336.post-5618175943520543944</id><published>2008-03-28T14:59:00.005-04:00</published><updated>2008-03-28T15:05:43.677-04:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='flex'/><category scheme='http://www.blogger.com/atom/ns#' term='build'/><category scheme='http://www.blogger.com/atom/ns#' term='example/sample code'/><title type='text'>BlazeDS for Java-Flex communication</title><content type='html'>BlazeDS is a server-based Java remoting and web messaging technology that enables communication between back-end Java applications and Adobe Flex applications running in the browser. In this post, I describe a way (may not be the best) I was able to successfully to build a simple application using BlazeDS and Flex. The application is build using eclipse and ant, rather than using FlexBuilder. Following are the main steps that you have to follow to implement the example.&lt;ol&gt;&lt;li&gt;Install tomcat&lt;/li&gt;&lt;li&gt;Install Flex sdk&lt;/li&gt;&lt;li&gt;Download BlazeDS web application.&lt;/li&gt;&lt;li&gt;Create tomcat user with manager permisison&lt;/li&gt;&lt;li&gt;Create dynamic web project in eclipse&lt;/li&gt;&lt;li&gt;Create java file&lt;/li&gt;&lt;li&gt;Create mxml file&lt;/li&gt;&lt;li&gt;Update the config files in the WEB-INF/flex directory of the web application&lt;/li&gt;&lt;li&gt;Copy flexTasks.tasks to the root directory.&lt;/li&gt;&lt;li&gt;Create build file&lt;/li&gt;&lt;li&gt;Copy catalina-ant.jar and flextasks.jar into ant lib directory, add them to ant runtime in eclipse.&lt;/li&gt;&lt;li&gt;Build application using ant&lt;/li&gt;&lt;/ol&gt;&lt;a href="http://java-x.blogspot.com/2008/03/blazeds-for-java-flex-communication.html#sampleCode"&gt;Skip to Sample Code&lt;/a&gt;: Moving the mouse over the bolded code parts shows additional information&lt;span class="fullpost"&gt;&lt;a name="sampleCode"&gt;&lt;/a&gt;&lt;ol&gt;&lt;li&gt;&lt;u&gt;Install tomcat&lt;/u&gt;: This example was implemented on Tomcat 6.0.16&lt;/li&gt;&lt;li&gt;&lt;u&gt;Install Flex sdk&lt;/u&gt;: You can download the flex sdk from &lt;a href="http://opensource.adobe.com/flex/"&gt;here&lt;/a&gt; The example was implemented on Flex 3.0.0&lt;/li&gt;&lt;li&gt;&lt;u&gt;Download BlazeDS&lt;/u&gt;:Download the BlazeDS web application from &lt;a href="http://opensource.adobe.com/wiki/display/blazeds/BlazeDS"&gt;here&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;u&gt;Create tomcat user with manager permisison&lt;/u&gt;: In the &lt;i&gt;TOMCAT_HOME&lt;/i&gt;/tomcat-users.xml, add the following line&lt;pre&gt;&amp;lt;role rolename="manager"/&amp;gt;&lt;br /&gt;&amp;lt;user username="abhi" password="abhi" roles="manager"/&amp;gt;&lt;/pre&gt;&lt;/li&gt;&lt;li&gt;&lt;u&gt;Create dynamic web project in eclipse by importing the BlazeDS Web application war file.&lt;/u&gt;&lt;/li&gt;&lt;li&gt;&lt;u&gt;Create java file&lt;/u&gt;The java class simply echoes the data input in the textbox shown in the browser.&lt;pre&gt;package hello;&lt;br /&gt;public class HelloWorld {&lt;br /&gt; &lt;br /&gt; public String sayHelloTo(String str) {&lt;br /&gt;  System.out.println("Hello " + str);&lt;br /&gt;  return "Hello " + str;&lt;br /&gt; }&lt;br /&gt;}&lt;/pre&gt;&lt;/li&gt;&lt;li&gt;&lt;u&gt;Create mxml file&lt;/u&gt;The mxml file simply shows a text box and a submit button. A label will be displayed below the textbox with the word "Hello" appended to the input text.&lt;pre&gt;&amp;lt;?xml version="1.0" encoding="utf-8"?&amp;gt; &lt;br /&gt;&amp;lt;mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" viewSourceURL="srcview/index.html"&amp;gt;&lt;br /&gt; &amp;lt;mx:Script&amp;gt;&lt;br /&gt;  &amp;lt;![CDATA[&lt;br /&gt;&lt;br /&gt;   import mx.rpc.events.FaultEvent;&lt;br /&gt;   import mx.rpc.events.ResultEvent;&lt;br /&gt;   &lt;br /&gt;&lt;br /&gt;   [Bindable]&lt;br /&gt;   private var helloResult:String;&lt;br /&gt;   private function sayHelloTo():void {&lt;br /&gt;    ro.sayHelloTo(inputText.text);&lt;br /&gt;   }&lt;br /&gt;    private function resultHandler(event:ResultEvent):void&lt;br /&gt;   {&lt;br /&gt;    &lt;br /&gt;    helloResult = event.result as String;&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;  ]]&amp;gt;&lt;br /&gt; &amp;lt;/mx:Script&amp;gt; &lt;br /&gt;     &lt;b title="The destination name should match the destination defined in the remoting-config.xml file shown below"&gt;&amp;lt;mx:RemoteObject id="ro" destination="helloworld" result="resultHandler(event)" /&amp;gt;&lt;/b&gt; &lt;br /&gt;     &amp;lt;mx:HBox width="100%"&amp;gt;&lt;br /&gt;          &amp;lt;mx:TextInput id="inputText"/&amp;gt;&lt;br /&gt;          &amp;lt;mx:Button label="Submit" click="sayHelloTo()"/&amp;gt;&lt;br /&gt;     &amp;lt;/mx:HBox&amp;gt;&lt;br /&gt;     &amp;lt;mx:Label text="{helloResult}"/&amp;gt; &lt;br /&gt;&amp;lt;/mx:Application&amp;gt;&lt;/pre&gt;&lt;b&gt;Note&lt;/b&gt;: Defining the remote object enables you to make calls to the remote Java classes as if they are local action script classes.&lt;/li&gt;&lt;li&gt;&lt;u&gt;Update the configuration files&lt;/u&gt; For this example, you only have to update one config file, WEB-INF/flex/remoting-config.xml, to add a new destination, the HelloWorld class.&lt;pre&gt;&amp;lt;?xml version="1.0" encoding="UTF-8"?&amp;gt;&lt;br /&gt;&amp;lt;service id="remoting-service"&lt;br /&gt;    class="flex.messaging.services.RemotingService"&amp;gt;&lt;br /&gt;&lt;br /&gt;    &amp;lt;adapters&amp;gt;&lt;br /&gt;        &amp;lt;adapter-definition id="java-object" class="flex.messaging.services.remoting.adapters.JavaAdapter" default="true"/&amp;gt;&lt;br /&gt;    &amp;lt;/adapters&amp;gt;&lt;br /&gt;&lt;br /&gt;    &amp;lt;default-channels&amp;gt;&lt;br /&gt;        &amp;lt;channel ref="my-amf"/&amp;gt;&lt;br /&gt;    &amp;lt;/default-channels&amp;gt;&lt;br /&gt;    &lt;br /&gt;    &lt;b&gt;&amp;lt;destination id="helloworld"&amp;gt;&lt;br /&gt;     &amp;lt;properties&amp;gt;&lt;br /&gt;      &amp;lt;source&amp;gt;hello.HelloWorld&amp;lt;/source&amp;gt;&lt;br /&gt;     &amp;lt;/properties&amp;gt;&lt;br /&gt;    &amp;lt;/destination&amp;gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;    &lt;br /&gt;  &amp;lt;/service&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li&gt;&lt;u&gt;Copy flexTasks.tasks to the root directory.&lt;/u&gt;&lt;/li&gt;&lt;li&gt;&lt;u&gt;Create build file&lt;/u&gt;The ant build file uses two flex specific tasks, &lt;code&gt;mxmlc&lt;/code&gt; to compile the mxml file and &lt;code&gt;html-wrapper&lt;/code&gt; to create a html wrapper that contains the created swf file.&lt;pre&gt;&amp;lt;project name="helloWorldServer" default="build" basedir="."&amp;gt;&lt;br /&gt;&lt;br /&gt; &amp;lt;!-- ===================== Property Definitions =========================== --&amp;gt;&lt;br /&gt;&lt;br /&gt; &amp;lt;property file="build.properties" /&amp;gt;&lt;br /&gt; &amp;lt;property file="${user.home}/build.properties" /&amp;gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt; &amp;lt;!-- ==================== File and Directory Names ======================== --&amp;gt;&lt;br /&gt;&lt;br /&gt; &amp;lt;property name="app.name" value="helloWorldServer" /&amp;gt;&lt;br /&gt; &amp;lt;property name="app.path" value="/${app.name}" /&amp;gt;&lt;br /&gt; &amp;lt;property name="app.version" value="0.1-dev" /&amp;gt;&lt;br /&gt; &amp;lt;property name="build.home" value="${basedir}/build" /&amp;gt;&lt;br /&gt; &amp;lt;property name="catalina.home" value="C:/unzipped/apache-tomcat-6.0.16" /&amp;gt;&lt;br /&gt; &amp;lt;property name="dist.home" value="${basedir}/dist" /&amp;gt;&lt;br /&gt; &amp;lt;property name="docs.home" value="${basedir}/docs" /&amp;gt;&lt;br /&gt; &amp;lt;property name="manager.url" value="http://localhost:8080/manager" /&amp;gt;&lt;br /&gt; &amp;lt;property name="src.home" value="${basedir}/src" /&amp;gt;&lt;br /&gt; &amp;lt;property name="web.home" value="${basedir}/WebContent" /&amp;gt;&lt;br /&gt; &amp;lt;property name="manager.username" value="abhi" /&amp;gt;&lt;br /&gt; &amp;lt;property name="manager.password" value="abhi" /&amp;gt;&lt;br /&gt; &lt;b&gt;&amp;lt;property name="FLEX_HOME" value="C:/Adobe/Flex" /&amp;gt;&lt;br /&gt; &amp;lt;property name="APP_ROOT" value="${basedir}/WebContent" /&amp;gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt; &amp;lt;!-- ==================== External Dependencies =========================== --&amp;gt;&lt;br /&gt;&lt;br /&gt; &amp;lt;!-- ==================== Compilation Classpath =========================== --&amp;gt;&lt;br /&gt;&lt;br /&gt; &amp;lt;path id="compile.classpath"&amp;gt;&lt;br /&gt;  &amp;lt;fileset dir="${catalina.home}/bin"&amp;gt;&lt;br /&gt;   &amp;lt;include name="*.jar" /&amp;gt;&lt;br /&gt;  &amp;lt;/fileset&amp;gt;&lt;br /&gt;  &amp;lt;pathelement location="${catalina.home}/lib" /&amp;gt;&lt;br /&gt;  &amp;lt;fileset dir="${catalina.home}/lib"&amp;gt;&lt;br /&gt;   &amp;lt;include name="*.jar" /&amp;gt;&lt;br /&gt;  &amp;lt;/fileset&amp;gt;&lt;br /&gt;&lt;br /&gt; &amp;lt;/path&amp;gt;&lt;br /&gt;&lt;br /&gt; &amp;lt;!-- ================== Custom Ant Task Definitions ======================= --&amp;gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt; &amp;lt;taskdef resource="org/apache/catalina/ant/catalina.tasks" classpathref="compile.classpath" /&amp;gt;&lt;br /&gt; &lt;b title="flexTasks.tasks file should be in the root folder of the application as is the build file."&gt;&amp;lt;taskdef resource="flexTasks.tasks" classpath="c:/ant/lib/flexTasks.jar" /&amp;gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt; &amp;lt;!--  ==================== Compilation Control Options ==================== --&amp;gt;&lt;br /&gt;&lt;br /&gt; &amp;lt;property name="compile.debug" value="true" /&amp;gt;&lt;br /&gt; &amp;lt;property name="compile.deprecation" value="false" /&amp;gt;&lt;br /&gt; &amp;lt;property name="compile.optimize" value="true" /&amp;gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt; &amp;lt;!-- ==================== All Target ====================================== --&amp;gt;&lt;br /&gt;&lt;br /&gt; &amp;lt;target name="all" depends="clean,build" description="Clean build and dist directories, then compile" /&amp;gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt; &amp;lt;!-- ==================== Clean Target ==================================== --&amp;gt;&lt;br /&gt;&lt;br /&gt; &amp;lt;target name="clean" description="Delete old build and dist directories"&amp;gt;&lt;br /&gt;  &amp;lt;delete dir="${build.home}" /&amp;gt;&lt;br /&gt;  &amp;lt;delete dir="${dist.home}" /&amp;gt;&lt;br /&gt; &amp;lt;/target&amp;gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt; &amp;lt;!-- ==================== Compile Target ================================== --&amp;gt;&lt;br /&gt;&lt;br /&gt; &amp;lt;target name="build" depends="prepare" description="Compile Java sources"&amp;gt;&lt;br /&gt;&lt;br /&gt;  &amp;lt;!-- Compile Java classes as necessary --&amp;gt;&lt;br /&gt;  &amp;lt;mkdir dir="${build.home}/WEB-INF/classes" /&amp;gt;&lt;br /&gt;  &amp;lt;javac srcdir="${src.home}" destdir="${build.home}/WEB-INF/classes" debug="${compile.debug}" deprecation="${compile.deprecation}" optimize="${compile.optimize}"&amp;gt;&lt;br /&gt;   &amp;lt;classpath refid="compile.classpath" /&amp;gt;&lt;br /&gt;  &amp;lt;/javac&amp;gt;&lt;br /&gt;&lt;br /&gt;  &amp;lt;!-- Copy application resources --&amp;gt;&lt;br /&gt;  &amp;lt;copy todir="${build.home}/WEB-INF/classes"&amp;gt;&lt;br /&gt;   &amp;lt;fileset dir="${src.home}" excludes="**/*.java" /&amp;gt;&lt;br /&gt;  &amp;lt;/copy&amp;gt;&lt;br /&gt;&lt;br /&gt; &amp;lt;/target&amp;gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt; &amp;lt;!-- ==================== Dist Target ===================================== --&amp;gt;&lt;br /&gt;&lt;br /&gt; &amp;lt;target name="dist" depends="build,javadoc" description="Create binary distribution"&amp;gt;&lt;br /&gt;&lt;br /&gt;  &amp;lt;!-- Copy documentation subdirectories --&amp;gt;&lt;br /&gt;  &amp;lt;mkdir dir="${dist.home}/docs" /&amp;gt;&lt;br /&gt;  &amp;lt;copy todir="${dist.home}/docs"&amp;gt;&lt;br /&gt;   &amp;lt;fileset dir="${docs.home}" /&amp;gt;&lt;br /&gt;  &amp;lt;/copy&amp;gt;&lt;br /&gt;&lt;br /&gt;  &amp;lt;!-- Create application JAR file --&amp;gt;&lt;br /&gt;  &amp;lt;jar jarfile="${dist.home}/${app.name}-${app.version}.war" basedir="${build.home}" /&amp;gt;&lt;br /&gt;&lt;br /&gt;  &amp;lt;!-- Copy additional files to ${dist.home} as necessary --&amp;gt;&lt;br /&gt;&lt;br /&gt; &amp;lt;/target&amp;gt;&lt;br /&gt;&lt;br /&gt; &amp;lt;!-- ==================== Flex targets ==================================== --&amp;gt;&lt;br /&gt;&lt;br /&gt; &lt;b&gt;&amp;lt;target name="appcompile" depends="install"&amp;gt;&lt;/b&gt;&lt;br /&gt;  &lt;b&gt;&amp;lt;mxmlc file="${APP_ROOT}/helloWorld.mxml" context-root="/helloWorldServer" keep-generated-actionscript="true" services="${web.home}/WEB-INF/flex/services-config.xml" output="${catalina.home}/webapps/${app.name}/helloWorld.swf"&amp;gt;&lt;br /&gt;   &amp;lt;compiler.library-path dir="${FLEX_HOME}/frameworks" append="true"&amp;gt;&lt;br /&gt;    &amp;lt;include name="${web.home}/WEB-INF/lib/" /&amp;gt;&lt;br /&gt;   &amp;lt;/compiler.library-path&amp;gt;&lt;br /&gt;   &amp;lt;load-config filename="${FLEX_HOME}/frameworks/flex-config.xml" /&amp;gt;&lt;br /&gt;   &amp;lt;source-path path-element="${FLEX_HOME}/frameworks" /&amp;gt;&lt;br /&gt;  &amp;lt;/mxmlc&amp;gt;&lt;br /&gt;&lt;br /&gt; &amp;lt;/target&amp;gt;&lt;br /&gt;&lt;br /&gt; &amp;lt;target name="createHtmlWrapper" depends="appcompile"&amp;gt;&lt;br /&gt;  &amp;lt;html-wrapper application="${APP_ROOT}/helloWorld.mxml" output="${catalina.home}/webapps/${app.name}" swf="helloWorld" /&amp;gt;&lt;br /&gt; &amp;lt;/target&amp;gt;&lt;/b&gt;&lt;br /&gt; &lt;br /&gt;&lt;br /&gt; &amp;lt;!-- ==================== Install Target ================================== --&amp;gt;&lt;br /&gt;&lt;br /&gt; &amp;lt;target name="install" depends="build" description="Install application to servlet container"&amp;gt;&lt;br /&gt;&lt;br /&gt;  &amp;lt;deploy url="${manager.url}" username="${manager.username}" password="${manager.password}" path="${app.path}" localWar="file://${build.home}" /&amp;gt;&lt;br /&gt;&lt;br /&gt; &amp;lt;/target&amp;gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt; &amp;lt;!-- ==================== Javadoc Target ================================== --&amp;gt;&lt;br /&gt;&lt;br /&gt; &amp;lt;target name="javadoc" depends="build" description="Create Javadoc API documentation"&amp;gt;&lt;br /&gt;&lt;br /&gt;  &amp;lt;mkdir dir="${dist.home}/docs/api" /&amp;gt;&lt;br /&gt;  &amp;lt;javadoc sourcepath="${src.home}" destdir="${dist.home}/docs/api" packagenames="*"&amp;gt;&lt;br /&gt;   &amp;lt;classpath refid="compile.classpath" /&amp;gt;&lt;br /&gt;  &amp;lt;/javadoc&amp;gt;&lt;br /&gt;&lt;br /&gt; &amp;lt;/target&amp;gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt; &amp;lt;!-- ====================== List Target =================================== --&amp;gt;&lt;br /&gt;&lt;br /&gt; &amp;lt;target name="list" description="List installed applications on servlet container"&amp;gt;&lt;br /&gt;&lt;br /&gt;  &amp;lt;list url="${manager.url}" username="${manager.username}" password="${manager.password}" /&amp;gt;&lt;br /&gt;&lt;br /&gt; &amp;lt;/target&amp;gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt; &amp;lt;!-- ==================== Prepare Target ================================== --&amp;gt;&lt;br /&gt;&lt;br /&gt; &amp;lt;target name="prepare"&amp;gt;&lt;br /&gt;&lt;br /&gt;  &amp;lt;!-- Create build directories as needed --&amp;gt;&lt;br /&gt;  &amp;lt;mkdir dir="${build.home}" /&amp;gt;&lt;br /&gt;  &amp;lt;mkdir dir="${build.home}/WEB-INF" /&amp;gt;&lt;br /&gt;  &amp;lt;mkdir dir="${build.home}/WEB-INF/classes" /&amp;gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;  &amp;lt;!-- Copy static content of this web application --&amp;gt;&lt;br /&gt;  &amp;lt;copy todir="${build.home}"&amp;gt;&lt;br /&gt;   &amp;lt;fileset dir="${web.home}" /&amp;gt;&lt;br /&gt;  &amp;lt;/copy&amp;gt;&lt;br /&gt;&lt;br /&gt;  &amp;lt;!-- Copy external dependencies as required --&amp;gt;&lt;br /&gt;  &amp;lt;mkdir dir="${build.home}/WEB-INF/lib" /&amp;gt;&lt;br /&gt;&lt;br /&gt;  &amp;lt;!-- Copy static files from external dependencies as needed --&amp;gt;&lt;br /&gt;  &amp;lt;!-- *** CUSTOMIZE HERE AS REQUIRED BY YOUR APPLICATION *** --&amp;gt;&lt;br /&gt;&lt;br /&gt; &amp;lt;/target&amp;gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt; &amp;lt;!-- ==================== Reload Target =================================== --&amp;gt;&lt;br /&gt;&lt;br /&gt; &amp;lt;target name="reload" depends="build" description="Reload application on servlet container"&amp;gt;&lt;br /&gt;&lt;br /&gt;  &amp;lt;reload url="${manager.url}" username="${manager.username}" password="${manager.password}" path="${app.path}" /&amp;gt;&lt;br /&gt;&lt;br /&gt; &amp;lt;/target&amp;gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt; &amp;lt;!-- ==================== Remove Target =================================== --&amp;gt;&lt;br /&gt;&lt;br /&gt; &amp;lt;target name="remove" description="Remove application on servlet container"&amp;gt;&lt;br /&gt;&lt;br /&gt;  &amp;lt;undeploy url="${manager.url}" username="${manager.username}" password="${manager.password}" path="${app.path}" /&amp;gt;&lt;br /&gt;&lt;br /&gt; &amp;lt;/target&amp;gt;&lt;br /&gt;&amp;lt;/project&amp;gt;&lt;/pre&gt;&lt;/li&gt;&lt;li&gt;&lt;u&gt;Addtional ant configuration: &lt;/u&gt;Copy catalina-ant.jar and flextasks.jar into ant lib directory, add them to ant runtime in eclipse.&lt;/li&gt;&lt;li&gt;To build and install the file, simply run the createHtmlWrapper target in ant&lt;/li&gt;&lt;/ol&gt;References&lt;ul&gt;&lt;li&gt;The ant build file was created using the base build.xml file available on &lt;a href="http://tomcat.apache.org/tomcat-6.0-doc/appdev/build.xml.txt"&gt;apache tomcat website&lt;/a&gt;.&lt;/li&gt;&lt;li&gt;The config files are used as is available with the BlazeDS service.&lt;/li&gt;&lt;/ul&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/18313336-5618175943520543944?l=java-x.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://java-x.blogspot.com/feeds/5618175943520543944/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://java-x.blogspot.com/2008/03/blazeds-for-java-flex-communication.html#comment-form' title='9 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/18313336/posts/default/5618175943520543944'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/18313336/posts/default/5618175943520543944'/><link rel='alternate' type='text/html' href='http://java-x.blogspot.com/2008/03/blazeds-for-java-flex-communication.html' title='BlazeDS for Java-Flex communication'/><author><name>Abhilash Vuyyuru</name><uri>https://profiles.google.com/107328798079149921932</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh6.googleusercontent.com/-NxAgT10-nrk/AAAAAAAAAAI/AAAAAAAAB0o/Gz3VMjlEXb0/s512-c/photo.jpg'/></author><thr:total>9</thr:total></entry><entry><id>tag:blogger.com,1999:blog-18313336.post-1211008463893426930</id><published>2007-08-23T12:40:00.000-04:00</published><updated>2007-08-23T12:51:33.221-04:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='news'/><title type='text'>Star Office now free</title><content type='html'>Google is going to offer Sun's Star Office for free as a part of &lt;a href="http://pack.google.com"&gt;Google Pack&lt;/a&gt;. Also, Sun will now be traded under the Symbol JAVA replacing the old symbol SUNW.&lt;br /&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;&lt;a href="http://bits.blogs.nytimes.com/2007/08/14/google-goes-after-microsoft-again/"&gt;Google Goes After Microsoft Again&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://blogs.sun.com/jonathan/entry/java_is_everywhere"&gt;The Rise of JAVA - The Retirement of SUNW&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.theregister.com/2007/08/23/sun_no_sunw_java/"&gt;Sun boots workstation roots in favor of JAVA ticker&lt;/a&gt;&lt;/li&gt;&lt;li&gt;On Slashdot - &lt;a href="http://slashdot.org/articles/07/08/12/1634208.shtml"&gt;Google Pack Adds StarOffice&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/18313336-1211008463893426930?l=java-x.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://java-x.blogspot.com/feeds/1211008463893426930/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://java-x.blogspot.com/2007/08/star-office-now-free.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/18313336/posts/default/1211008463893426930'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/18313336/posts/default/1211008463893426930'/><link rel='alternate' type='text/html' href='http://java-x.blogspot.com/2007/08/star-office-now-free.html' title='Star Office now free'/><author><name>Abhilash Vuyyuru</name><uri>https://profiles.google.com/107328798079149921932</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh6.googleusercontent.com/-NxAgT10-nrk/AAAAAAAAAAI/AAAAAAAAB0o/Gz3VMjlEXb0/s512-c/photo.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-18313336.post-7663901502559146821</id><published>2007-08-20T15:26:00.000-04:00</published><updated>2007-08-20T15:29:05.527-04:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='spring'/><category scheme='http://www.blogger.com/atom/ns#' term='ajax'/><category scheme='http://www.blogger.com/atom/ns#' term='security'/><category scheme='http://www.blogger.com/atom/ns#' term='example/sample code'/><title type='text'>Handling Security with Ajax, DWR and Acegi</title><content type='html'>This is an extension of a previous post that described how to secure your method calls using Acegi security. Here, I will go through how to secure your Asynchronous calls,  using the same example with some modifications to include Ajax calls using Direct Web Remoting (DWR).&lt;span class="fullpost"&gt;&lt;ol&gt;&lt;li&gt;Create the example project as shown in "&lt;a href="http://java-x.blogspot.com/2006/12/spring-security-with-acegi-security.html"&gt;Spring security with Acegi Security Framework&lt;/a&gt;". This will be the starting point.&lt;/li&gt;&lt;li&gt;Create the SecureDAO object.&lt;pre&gt;package test;&lt;br /&gt;&lt;br /&gt;public class SecureDAO {&lt;br /&gt;  public String create() {&lt;br /&gt;    System.out.println("Create");&lt;br /&gt;    return "create";&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  public String read() {&lt;br /&gt;    System.out.println("read");&lt;br /&gt;    return "read";&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  public String update() {&lt;br /&gt;    System.out.println("update");&lt;br /&gt;    return "update";&lt;br /&gt;  }&lt;br /&gt;}&lt;/pre&gt;&lt;/li&gt;&lt;li&gt;Update the applicationContext.xml file to include the security definitions by adding the following bean definitions as shown below&lt;pre&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot;?&amp;gt;&lt;br /&gt;&amp;lt;!DOCTYPE beans PUBLIC &amp;quot;-//SPRING//DTD BEAN//EN&amp;quot; &amp;quot;http://www.springframework.org/dtd/spring-beans.dtd&amp;quot;&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;beans&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;bean id=&amp;quot;filterChainProxy&amp;quot; class=&amp;quot;org.acegisecurity.util.FilterChainProxy&amp;quot;&amp;gt;&lt;br /&gt; &amp;lt;property name=&amp;quot;filterInvocationDefinitionSource&amp;quot;&amp;gt;&lt;br /&gt;  &amp;lt;value&amp;gt;&lt;br /&gt;   CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON&lt;br /&gt;   PATTERN_TYPE_APACHE_ANT&lt;br /&gt;   /**=httpSessionContextIntegrationFilter,logoutFilter,authenticationProcessingFilter,securityContextHolderAwareRequestFilter,rememberMeProcessingFilter,anonymousProcessingFilter,exceptionTranslationFilter,filterInvocationInterceptor&lt;br /&gt;  &amp;lt;/value&amp;gt;&lt;br /&gt; &amp;lt;/property&amp;gt;&lt;br /&gt;&amp;lt;/bean&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;bean id=&amp;quot;httpSessionContextIntegrationFilter&amp;quot; class=&amp;quot;org.acegisecurity.context.HttpSessionContextIntegrationFilter&amp;quot;/&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;bean id=&amp;quot;logoutFilter&amp;quot; class=&amp;quot;org.acegisecurity.ui.logout.LogoutFilter&amp;quot;&amp;gt;&lt;br /&gt; &amp;lt;constructor-arg value=&amp;quot;/index.jsp&amp;quot;/&amp;gt;&lt;br /&gt; &amp;lt;constructor-arg&amp;gt;&lt;br /&gt;  &amp;lt;list&amp;gt;&lt;br /&gt;   &amp;lt;ref bean=&amp;quot;rememberMeServices&amp;quot;/&amp;gt;&lt;br /&gt;   &amp;lt;bean class=&amp;quot;org.acegisecurity.ui.logout.SecurityContextLogoutHandler&amp;quot;/&amp;gt;&lt;br /&gt;  &amp;lt;/list&amp;gt;&lt;br /&gt; &amp;lt;/constructor-arg&amp;gt;&lt;br /&gt;&amp;lt;/bean&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;bean id=&amp;quot;authenticationProcessingFilter&amp;quot; class=&amp;quot;org.acegisecurity.ui.webapp.AuthenticationProcessingFilter&amp;quot;&amp;gt;&lt;br /&gt; &amp;lt;property name=&amp;quot;authenticationManager&amp;quot; ref=&amp;quot;authenticationManager&amp;quot;/&amp;gt;&lt;br /&gt; &amp;lt;property name=&amp;quot;authenticationFailureUrl&amp;quot; value=&amp;quot;/login.jsp?errorId=1&amp;quot;/&amp;gt;&lt;br /&gt; &amp;lt;property name=&amp;quot;defaultTargetUrl&amp;quot; value=&amp;quot;/&amp;quot;/&amp;gt;&lt;br /&gt; &amp;lt;property name=&amp;quot;filterProcessesUrl&amp;quot; value=&amp;quot;/j_acegi_security_check&amp;quot;/&amp;gt;&lt;br /&gt; &amp;lt;property name=&amp;quot;rememberMeServices&amp;quot; ref=&amp;quot;rememberMeServices&amp;quot;/&amp;gt;&lt;br /&gt;&amp;lt;/bean&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;bean id=&amp;quot;securityContextHolderAwareRequestFilter&amp;quot; class=&amp;quot;org.acegisecurity.wrapper.SecurityContextHolderAwareRequestFilter&amp;quot;/&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;bean id=&amp;quot;rememberMeProcessingFilter&amp;quot; class=&amp;quot;org.acegisecurity.ui.rememberme.RememberMeProcessingFilter&amp;quot;&amp;gt;&lt;br /&gt; &amp;lt;property name=&amp;quot;authenticationManager&amp;quot; ref=&amp;quot;authenticationManager&amp;quot;/&amp;gt;&lt;br /&gt; &amp;lt;property name=&amp;quot;rememberMeServices&amp;quot; ref=&amp;quot;rememberMeServices&amp;quot;/&amp;gt;&lt;br /&gt;&amp;lt;/bean&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;bean id=&amp;quot;anonymousProcessingFilter&amp;quot; class=&amp;quot;org.acegisecurity.providers.anonymous.AnonymousProcessingFilter&amp;quot;&amp;gt;&lt;br /&gt; &amp;lt;property name=&amp;quot;key&amp;quot; value=&amp;quot;changeThis&amp;quot;/&amp;gt;&lt;br /&gt; &amp;lt;property name=&amp;quot;userAttribute&amp;quot; value=&amp;quot;anonymousUser,ROLE_ANONYMOUS&amp;quot;/&amp;gt;&lt;br /&gt;&amp;lt;/bean&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;bean id=&amp;quot;exceptionTranslationFilter&amp;quot; class=&amp;quot;org.acegisecurity.ui.ExceptionTranslationFilter&amp;quot;&amp;gt;&lt;br /&gt; &amp;lt;property name=&amp;quot;authenticationEntryPoint&amp;quot;&amp;gt;&lt;br /&gt;  &amp;lt;bean class=&amp;quot;org.acegisecurity.ui.webapp.AuthenticationProcessingFilterEntryPoint&amp;quot;&amp;gt;&lt;br /&gt;   &amp;lt;property name=&amp;quot;loginFormUrl&amp;quot; value=&amp;quot;/login.jsp&amp;quot;/&amp;gt;&lt;br /&gt;   &amp;lt;property name=&amp;quot;forceHttps&amp;quot; value=&amp;quot;false&amp;quot;/&amp;gt;&lt;br /&gt;  &amp;lt;/bean&amp;gt;&lt;br /&gt; &amp;lt;/property&amp;gt;&lt;br /&gt; &amp;lt;property name=&amp;quot;accessDeniedHandler&amp;quot;&amp;gt;&lt;br /&gt;  &amp;lt;bean class=&amp;quot;org.acegisecurity.ui.AccessDeniedHandlerImpl&amp;quot;&amp;gt;&lt;br /&gt;   &amp;lt;property name=&amp;quot;errorPage&amp;quot; value=&amp;quot;/denied.jsp&amp;quot;/&amp;gt;&lt;br /&gt;  &amp;lt;/bean&amp;gt;&lt;br /&gt; &amp;lt;/property&amp;gt;&lt;br /&gt;&amp;lt;/bean&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;bean id=&amp;quot;filterInvocationInterceptor&amp;quot; class=&amp;quot;org.acegisecurity.intercept.web.FilterSecurityInterceptor&amp;quot;&amp;gt;&lt;br /&gt; &amp;lt;property name=&amp;quot;authenticationManager&amp;quot; ref=&amp;quot;authenticationManager&amp;quot;/&amp;gt;&lt;br /&gt; &amp;lt;property name=&amp;quot;accessDecisionManager&amp;quot;&amp;gt;&lt;br /&gt;  &amp;lt;bean class=&amp;quot;org.acegisecurity.vote.AffirmativeBased&amp;quot;&amp;gt;&lt;br /&gt;   &amp;lt;property name=&amp;quot;allowIfAllAbstainDecisions&amp;quot; value=&amp;quot;false&amp;quot;/&amp;gt;&lt;br /&gt;   &amp;lt;property name=&amp;quot;decisionVoters&amp;quot;&amp;gt;&lt;br /&gt;    &amp;lt;list&amp;gt;&lt;br /&gt;     &amp;lt;bean class=&amp;quot;org.acegisecurity.vote.RoleVoter&amp;quot;/&amp;gt;&lt;br /&gt;     &amp;lt;bean class=&amp;quot;org.acegisecurity.vote.AuthenticatedVoter&amp;quot;/&amp;gt;&lt;br /&gt;    &amp;lt;/list&amp;gt;&lt;br /&gt;   &amp;lt;/property&amp;gt;&lt;br /&gt;  &amp;lt;/bean&amp;gt;&lt;br /&gt; &amp;lt;/property&amp;gt;&lt;br /&gt; &amp;lt;property name=&amp;quot;objectDefinitionSource&amp;quot;&amp;gt;&lt;br /&gt;  &amp;lt;value&amp;gt;&lt;br /&gt;   CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON&lt;br /&gt;   PATTERN_TYPE_APACHE_ANT&lt;br /&gt;   /secure/admin/**=ROLE_ADMIN&lt;br /&gt;   /secure/**=IS_AUTHENTICATED_REMEMBERED&lt;br /&gt;   /**=IS_AUTHENTICATED_ANONYMOUSLY&lt;br /&gt;  &amp;lt;/value&amp;gt;&lt;br /&gt; &amp;lt;/property&amp;gt;&lt;br /&gt;&amp;lt;/bean&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;bean id=&amp;quot;rememberMeServices&amp;quot; class=&amp;quot;org.acegisecurity.ui.rememberme.TokenBasedRememberMeServices&amp;quot;&amp;gt;&lt;br /&gt; &amp;lt;property name=&amp;quot;userDetailsService&amp;quot; ref=&amp;quot;userDetailsService&amp;quot;/&amp;gt;&lt;br /&gt; &amp;lt;property name=&amp;quot;tokenValiditySeconds&amp;quot; value=&amp;quot;1800&amp;quot;&amp;gt;&amp;lt;/property&amp;gt;&lt;br /&gt; &amp;lt;property name=&amp;quot;key&amp;quot; value=&amp;quot;changeThis&amp;quot;/&amp;gt;&lt;br /&gt;&amp;lt;/bean&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;bean id=&amp;quot;authenticationManager&amp;quot; class=&amp;quot;org.acegisecurity.providers.ProviderManager&amp;quot;&amp;gt;&lt;br /&gt; &amp;lt;property name=&amp;quot;providers&amp;quot;&amp;gt;&lt;br /&gt;  &amp;lt;list&amp;gt;&lt;br /&gt;   &amp;lt;ref local=&amp;quot;daoAuthenticationProvider&amp;quot;/&amp;gt;&lt;br /&gt;   &amp;lt;bean class=&amp;quot;org.acegisecurity.providers.anonymous.AnonymousAuthenticationProvider&amp;quot;&amp;gt;&lt;br /&gt;    &amp;lt;property name=&amp;quot;key&amp;quot; value=&amp;quot;changeThis&amp;quot;/&amp;gt;&lt;br /&gt;   &amp;lt;/bean&amp;gt;&lt;br /&gt;   &amp;lt;bean class=&amp;quot;org.acegisecurity.providers.rememberme.RememberMeAuthenticationProvider&amp;quot;&amp;gt;&lt;br /&gt;    &amp;lt;property name=&amp;quot;key&amp;quot; value=&amp;quot;changeThis&amp;quot;/&amp;gt;&lt;br /&gt;   &amp;lt;/bean&amp;gt;&lt;br /&gt;  &amp;lt;/list&amp;gt;&lt;br /&gt; &amp;lt;/property&amp;gt;&lt;br /&gt;&amp;lt;/bean&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;bean id=&amp;quot;daoAuthenticationProvider&amp;quot; class=&amp;quot;org.acegisecurity.providers.dao.DaoAuthenticationProvider&amp;quot;&amp;gt;&lt;br /&gt; &amp;lt;property name=&amp;quot;userDetailsService&amp;quot; ref=&amp;quot;userDetailsService&amp;quot;/&amp;gt;&lt;br /&gt; &amp;lt;property name=&amp;quot;userCache&amp;quot;&amp;gt;&lt;br /&gt;  &amp;lt;bean class=&amp;quot;org.acegisecurity.providers.dao.cache.EhCacheBasedUserCache&amp;quot;&amp;gt;&lt;br /&gt;   &amp;lt;property name=&amp;quot;cache&amp;quot;&amp;gt;&lt;br /&gt;    &amp;lt;bean class=&amp;quot;org.springframework.cache.ehcache.EhCacheFactoryBean&amp;quot;&amp;gt;&lt;br /&gt;     &amp;lt;property name=&amp;quot;cacheManager&amp;quot;&amp;gt;&lt;br /&gt;      &amp;lt;bean class=&amp;quot;org.springframework.cache.ehcache.EhCacheManagerFactoryBean&amp;quot;/&amp;gt;&lt;br /&gt;     &amp;lt;/property&amp;gt;&lt;br /&gt;     &amp;lt;property name=&amp;quot;cacheName&amp;quot; value=&amp;quot;userCache&amp;quot;/&amp;gt;&lt;br /&gt;    &amp;lt;/bean&amp;gt;&lt;br /&gt;   &amp;lt;/property&amp;gt;&lt;br /&gt;  &amp;lt;/bean&amp;gt;&lt;br /&gt; &amp;lt;/property&amp;gt;&lt;br /&gt;&amp;lt;/bean&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;bean id=&amp;quot;userDetailsService&amp;quot; class=&amp;quot;org.acegisecurity.userdetails.memory.InMemoryDaoImpl&amp;quot;&amp;gt;&lt;br /&gt; &amp;lt;property name=&amp;quot;userProperties&amp;quot;&amp;gt;&lt;br /&gt;  &amp;lt;bean class=&amp;quot;org.springframework.beans.factory.config.PropertiesFactoryBean&amp;quot;&amp;gt;&lt;br /&gt;   &amp;lt;property name=&amp;quot;location&amp;quot; value=&amp;quot;/WEB-INF/users.properties&amp;quot;/&amp;gt;&lt;br /&gt;  &amp;lt;/bean&amp;gt;&lt;br /&gt; &amp;lt;/property&amp;gt;&lt;br /&gt;&amp;lt;/bean&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;bean id=&amp;quot;loggerListener&amp;quot; class=&amp;quot;org.acegisecurity.event.authentication.LoggerListener&amp;quot;/&amp;gt;&lt;br /&gt;&lt;br /&gt; &amp;lt;bean id=&amp;quot;methodSecurityInterceptor&amp;quot; class=&amp;quot;org.acegisecurity.intercept.method.aopalliance.MethodSecurityInterceptor&amp;quot;&amp;gt;&lt;br /&gt;  &amp;lt;property name=&amp;quot;authenticationManager&amp;quot;&amp;gt;&lt;br /&gt;   &amp;lt;ref bean=&amp;quot;authenticationManager&amp;quot; /&amp;gt;&lt;br /&gt;  &amp;lt;/property&amp;gt;&lt;br /&gt;  &amp;lt;property name=&amp;quot;accessDecisionManager&amp;quot;&amp;gt;&lt;br /&gt;   &amp;lt;bean class=&amp;quot;org.acegisecurity.vote.AffirmativeBased&amp;quot;&amp;gt;&lt;br /&gt;    &amp;lt;property name=&amp;quot;allowIfAllAbstainDecisions&amp;quot; value=&amp;quot;false&amp;quot; /&amp;gt;&lt;br /&gt;    &amp;lt;property name=&amp;quot;decisionVoters&amp;quot;&amp;gt;&lt;br /&gt;     &amp;lt;list&amp;gt;&lt;br /&gt;      &amp;lt;bean class=&amp;quot;org.acegisecurity.vote.RoleVoter&amp;quot; /&amp;gt;&lt;br /&gt;      &amp;lt;bean class=&amp;quot;org.acegisecurity.vote.AuthenticatedVoter&amp;quot; /&amp;gt;&lt;br /&gt;     &amp;lt;/list&amp;gt;&lt;br /&gt;    &amp;lt;/property&amp;gt;&lt;br /&gt;   &amp;lt;/bean&amp;gt;&lt;br /&gt;  &amp;lt;/property&amp;gt;&lt;br /&gt;&lt;b&gt;  &amp;lt;property name=&amp;quot;objectDefinitionSource&amp;quot;&amp;gt;&lt;br /&gt;   &amp;lt;value&amp;gt;&lt;br /&gt;   test.SecureDAO.*=IS_AUTHENTICATED_REMEMBERED &lt;br /&gt;   test.SecureDAO.u=ROLE_ADMIN&lt;br /&gt;   &amp;lt;/value&amp;gt;&lt;br /&gt;  &amp;lt;/property&amp;gt;&lt;br /&gt; &lt;/b&gt;&amp;lt;/bean&amp;gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt; &amp;lt;bean id=&amp;quot;autoProxyCreator&amp;quot; class=&amp;quot;org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator&amp;quot;&amp;gt;&lt;br /&gt;  &amp;lt;property name=&amp;quot;interceptorNames&amp;quot;&amp;gt;&lt;br /&gt;   &amp;lt;list&amp;gt;&lt;br /&gt;   &amp;lt;value&amp;gt;methodSecurityInterceptor&amp;lt;/value&amp;gt;&lt;br /&gt;   &amp;lt;/list&amp;gt;&lt;br /&gt;  &amp;lt;/property&amp;gt;&lt;br /&gt;  &amp;lt;property name=&amp;quot;beanNames&amp;quot;&amp;gt;&lt;br /&gt;   &amp;lt;list&amp;gt;&lt;br /&gt;   &amp;lt;value&amp;gt;secureDAO&amp;lt;/value&amp;gt;&lt;br /&gt;   &amp;lt;/list&amp;gt;&lt;br /&gt;  &amp;lt;/property&amp;gt;&lt;br /&gt; &amp;lt;/bean&amp;gt;&lt;br /&gt; &lt;br /&gt; &amp;lt;bean id=&amp;quot;secureDAO&amp;quot; class=&amp;quot;test.SecureDAO&amp;quot; /&amp;gt;&lt;br /&gt;&lt;/b&gt;&amp;lt;/beans&amp;gt;&lt;/pre&gt;&lt;/li&gt;&lt;li&gt;Update the Web deployment descriptor to forward DWR requests to the DWR Servlet&lt;pre&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot;?&amp;gt;&lt;br /&gt;&amp;lt;web-app xmlns:xsi=&amp;quot;http://www.w3.org/2001/XMLSchema-instance&amp;quot; xmlns=&amp;quot;http://java.sun.com/xml/ns/javaee&amp;quot;&lt;br /&gt; xmlns:web=&amp;quot;http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd&amp;quot;&lt;br /&gt; xsi:schemaLocation=&amp;quot;http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd&amp;quot; id=&amp;quot;WebApp_ID&amp;quot; version=&amp;quot;2.5&amp;quot;&amp;gt;&lt;br /&gt; &amp;lt;display-name&amp;gt;DWRSpring&amp;lt;/display-name&amp;gt;&lt;br /&gt; &amp;lt;context-param&amp;gt;&lt;br /&gt;  &amp;lt;param-name&amp;gt;contextConfigLocation&amp;lt;/param-name&amp;gt;&lt;br /&gt;  &amp;lt;param-value&amp;gt;/WEB-INF/applicationContext.xml&amp;lt;/param-value&amp;gt;&lt;br /&gt; &amp;lt;/context-param&amp;gt;&lt;br /&gt; &amp;lt;listener&amp;gt;&lt;br /&gt;  &amp;lt;display-name&amp;gt;SpringListener&amp;lt;/display-name&amp;gt;&lt;br /&gt;  &amp;lt;listener-class&amp;gt;org.springframework.web.context.ContextLoaderListener&amp;lt;/listener-class&amp;gt;&lt;br /&gt; &amp;lt;/listener&amp;gt;&lt;br /&gt; &amp;lt;filter&amp;gt;&lt;br /&gt;  &amp;lt;filter-name&amp;gt;Acegi Filter Chain Proxy&amp;lt;/filter-name&amp;gt;&lt;br /&gt;  &amp;lt;filter-class&amp;gt;org.acegisecurity.util.FilterToBeanProxy&amp;lt;/filter-class&amp;gt;&lt;br /&gt;  &amp;lt;init-param&amp;gt;&lt;br /&gt;   &amp;lt;param-name&amp;gt;targetClass&amp;lt;/param-name&amp;gt;&lt;br /&gt;   &amp;lt;param-value&amp;gt;org.acegisecurity.util.FilterChainProxy&amp;lt;/param-value&amp;gt;&lt;br /&gt;  &amp;lt;/init-param&amp;gt;&lt;br /&gt; &amp;lt;/filter&amp;gt;&lt;br /&gt;&lt;br /&gt; &amp;lt;filter-mapping&amp;gt;&lt;br /&gt;  &amp;lt;filter-name&amp;gt;Acegi Filter Chain Proxy&amp;lt;/filter-name&amp;gt;&lt;br /&gt;  &amp;lt;url-pattern&amp;gt;/*&amp;lt;/url-pattern&amp;gt;&lt;br /&gt; &amp;lt;/filter-mapping&amp;gt;&lt;br /&gt;&lt;b&gt; &amp;lt;servlet&amp;gt;&lt;br /&gt;  &amp;lt;servlet-name&amp;gt;dwr-invoker&amp;lt;/servlet-name&amp;gt;&lt;br /&gt;  &amp;lt;servlet-class&amp;gt;uk.ltd.getahead.dwr.DWRServlet&amp;lt;/servlet-class&amp;gt;&lt;br /&gt;  &amp;lt;init-param&amp;gt;&lt;br /&gt;   &amp;lt;param-name&amp;gt;debug&amp;lt;/param-name&amp;gt;&lt;br /&gt;   &amp;lt;param-value&amp;gt;true&amp;lt;/param-value&amp;gt;&lt;br /&gt;  &amp;lt;/init-param&amp;gt;&lt;br /&gt; &amp;lt;/servlet&amp;gt;&lt;br /&gt;&lt;br /&gt; &amp;lt;servlet-mapping&amp;gt;&lt;br /&gt;  &amp;lt;servlet-name&amp;gt;dwr-invoker&amp;lt;/servlet-name&amp;gt;&lt;br /&gt;  &amp;lt;url-pattern&amp;gt;/dwr/*&amp;lt;/url-pattern&amp;gt;&lt;br /&gt; &amp;lt;/servlet-mapping&amp;gt;&lt;br /&gt; &lt;/b&gt;&amp;lt;welcome-file-list&amp;gt;&lt;br /&gt;  &amp;lt;welcome-file&amp;gt;index.jsp&amp;lt;/welcome-file&amp;gt;&lt;br /&gt;  &amp;lt;welcome-file&amp;gt;default.jsp&amp;lt;/welcome-file&amp;gt;&lt;br /&gt; &amp;lt;/welcome-file-list&amp;gt;&lt;br /&gt;&amp;lt;/web-app&amp;gt;&lt;/pre&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;In the DWR configuration file, set the creator to Spring&lt;pre&gt;&amp;lt;!DOCTYPE dwr PUBLIC&lt;br /&gt;  &amp;quot;-//GetAhead Limited//DTD Direct Web Remoting 1.0//EN&amp;quot;&lt;br /&gt;  &amp;quot;http://www.getahead.ltd.uk/dwr/dwr10.dtd&amp;quot;&amp;gt;&lt;br /&gt;&amp;lt;dwr&amp;gt;&lt;br /&gt; &amp;lt;allow&amp;gt;&lt;br /&gt;&lt;b&gt;  &amp;lt;convert match=&amp;quot;java.lang.Exception&amp;quot; converter=&amp;quot;exception&amp;quot;/&amp;gt;&lt;br /&gt;  &amp;lt;create creator=&amp;quot;spring&amp;quot; javascript=&amp;quot;secureDAO&amp;quot;&amp;gt;&lt;br /&gt;   &lt;/b&gt;&amp;lt;param name=&amp;quot;beanName&amp;quot; value=&amp;quot;secureDAO&amp;quot;/&amp;gt;&lt;br /&gt;  &amp;lt;/create&amp;gt;&lt;br /&gt; &amp;lt;/allow&amp;gt;&lt;br /&gt;&amp;lt;/dwr&amp;gt;&lt;/pre&gt;Note:&lt;ul&gt;&lt;li&gt;The converter is used to convert the Java exception to Javascript exception.&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;li&gt;The main change will be made to the secure/authenticatedusers.jsp file. This will use DWR to make Ajax requests. Here is the code for the file&lt;pre&gt;&amp;lt;%@ page import=&amp;quot;org.acegisecurity.context.SecurityContextHolder&amp;quot;%&amp;gt;&lt;br /&gt;&amp;lt;html&amp;gt;&lt;br /&gt;&amp;lt;head&amp;gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;&amp;lt;script type='text/javascript' src='/DWRSpring/dwr/interface/secureDAO.js'&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;&amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;../dwr/engine.js&amp;quot;&amp;gt; &amp;lt;/script&amp;gt;&lt;br /&gt;&amp;lt;script type=&amp;quot;text/javascript&amp;quot;  src=&amp;quot;../dwr/util.js&amp;quot;&amp;gt; &amp;lt;/script&amp;gt;&lt;br /&gt;&lt;/b&gt;&amp;lt;script&amp;gt;&lt;br /&gt; &lt;b&gt;dwr.engine.setErrorHandler(errorHandlerFn);&lt;br /&gt; &lt;/b&gt;function update() {&lt;br /&gt;  var name = dwr.util.getValue(&amp;quot;method&amp;quot;);&lt;br /&gt;  switch(name) {&lt;br /&gt;   case &amp;quot;create&amp;quot;: &lt;br /&gt;     secureDAO.create(callBackFn)&lt;br /&gt;     break;&lt;br /&gt;   case &amp;quot;read&amp;quot;: &lt;br /&gt;     secureDAO.read(callBackFn);&lt;br /&gt;     break;&lt;br /&gt;   case &amp;quot;update&amp;quot;: &lt;br /&gt;     secureDAO.update(callBackFn);&lt;br /&gt;     break;             &lt;br /&gt;  }&lt;br /&gt; &lt;br /&gt; }&lt;br /&gt; &lt;br /&gt; function callBackFn(str) { &lt;br /&gt;  dwr.util.setValue(&amp;quot;selectedAction&amp;quot;,&amp;quot;Server Returned : &amp;quot; + str); &lt;br /&gt; }&lt;br /&gt; &lt;br /&gt; function errorHandlerFn(message, exception) {&lt;br /&gt;  dwr.util.setValue(&amp;quot;selectedAction&amp;quot;, &amp;quot;Error : &amp;quot; + message);&lt;br /&gt; }&lt;br /&gt;&amp;lt;/script&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;/head&amp;gt;&lt;br /&gt;&amp;lt;body&amp;gt;&lt;br /&gt;&amp;lt;h1&amp;gt;Welcome: &amp;lt;%=SecurityContextHolder.getContext().getAuthentication().getName()%&amp;gt;&amp;lt;/h1&amp;gt;&lt;br /&gt;&amp;lt;p&amp;gt;&amp;lt;a href=&amp;quot;../&amp;quot;&amp;gt;Home&amp;lt;/a&amp;gt;&lt;br /&gt;&amp;lt;form name=&amp;quot;testForm&amp;quot; action=&amp;quot;&amp;quot;&amp;gt;&amp;lt;select name=&amp;quot;method&amp;quot; onchange=&amp;quot;update()&amp;quot;&amp;gt;&lt;br /&gt; &amp;lt;option value=&amp;quot;&amp;quot;&amp;gt;&amp;lt;/option&amp;gt;&lt;br /&gt; &amp;lt;option value=&amp;quot;create&amp;quot;&amp;gt;create&amp;lt;/option&amp;gt;&lt;br /&gt; &amp;lt;option value=&amp;quot;read&amp;quot;&amp;gt;read&amp;lt;/option&amp;gt;&lt;br /&gt; &amp;lt;option value=&amp;quot;update&amp;quot;&amp;gt;update&amp;lt;/option&amp;gt;&lt;br /&gt;&amp;lt;/select&amp;gt;&amp;lt;/form&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;div id=&amp;quot;selectedAction&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;&amp;lt;p&amp;gt;&amp;lt;a href=&amp;quot;../j_acegi_logout&amp;quot;&amp;gt;Logout&amp;lt;/a&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;&amp;lt;/body&amp;gt;&lt;br /&gt;&amp;lt;/html&amp;gt;&lt;/pre&gt;Note:&lt;ul&gt;&lt;li&gt;The classes that are exposed through DWR will be available through the /WEB_APP_NAME/dwr/interface/JAVASCRIPT_NAME.js files.&lt;pre&gt;&amp;lt;script type='text/javascript' src='/DWRSpring/dwr/interface/secureDAO.js'&amp;gt;&amp;lt;/script&amp;gt;&lt;/pre&gt; &lt;/li&gt;&lt;li&gt;The setErrorhandler call sets the global error handling function.&lt;pre&gt;dwr.engine.setErrorHandler(errorHandlerFn);&lt;/pre&gt;Alternatively, the error handling function can be set for individual method calls (as described in &lt;a href="http://getahead.org/dwr/other/errors"&gt;DWR documentation&lt;/a&gt;a&lt;pre&gt;Remote.method(params, {&lt;br /&gt;  callback:function(data) { ... },&lt;br /&gt;  errorHandler:function(errorString, exception) { ... }&lt;br /&gt;});&lt;/pre&gt;&lt;/li&gt;&lt;li&gt;util.js file contains utility functions for getting and setting values for the document elements.&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;li&gt;Make sure you have the following Jar files in your classpath:&lt;ul&gt;&lt;li&gt;acegi-security-1.0.3.jar&lt;/li&gt;&lt;li&gt;ant-junit.jar&lt;/li&gt;&lt;li&gt;cglib-nodep-2.1_3.jar&lt;/li&gt;&lt;li&gt;commons-codec-1.3.jar&lt;/li&gt;&lt;li&gt;commons-logging.jar&lt;/li&gt;&lt;li&gt;dwr.jar&lt;/li&gt;&lt;li&gt;ehcache-1.2.3.jar&lt;/li&gt;&lt;li&gt;jstl.jar&lt;/li&gt;&lt;li&gt;spring.jar&lt;/li&gt;&lt;li&gt;standard.jar&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;/ol&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/18313336-7663901502559146821?l=java-x.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://java-x.blogspot.com/feeds/7663901502559146821/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://java-x.blogspot.com/2007/08/handling-security-with-ajax-dwr-and.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/18313336/posts/default/7663901502559146821'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/18313336/posts/default/7663901502559146821'/><link rel='alternate' type='text/html' href='http://java-x.blogspot.com/2007/08/handling-security-with-ajax-dwr-and.html' title='Handling Security with Ajax, DWR and Acegi'/><author><name>Abhilash Vuyyuru</name><uri>https://profiles.google.com/107328798079149921932</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh6.googleusercontent.com/-NxAgT10-nrk/AAAAAAAAAAI/AAAAAAAAB0o/Gz3VMjlEXb0/s512-c/photo.jpg'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-18313336.post-6807336721880289484</id><published>2007-05-09T08:11:00.000-04:00</published><updated>2007-05-09T08:50:18.139-04:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='news'/><title type='text'>OpenJDK and JavaFX</title><content type='html'>Java is now completely open source. The JDK source code is now available through the &lt;a href="http://openjdk.java.net/"&gt;OpenJDK&lt;/a&gt; project. According to the marketing manager of the OpenJDK project Rich Sands, Developers can,&lt;pre&gt;... learn how the JDK is put together, fix that bug that's been &lt;br /&gt;driving you nuts, join the conversations in the mailing lists, &lt;br /&gt;start or participate in projects to improve the implementation.&lt;/pre&gt;It's good to see that the governing body consists of not just Sun employees.&lt;br /&gt;&lt;br /&gt;On a related note, &lt;span class="fullpost"&gt;Sun has announced the new &lt;a href="http://www.sun.com/software/javafx/"&gt;JavaFX&lt;/a&gt; product family. Following Adobe's Flex, and Microsoft's Silverlight, JavaFX script is another product for building rich internet applicaitons. JavaFX Mobile is a complete mobile operating and application environment built around Java and Linux. JavaFX Mobile includes support for Java ME applications and other standard Java APIs.&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/18313336-6807336721880289484?l=java-x.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://java-x.blogspot.com/feeds/6807336721880289484/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://java-x.blogspot.com/2007/05/openjdk-and-javafx.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/18313336/posts/default/6807336721880289484'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/18313336/posts/default/6807336721880289484'/><link rel='alternate' type='text/html' href='http://java-x.blogspot.com/2007/05/openjdk-and-javafx.html' title='OpenJDK and JavaFX'/><author><name>Abhilash Vuyyuru</name><uri>https://profiles.google.com/107328798079149921932</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh6.googleusercontent.com/-NxAgT10-nrk/AAAAAAAAAAI/AAAAAAAAB0o/Gz3VMjlEXb0/s512-c/photo.jpg'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-18313336.post-1280189309740933615</id><published>2007-05-07T15:57:00.000-04:00</published><updated>2007-05-07T16:01:30.100-04:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Java EE 5'/><category scheme='http://www.blogger.com/atom/ns#' term='security'/><title type='text'>Securing EJB 3.0 Beans</title><content type='html'>The Java EE 5 Security services are provided by the container and can be implemented using declarative or programmatic techniques. In addition to declarative and programmatic ways to implement security (in J2EE), Java EE 5 supports the use of metadata annotations for security. This post will describe how to secure EJB 3.0 beans. The post consists of a simple EJB, with a web client. In order to run the example, follow these steps.&lt;br /&gt;&lt;span style="font-size:130%;"&gt;&lt;span style="font-weight: bold;font-family:arial;" &gt;Create Users in Glassfish&lt;/span&gt;&lt;/span&gt;&lt;ol&gt;&lt;li&gt;Go to Configuration-&gt;Security-&gt;Realms-&gt;file in the Glassfish admin console.&lt;/li&gt;&lt;li&gt;In the file realm, click on manage users.&lt;/li&gt;&lt;li&gt;Add new users by clicking on add there.&lt;/li&gt;&lt;/ol&gt;&lt;span class="fullpost"&gt;&lt;br /&gt;&lt;span style="font-weight: bold;font-size:130%;" &gt;&lt;span style="font-family:arial;"&gt;The EJB Component&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;ol&gt;&lt;li&gt;Start with a Simple Java project in Eclipse.&lt;/li&gt;&lt;li&gt;&lt;span style="font-weight: bold;"&gt;Remote Interface&lt;/span&gt;&lt;pre&gt;package ejb;&lt;br /&gt;&lt;br /&gt;import javax.ejb.Remote;&lt;br /&gt;&lt;br /&gt;@Remote&lt;br /&gt;public interface DABean {&lt;br /&gt;public String create();&lt;br /&gt;&lt;br /&gt;public String read();&lt;br /&gt;&lt;br /&gt;public String update();&lt;br /&gt;&lt;br /&gt;public String delete();&lt;br /&gt;}&lt;/pre&gt;&lt;center&gt;ejb/DABean.java&lt;/center&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-weight: bold;"&gt;The Bean&lt;/span&gt;:&lt;pre&gt;package ejb;&lt;br /&gt;&lt;br /&gt;import javax.annotation.security.DeclareRoles;&lt;br /&gt;import javax.annotation.security.RolesAllowed;&lt;br /&gt;import javax.ejb.Stateless;&lt;br /&gt;&lt;br /&gt;@Stateless (mappedName = "ejb/secureEJB")&lt;br /&gt;@DeclareRoles({"emp","guest"})&lt;br /&gt;&lt;br /&gt;public class SecureEJB implements DABean {&lt;br /&gt;&lt;br /&gt;@RolesAllowed({"emp","guest"})&lt;br /&gt;public String create() {&lt;br /&gt;  return "create";&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;@RolesAllowed({"emp","guest"})&lt;br /&gt;public String read() {&lt;br /&gt;  return "read";&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;@RolesAllowed("emp")&lt;br /&gt;public String update() {&lt;br /&gt;  return "update";&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;@RolesAllowed("emp")&lt;br /&gt;public String delete() {&lt;br /&gt;  return "delete";&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;/pre&gt;&lt;center&gt;ejb/SecureEJB.java&lt;/center&gt;&lt;ul&gt;&lt;li&gt;The declaredRoles and RolesAllowed annotations take a string array as a parameter.&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-weight: bold;"&gt;Deployment descriptor:&lt;/span&gt;&lt;pre&gt;&amp;lt;?xml version="1.0" encoding="UTF-8"?&amp;gt;&lt;br /&gt;&amp;lt;!DOCTYPE sun-ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Application Server 9.0 EJB 3.0//EN" "http://www.sun.com/software/appserver/dtds/sun-ejb-jar_3_0-0.dtd"&amp;gt;&lt;br /&gt;&amp;lt;sun-ejb-jar&amp;gt;&lt;br /&gt;&amp;lt;security-role-mapping&amp;gt;&lt;br /&gt;  &amp;lt;role-name&amp;gt;guest&amp;lt;/role-name&amp;gt;&lt;br /&gt;  &amp;lt;group-name&amp;gt;guest&amp;lt;/group-name&amp;gt;&lt;br /&gt;&amp;lt;/security-role-mapping&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;security-role-mapping&amp;gt;&lt;br /&gt;  &amp;lt;role-name&amp;gt;emp&amp;lt;/role-name&amp;gt;&lt;br /&gt;  &amp;lt;group-name&amp;gt;employee&amp;lt;/group-name&amp;gt;&lt;br /&gt;&amp;lt;/security-role-mapping&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;enterprise-beans&amp;gt;&lt;br /&gt;  &amp;lt;unique-id&amp;gt;0&amp;lt;/unique-id&amp;gt;&lt;br /&gt;  &amp;lt;ejb&amp;gt;&lt;br /&gt;    &amp;lt;ejb-name&amp;gt;SecureEJB&amp;lt;/ejb-name&amp;gt;&lt;br /&gt;    &amp;lt;jndi-name&amp;gt;ejb/secureEJB&amp;lt;/jndi-name&amp;gt;&lt;br /&gt;    &amp;lt;gen-classes /&amp;gt;&lt;br /&gt;  &amp;lt;/ejb&amp;gt;&lt;br /&gt;&amp;lt;/enterprise-beans&amp;gt;&lt;br /&gt;&amp;lt;/sun-ejb-jar&amp;gt;&lt;/pre&gt;&lt;center&gt;META-INF/sun-ejb-jar.xml&lt;/center&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;span style="font-weight: bold;font-size:130%;" &gt;&lt;span style="font-family:arial;"&gt;The Web Client&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;For a little bit more detail explanation on the Web Application, see the previous post &lt;a href="http://java-x.blogspot.com/2007/01/securing-java-ee-5-web-applications.html"&gt;Securing Java EE 5 Web Applications&lt;/a&gt;&lt;ol&gt;&lt;li&gt;&lt;span style="font-weight: bold;"&gt;The EJB Client Jar file:&lt;/span&gt; When you deploy the EJB application in Glassfish, it creates a corresponding EJB Client jar file for the EJB component, which can be used in the clients. The file will created in the following directory.&lt;pre&gt;&lt;i&gt;GLASSFISH_HOME&lt;/i&gt;\domains\&lt;i&gt;DOMAIN_NAME&lt;/i&gt;/generated\xml/j2ee-modules/&lt;i&gt;APPLICATION_NAME&lt;/i&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-weight: bold;"&gt;Selection page&lt;/span&gt;&lt;pre&gt;&amp;lt;html&amp;gt;&lt;br /&gt;&amp;lt;body&amp;gt;&lt;br /&gt;&amp;lt;h1&amp;gt;Home Page&amp;lt;/h1&amp;gt;&lt;br /&gt;Anyone can view this page.&lt;br /&gt;&lt;br /&gt;&amp;lt;form action="securityServlet"&amp;gt;&amp;lt;select name="method"&amp;gt;&lt;br /&gt;&amp;lt;option value="create"&amp;gt;create&amp;lt;/option&amp;gt;&lt;br /&gt;&amp;lt;option value="read"&amp;gt;read&amp;lt;/option&amp;gt;&lt;br /&gt;&amp;lt;option value="update"&amp;gt;update&amp;lt;/option&amp;gt;&lt;br /&gt;&amp;lt;option value="delete"&amp;gt;delete&amp;lt;/option&amp;gt;&lt;br /&gt;&amp;lt;/select&amp;gt; &amp;lt;input type="submit" name="submit" /&amp;gt;&amp;lt;/form&amp;gt;&lt;br /&gt;&amp;lt;/body&amp;gt;&lt;br /&gt;&amp;lt;/html&amp;gt;&lt;/pre&gt;&lt;center&gt;index.jsp&lt;/center&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-weight: bold;"&gt;Servlet&lt;/span&gt;&lt;pre&gt;package servlets;&lt;br /&gt;&lt;br /&gt;import java.io.IOException;&lt;br /&gt;import java.io.PrintWriter;&lt;br /&gt;&lt;br /&gt;import javax.annotation.security.DeclareRoles;&lt;br /&gt;import javax.ejb.EJB;&lt;br /&gt;import javax.servlet.ServletException;&lt;br /&gt;import javax.servlet.http.HttpServletRequest;&lt;br /&gt;import javax.servlet.http.HttpServletResponse;&lt;br /&gt;&lt;br /&gt;import ejb.DABean;&lt;br /&gt;&lt;br /&gt;@DeclareRoles("emp")&lt;br /&gt;public class SecurityServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {&lt;br /&gt;&lt;br /&gt;@EJB(name = "timerBean", mappedName = "corbaname:iiop:localhost:3700#ejb/secureEJB")&lt;br /&gt;private DABean daBean;&lt;br /&gt;&lt;br /&gt;public SecurityServlet() {&lt;br /&gt;  super();&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {&lt;br /&gt;  PrintWriter out = response.getWriter();&lt;br /&gt;  String method = request.getParameter("method");&lt;br /&gt;  try {&lt;br /&gt;    String result = "";&lt;br /&gt;    if (method.equals("create")) {&lt;br /&gt;      result = daBean.create();&lt;br /&gt;    }&lt;br /&gt;    if (method.equals("read")) {&lt;br /&gt;      result = daBean.read();&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    if (method.equals("update")) {&lt;br /&gt;      result = daBean.update();&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    if (method.equals("delete")) {&lt;br /&gt;      result = daBean.delete();&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    out.println(request.getUserPrincipal() + " is an Authorized User");&lt;br /&gt;  } catch (Exception e) {&lt;br /&gt;    e.printStackTrace();&lt;br /&gt;    out.println(request.getUserPrincipal() + " is not an Authorized to see this page.");&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;}&lt;/pre&gt;&lt;center&gt;SecurityServlet.java&lt;/center&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-weight: bold;"&gt;Deployment descriptor&lt;/span&gt;&lt;pre&gt;&amp;lt;?xml version="1.0" encoding="UTF-8"?&amp;gt;&lt;br /&gt;&amp;lt;web-app xmlns="http://java.sun.com/xml/ns/javaee" version="2.5" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&lt;br /&gt;xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"&amp;gt;&lt;br /&gt;&amp;lt;display-name&amp;gt;Java5Security&amp;lt;/display-name&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;servlet&amp;gt;&lt;br /&gt;  &amp;lt;description&amp;gt;&amp;lt;/description&amp;gt;&lt;br /&gt;  &amp;lt;display-name&amp;gt;SecurityServlet&amp;lt;/display-name&amp;gt;&lt;br /&gt;  &amp;lt;servlet-name&amp;gt;SecurityServlet&amp;lt;/servlet-name&amp;gt;&lt;br /&gt;  &amp;lt;servlet-class&amp;gt;servlets.SecurityServlet&amp;lt;/servlet-class&amp;gt;&lt;br /&gt;  &amp;lt;security-role-ref&amp;gt;&lt;br /&gt;    &amp;lt;role-name&amp;gt;emp&amp;lt;/role-name&amp;gt;&lt;br /&gt;    &amp;lt;role-link&amp;gt;emp&amp;lt;/role-link&amp;gt;&lt;br /&gt;  &amp;lt;/security-role-ref&amp;gt;&lt;br /&gt;&amp;lt;/servlet&amp;gt;&lt;br /&gt;&amp;lt;servlet-mapping&amp;gt;&lt;br /&gt;  &amp;lt;servlet-name&amp;gt;SecurityServlet&amp;lt;/servlet-name&amp;gt;&lt;br /&gt;  &amp;lt;url-pattern&amp;gt;/securityServlet&amp;lt;/url-pattern&amp;gt;&lt;br /&gt;&amp;lt;/servlet-mapping&amp;gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;login-config&amp;gt;&lt;br /&gt;  &amp;lt;auth-method&amp;gt;FORM&amp;lt;/auth-method&amp;gt;&lt;br /&gt;  &amp;lt;realm-name&amp;gt;file&amp;lt;/realm-name&amp;gt;&lt;br /&gt;  &amp;lt;form-login-config&amp;gt;&lt;br /&gt;    &amp;lt;form-login-page&amp;gt;/login.jsp&amp;lt;/form-login-page&amp;gt;&lt;br /&gt;    &amp;lt;form-error-page&amp;gt;/error.jsp&amp;lt;/form-error-page&amp;gt;&lt;br /&gt;  &amp;lt;/form-login-config&amp;gt;&lt;br /&gt;&amp;lt;/login-config&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;security-constraint&amp;gt;&lt;br /&gt;  &amp;lt;web-resource-collection&amp;gt;&lt;br /&gt;    &amp;lt;web-resource-name&amp;gt;Protected Area&amp;lt;/web-resource-name&amp;gt;&lt;br /&gt;    &amp;lt;url-pattern&amp;gt;/*&amp;lt;/url-pattern&amp;gt;&lt;br /&gt;    &amp;lt;http-method&amp;gt;PUT&amp;lt;/http-method&amp;gt;&lt;br /&gt;    &amp;lt;http-method&amp;gt;DELETE&amp;lt;/http-method&amp;gt;&lt;br /&gt;    &amp;lt;http-method&amp;gt;GET&amp;lt;/http-method&amp;gt;&lt;br /&gt;    &amp;lt;http-method&amp;gt;POST&amp;lt;/http-method&amp;gt;&lt;br /&gt;  &amp;lt;/web-resource-collection&amp;gt;&lt;br /&gt;  &amp;lt;auth-constraint&amp;gt;&lt;br /&gt;    &amp;lt;role-name&amp;gt;guest&amp;lt;/role-name&amp;gt;&lt;br /&gt;    &amp;lt;role-name&amp;gt;emp&amp;lt;/role-name&amp;gt;&lt;br /&gt;  &amp;lt;/auth-constraint&amp;gt;&lt;br /&gt;&amp;lt;/security-constraint&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;security-constraint&amp;gt;&lt;br /&gt;  &amp;lt;web-resource-collection&amp;gt;&lt;br /&gt;    &amp;lt;web-resource-name&amp;gt;Protected Area&amp;lt;/web-resource-name&amp;gt;&lt;br /&gt;    &amp;lt;url-pattern&amp;gt;/secure/*&amp;lt;/url-pattern&amp;gt;&lt;br /&gt;    &amp;lt;http-method&amp;gt;PUT&amp;lt;/http-method&amp;gt;&lt;br /&gt;    &amp;lt;http-method&amp;gt;DELETE&amp;lt;/http-method&amp;gt;&lt;br /&gt;    &amp;lt;http-method&amp;gt;GET&amp;lt;/http-method&amp;gt;&lt;br /&gt;    &amp;lt;http-method&amp;gt;POST&amp;lt;/http-method&amp;gt;&lt;br /&gt;  &amp;lt;/web-resource-collection&amp;gt;&lt;br /&gt;  &amp;lt;auth-constraint&amp;gt;&lt;br /&gt;    &amp;lt;role-name&amp;gt;emp&amp;lt;/role-name&amp;gt;&lt;br /&gt;  &amp;lt;/auth-constraint&amp;gt;&lt;br /&gt;&amp;lt;/security-constraint&amp;gt;&lt;br /&gt;&amp;lt;!-- Security roles referenced by this web application --&amp;gt;&lt;br /&gt;&amp;lt;security-role&amp;gt;&lt;br /&gt;  &amp;lt;role-name&amp;gt;guest&amp;lt;/role-name&amp;gt;&lt;br /&gt;&amp;lt;/security-role&amp;gt;&lt;br /&gt;&amp;lt;security-role&amp;gt;&lt;br /&gt;  &amp;lt;role-name&amp;gt;emp&amp;lt;/role-name&amp;gt;&lt;br /&gt;&amp;lt;/security-role&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;welcome-file-list&amp;gt;&lt;br /&gt;  &amp;lt;welcome-file&amp;gt;index.html&amp;lt;/welcome-file&amp;gt;&lt;br /&gt;&amp;lt;/welcome-file-list&amp;gt;&lt;br /&gt;&amp;lt;/web-app&amp;gt;&lt;/pre&gt;&lt;center&gt;web.xml&lt;/center&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-weight: bold;"&gt;Glassfish Deployment descriptor&lt;/span&gt;&lt;pre&gt;&amp;lt;?xml version="1.0" encoding="UTF-8"?&amp;gt;&lt;br /&gt;&amp;lt;!DOCTYPE sun-web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Application Server 8.1 Servlet 2.4//EN" "http://www.sun.com/software/appserver/dtds/sun-web-app_2_4-1.dtd"&amp;gt;&lt;br /&gt;&amp;lt;sun-web-app&amp;gt;&lt;br /&gt;&amp;lt;context-root&amp;gt;/Java5Security&amp;lt;/context-root&amp;gt;&lt;br /&gt;&amp;lt;security-role-mapping&amp;gt;&lt;br /&gt;  &amp;lt;role-name&amp;gt;guest&amp;lt;/role-name&amp;gt;&lt;br /&gt;  &amp;lt;group-name&amp;gt;guest&amp;lt;/group-name&amp;gt;&lt;br /&gt;&amp;lt;/security-role-mapping&amp;gt;&lt;br /&gt;&amp;lt;security-role-mapping&amp;gt;&lt;br /&gt;  &amp;lt;role-name&amp;gt;emp&amp;lt;/role-name&amp;gt;&lt;br /&gt;  &amp;lt;group-name&amp;gt;employee&amp;lt;/group-name&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;/security-role-mapping&amp;gt;&lt;br /&gt;&amp;lt;/sun-web-app&amp;gt;&lt;/pre&gt;&lt;center&gt;sun-web.xml&lt;/center&gt;&lt;/li&gt;&lt;/ol&gt;&lt;span style="font-weight: bold;"&gt;Environment:&lt;/span&gt; This example was run on Glassfish V2 Build 41 (Glassfish V2 Beta 2).&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/18313336-1280189309740933615?l=java-x.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://java-x.blogspot.com/feeds/1280189309740933615/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://java-x.blogspot.com/2007/05/securing-ejb-30-beans.html#comment-form' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/18313336/posts/default/1280189309740933615'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/18313336/posts/default/1280189309740933615'/><link rel='alternate' type='text/html' href='http://java-x.blogspot.com/2007/05/securing-ejb-30-beans.html' title='Securing EJB 3.0 Beans'/><author><name>Abhilash Vuyyuru</name><uri>https://profiles.google.com/107328798079149921932</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh6.googleusercontent.com/-NxAgT10-nrk/AAAAAAAAAAI/AAAAAAAAB0o/Gz3VMjlEXb0/s512-c/photo.jpg'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-18313336.post-8694764411244263519</id><published>2007-05-01T10:06:00.000-04:00</published><updated>2007-05-01T10:10:20.398-04:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='websphere'/><category scheme='http://www.blogger.com/atom/ns#' term='news'/><category scheme='http://www.blogger.com/atom/ns#' term='Java EE 5'/><title type='text'>Java EE 5 on WebSphere</title><content type='html'>While the next version of WAS (v7.0) with Java EE 5, is yet to be released, you can now test some of the features such as EJB 3.0 with &lt;a href="https://www14.software.ibm.com/iwm/web/cc/earlyprograms/websphere.shtml"&gt;WebSphere software early programs&lt;/a&gt;. The new features are available as feature packs for the current WebSphere Application Server (v6.1).&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/18313336-8694764411244263519?l=java-x.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://java-x.blogspot.com/feeds/8694764411244263519/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://java-x.blogspot.com/2007/05/java-ee-5-on-websphere.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/18313336/posts/default/8694764411244263519'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/18313336/posts/default/8694764411244263519'/><link rel='alternate' type='text/html' href='http://java-x.blogspot.com/2007/05/java-ee-5-on-websphere.html' title='Java EE 5 on WebSphere'/><author><name>Abhilash Vuyyuru</name><uri>https://profiles.google.com/107328798079149921932</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh6.googleusercontent.com/-NxAgT10-nrk/AAAAAAAAAAI/AAAAAAAAB0o/Gz3VMjlEXb0/s512-c/photo.jpg'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-18313336.post-6719544913293902004</id><published>2007-04-13T15:43:00.000-04:00</published><updated>2007-04-14T05:40:43.035-04:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='spring'/><category scheme='http://www.blogger.com/atom/ns#' term='security'/><category scheme='http://www.blogger.com/atom/ns#' term='example/sample code'/><title type='text'>Customize Acegi SecurityContext</title><content type='html'>This post describes how to modify the User details stored in the Acegi Security Context.&lt;br /&gt;&lt;br /&gt;Acegi Security uses the SecurityContextHolder object to store details of the current security context of the application. The SecurityContext holds the details of the authenticated principal in an Authentication object. By default the SecurityContextHolder uses a ThreadLocal to store these details, so that they will be available all methods in the current thread of execution.In order to obtain the Principal, you would use the following line&lt;pre&gt;Object obj = SecurityContextHolder.getContext().getAuthentication().getPrincipal();&lt;/pre&gt;&lt;span class="fullpost"&gt;Usually, the principal is an object of the type UserDetails. The UserDetails object is sort of an adapter between the security database and Acegi SecurityContextHolder. Acegi uses a UserDetailsSevice to build the UserDetails object. In order to modify the data stored Security Context of Acegi, you either have to&lt;ul&gt;&lt;li&gt; setContext() method of the SecurityContextHolder. The SecurityContext object can be set to hold an Authentication object. This will mean that you have to add some additional security code to your application code.&lt;/li&gt;&lt;li&gt;Implement the UserDetailsService and UserDetails interfaces. This way you can keep security code seperated from the application code.&lt;/li&gt;&lt;/ul&gt;This post will describe how to implement UserDetailsService and UserDetails Objects. Here's what to do to implement a UserDetailsService.&lt;ol&gt;&lt;li&gt;Start with the implementing acegi security posts &lt;a href="http://java-x.blogspot.com/2006/12/spring-security-with-acegi-security.html"&gt;part 1&lt;/a&gt; and &lt;a href="http://java-x.blogspot.com/2007/03/securing-middle-tier-objects-with-acegi.html"&gt;part 2&lt;/a&gt;.&lt;/li&gt;&lt;li&gt;Create the UserDetailsService:&lt;pre&gt;package authentication;&lt;br /&gt;&lt;br /&gt;import java.util.HashMap;&lt;br /&gt;import java.util.Map;&lt;br /&gt;&lt;br /&gt;import org.acegisecurity.GrantedAuthority;&lt;br /&gt;import org.acegisecurity.userdetails.UserDetails;&lt;br /&gt;import org.acegisecurity.userdetails.UserDetailsService;&lt;br /&gt;import org.acegisecurity.userdetails.UsernameNotFoundException;&lt;br /&gt;import org.springframework.dao.DataAccessException;&lt;br /&gt;&lt;br /&gt;public class MyUserDetailsService implements UserDetailsService {&lt;br /&gt;&lt;br /&gt;private Map users = init();&lt;br /&gt;&lt;br /&gt;private Map init() {&lt;br /&gt;  Map tUsers = new HashMap();&lt;br /&gt;&lt;br /&gt;  tUsers.put("scott", new User("scott", "tiger", "ROLE_USER"));&lt;br /&gt;  tUsers.put("harry", new User("harry", "potter", "ROLE_ADMIN"));&lt;br /&gt;  tUsers.put("frodo", new User("frodo", "baggins", "ROLE_USER"));&lt;br /&gt;&lt;br /&gt;  return tUsers;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException, DataAccessException {&lt;br /&gt;  User user = (User) users.get(s);&lt;br /&gt;  GrantedAuthority authority = new MyGrantedAuthority(user.getRole());&lt;br /&gt;  UserDetails userDetails = new MyUserDetails(new GrantedAuthority[] { authority }, user.getUserId(), user.getPassword(), "Additional Data");&lt;br /&gt;  return userDetails;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;/pre&gt;&lt;center&gt;MyUserDetailsService.java&lt;/center&gt;&lt;br /&gt;Notice that all the user data is defined in this class. The UserDetailsService has to return a UserDetails object using the User name.&lt;/li&gt;&lt;li&gt;Create the UserDetails class:&lt;pre&gt;package authentication;&lt;br /&gt;&lt;br /&gt;import org.acegisecurity.GrantedAuthority;&lt;br /&gt;import org.acegisecurity.userdetails.UserDetails;&lt;br /&gt;&lt;br /&gt;public class MyUserDetails implements UserDetails {&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;private GrantedAuthority[] authorities = null;&lt;br /&gt;private String password = null;&lt;br /&gt;private String username = null;&lt;br /&gt;private String additionalData = null;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;public MyUserDetails(GrantedAuthority[] authorities, String password, String username, String additionalData) {&lt;br /&gt;  super();&lt;br /&gt;  this.authorities = authorities;&lt;br /&gt;  this.password = password;&lt;br /&gt;  this.username = username;&lt;br /&gt;  this.additionalData = additionalData;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public GrantedAuthority[] getAuthorities() {&lt;br /&gt;  return authorities;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public String getPassword() {&lt;br /&gt;  return password;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public String getUsername() {&lt;br /&gt;  return username;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public boolean isAccountNonExpired() {&lt;br /&gt;  return true;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public boolean isAccountNonLocked() {&lt;br /&gt;  return true;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public boolean isCredentialsNonExpired() {&lt;br /&gt;  return true;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public boolean isEnabled() {&lt;br /&gt;  return true;&lt;br /&gt;}&lt;br /&gt;}&lt;/pre&gt;&lt;center&gt;MyUserDetails.java&lt;/center&gt;&lt;/li&gt;&lt;li&gt;The User class:&lt;pre&gt;package authentication;&lt;br /&gt;&lt;br /&gt;public class User {&lt;br /&gt;&lt;br /&gt;private String userId;&lt;br /&gt;&lt;br /&gt;private String password;&lt;br /&gt;&lt;br /&gt;private String role;&lt;br /&gt;&lt;br /&gt;public String getPassword() {&lt;br /&gt;  return password;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public void setPassword(String password) {&lt;br /&gt;  this.password = password;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public String getRole() {&lt;br /&gt;  return role;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public void setRole(String role) {&lt;br /&gt;  this.role = role;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public String getUserId() {&lt;br /&gt;  return userId;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public void setUserId(String userId) {&lt;br /&gt;  this.userId = userId;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public User(String userId, String password, String role) {&lt;br /&gt;  super();&lt;br /&gt;  this.userId = userId;&lt;br /&gt;  this.password = password;&lt;br /&gt;  this.role = role;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;/pre&gt;&lt;center&gt;User.java&lt;/center&gt;&lt;/li&gt;&lt;li&gt;Modify the applicationContext.xml file: Modify the userDetailsService bean in the application context to as shown below&lt;pre&gt;&amp;lt;bean id="userDetailsService" class="authentication.MyUserDetailsService"&amp;gt;&lt;br /&gt;&amp;lt;/bean&amp;gt;&lt;/pre&gt;&lt;/li&gt;&lt;li&gt;The GrantedAuthority class: The MyGrantedAuthority class is typically used to store application roles.&lt;pre&gt;package authentication;&lt;br /&gt;&lt;br /&gt;import org.acegisecurity.GrantedAuthority;&lt;br /&gt;&lt;br /&gt;public class MyGrantedAuthority implements GrantedAuthority {&lt;br /&gt;&lt;br /&gt;private String authority = null;&lt;br /&gt;&lt;br /&gt;public MyGrantedAuthority(String authority) {&lt;br /&gt;  this.authority = authority;&lt;br /&gt;}&lt;br /&gt;public String getAuthority() {&lt;br /&gt;  return authority;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;/pre&gt;&lt;center&gt;MyGrantedAuthority.java&lt;/center&gt;&lt;/li&gt;&lt;/ol&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/18313336-6719544913293902004?l=java-x.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://java-x.blogspot.com/feeds/6719544913293902004/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://java-x.blogspot.com/2007/04/customize-acegi-securitycontext.html#comment-form' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/18313336/posts/default/6719544913293902004'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/18313336/posts/default/6719544913293902004'/><link rel='alternate' type='text/html' href='http://java-x.blogspot.com/2007/04/customize-acegi-securitycontext.html' title='Customize Acegi SecurityContext'/><author><name>Abhilash Vuyyuru</name><uri>https://profiles.google.com/107328798079149921932</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh6.googleusercontent.com/-NxAgT10-nrk/AAAAAAAAAAI/AAAAAAAAB0o/Gz3VMjlEXb0/s512-c/photo.jpg'/></author><thr:total>5</thr:total></entry><entry><id>tag:blogger.com,1999:blog-18313336.post-8296642962549023556</id><published>2007-04-10T16:17:00.000-04:00</published><updated>2009-06-27T19:45:27.740-04:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='hibernate'/><category scheme='http://www.blogger.com/atom/ns#' term='persistence'/><category scheme='http://www.blogger.com/atom/ns#' term='example/sample code'/><title type='text'>Native Queries with Hibernate Annotations</title><content type='html'>Hibernate EntityManager implements the programming interfaces and lifecycle rules as defined by the EJB3 persistence specification. Together with Hibernate Annotations, this wrapper implements a complete (and standalone) EJB3 persistence solution on top of the mature Hibernate core. In this post I will describe how map native queries (plain SQL) using Hibernate Annotations. Hibernate Annotations supports the use of Native queries through the @NamedNativeQuery and the @SqlResultSetMapping annotations. &lt;ul&gt;&lt;li&gt;&lt;span style="font-weight: bold;"&gt;@NamedNativeQuery&lt;/span&gt;: Specifies a native SQL named query.&lt;/li&gt;&lt;li&gt;&lt;span style="font-weight: bold;"&gt;@SqlResultSetMapping&lt;/span&gt;: Used to specify the mapping of the result of a native SQL query.&lt;/li&gt;&lt;/ul&gt; You will not need any EJB container support. At a minimum, you will need Hibernate core and Hibernate Annotations. The entire list of required Jar files is provided  at the end.&lt;span class="fullpost"&gt;&lt;ol&gt;&lt;li&gt;&lt;span style="font-weight: bold;"&gt;The Hibernate Configuration File&lt;/span&gt;: Nothing new here. Except that there are no mappings defined. I used programmatic declaration of mapping for this example as shown in the following steps.&lt;pre&gt;&amp;lt;!DOCTYPE hibernate-configuration PUBLIC&lt;br /&gt; "-//Hibernate/Hibernate Configuration DTD 3.0//EN"&lt;br /&gt; "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;hibernate-configuration&amp;gt;&lt;br /&gt; &amp;lt;session-factory&amp;gt;&lt;br /&gt;  &amp;lt;property name="connection.driver_class"&amp;gt;oracle.jdbc.driver.OracleDriver&amp;lt;/property&amp;gt;&lt;br /&gt;  &amp;lt;property name="connection.url"&amp;gt;jdbc:oracle:thin:@localhost:1521/orcl&amp;lt;/property&amp;gt;&lt;br /&gt;  &amp;lt;property name="connection.username"&amp;gt;scott&amp;lt;/property&amp;gt;&lt;br /&gt;  &amp;lt;property name="connection.password"&amp;gt;tiger&amp;lt;/property&amp;gt;&lt;br /&gt;  &amp;lt;property name="dialect"&amp;gt;org.hibernate.dialect.Oracle9Dialect&amp;lt;/property&amp;gt;&lt;br /&gt;  &amp;lt;property name="hibernate.current_session_context_class"&amp;gt;thread&amp;lt;/property&amp;gt;&lt;br /&gt; &amp;lt;/session-factory&amp;gt;&lt;br /&gt;&amp;lt;/hibernate-configuration&amp;gt;&lt;/pre&gt;&lt;center&gt;hibernate.cfg.xml&lt;/center&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-weight: bold;"&gt;The Entity class&lt;/span&gt;: &lt;pre&gt;package data;&lt;br /&gt;&lt;br /&gt;import javax.persistence.Column;&lt;br /&gt;import javax.persistence.Entity;&lt;br /&gt;import javax.persistence.EntityResult;&lt;br /&gt;import javax.persistence.FieldResult;&lt;br /&gt;import javax.persistence.Id;&lt;br /&gt;import javax.persistence.NamedNativeQuery;&lt;br /&gt;import javax.persistence.SqlResultSetMapping;&lt;br /&gt;&lt;br /&gt;@Entity&lt;br /&gt;@SqlResultSetMapping(name = "implicit", entities = @EntityResult(entityClass = data.Employee.class))&lt;br /&gt;@NamedNativeQuery(name = "implicitSample", query = "select e.empno empNumber, e.ename empName, e.job empJob, e.sal empSalary, salg.grade empGrade from emp e, salgrade salg where e.sal between salg.losal and salg.HISAL", resultSetMapping = "implicit")&lt;br /&gt;&lt;span style="color: rgb(51, 153, 153);"&gt;//@SqlResultSetMapping(name = "explicit", entities = { @EntityResult(entityClass = data.Employee.class, fields = { &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 153, 153);"&gt;//    @FieldResult(name = "empNumber", column = "empno"), &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 153, 153);"&gt;//    @FieldResult(name = "empName", column = "ename"),&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 153, 153);"&gt;//    @FieldResult(name = "empJob", column = "job"), &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 153, 153);"&gt;//    @FieldResult(name = "empSalary", column = "sal"), &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 153, 153);"&gt;//    @FieldResult(name = "empGrade", column = "grade") }) })&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 153, 153);"&gt;//@NamedNativeQuery(name = "implicitSample", &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 153, 153);"&gt;//    query = "select e.empno empno, e.ename ename, e.job job, e.sal sal, salg.grade grade from emp e, salgrade salg where e.sal between salg.losal and salg.HISAL", resultSetMapping = "explicit")&lt;/span&gt;&lt;br /&gt;public class Employee {&lt;br /&gt;&lt;br /&gt; private String empNumber;&lt;br /&gt;&lt;br /&gt; private String empName;&lt;br /&gt;&lt;br /&gt; private String empJob;&lt;br /&gt;&lt;br /&gt; private Double empSalary;&lt;br /&gt;&lt;br /&gt; private int empGrade;&lt;br /&gt;&lt;br /&gt; @Column&lt;br /&gt; @Id&lt;br /&gt; public int getEmpGrade() {&lt;br /&gt;   return empGrade;&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; public void setEmpGrade(int empGrade) {&lt;br /&gt;   this.empGrade = empGrade;&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; @Column&lt;br /&gt; public String getEmpJob() {&lt;br /&gt;   return empJob;&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; public void setEmpJob(String empJob) {&lt;br /&gt;   this.empJob = empJob;&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; @Column&lt;br /&gt; public String getEmpName() {&lt;br /&gt;   return empName;&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; public void setEmpName(String empName) {&lt;br /&gt;   this.empName = empName;&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; @Column&lt;br /&gt; public String getEmpNumber() {&lt;br /&gt;   return empNumber;&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; public void setEmpNumber(String empNumber) {&lt;br /&gt;   this.empNumber = empNumber;&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; @Column&lt;br /&gt; public Double getEmpSalary() {&lt;br /&gt;   return empSalary;&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; public void setEmpSalary(Double empSalary) {&lt;br /&gt;   this.empSalary = empSalary;&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt;}&lt;/pre&gt;&lt;center&gt;Employee.java&lt;/center&gt;&lt;ul&gt;&lt;li&gt;The implitic mapping (the uncommented @NamedNativeQuery and @SqlResultSetMapping declarations are used for implicitly mapping the ResultSet to the entity class. Note that the SQL column names match the field names in the class. If the names do not match then you can set the name attribute of the @Column annotation to the column name in the query.&lt;/li&gt;&lt;li&gt;The commented @NamedNativeQuery and the @SqlResultSetMapping declarations explicitly map the fields to the columns. This will come in handy when using joins and composite keys etc.&lt;/li&gt;&lt;li&gt;Note the package definitions refer to javax.persistence and not the hibernate packages. If the packages are not declared properly, you will most likely end up with some exceptions like the following&lt;pre style="font-weight: bold;"&gt;org.hibernate.hql.ast.QuerySyntaxException: Employee is not mapped.&lt;/pre&gt;While there are other causes for this exception, the package declarations did cause a little trouble for me.&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-weight: bold;"&gt;The Client&lt;/span&gt;: &lt;pre&gt;import java.util.List;&lt;br /&gt;&lt;br /&gt;import org.hibernate.Session;&lt;br /&gt;import org.hibernate.SessionFactory;&lt;br /&gt;import org.hibernate.cfg.AnnotationConfiguration;&lt;br /&gt;import org.hibernate.cfg.Configuration;&lt;br /&gt;&lt;br /&gt;import data.Employee;&lt;br /&gt;&lt;br /&gt;public class Client {&lt;br /&gt;&lt;br /&gt; public static void main(String[] args) {&lt;br /&gt;   Configuration config = new AnnotationConfiguration().addAnnotatedClass(Employee.class).configure();&lt;br /&gt;&lt;br /&gt;   SessionFactory sessionFactory = config.buildSessionFactory();&lt;br /&gt;   Session session = sessionFactory.getCurrentSession();&lt;br /&gt;&lt;br /&gt;   List result = null;&lt;br /&gt;   try {&lt;br /&gt;     session.beginTransaction();&lt;br /&gt;&lt;br /&gt;     result = session.getNamedQuery("implicitSample").list();&lt;br /&gt;     System.out.println("Result size : " + result.size());&lt;br /&gt;     session.getTransaction().commit();&lt;br /&gt;   } catch (Exception e) {&lt;br /&gt;     e.printStackTrace();&lt;br /&gt;   }&lt;br /&gt;   System.out.println(result.size());&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt;}&lt;/pre&gt;&lt;center&gt;Client.java&lt;/center&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-weight: bold;"&gt;Jar Files&lt;/span&gt;: The following jar files need to be included in the classpath&lt;pre&gt;hibernate3.jar&lt;br /&gt;commons-collections-2.1.1.jar&lt;br /&gt;antlr-2.7.6.jar&lt;br /&gt;commons-logging-1.0.4.jar&lt;br /&gt;hibernate-annotations.jar&lt;br /&gt;ejb3-persistence.jar&lt;br /&gt;hibernate-commons-annotations.jar&lt;br /&gt;dom4j-1.6.1.jar&lt;br /&gt;ojdbc14.jar&lt;br /&gt;jta.jar&lt;br /&gt;log4j-1.2.11.jar&lt;br /&gt;xerces-2.6.2.jar&lt;br /&gt;xml-apis.jar&lt;br /&gt;cglib-2.1.3.jar&lt;br /&gt;asm.jar&lt;/pre&gt;All the Jar files will be available in the hibernate download. The hibernate-annotations.jar file is available in the hibernate annotations download.&lt;/li&gt;&lt;/ol&gt;This example was tested on &lt;span style="font-weight: bold;"&gt;Java 5 Update 9&lt;/span&gt; with &lt;span style="font-weight: bold;"&gt;Hibernate version 3.2.3&lt;/span&gt; and &lt;span style="font-weight: bold;"&gt;Hibernate Annotations Version: 3.3.0.GA&lt;/span&gt;.&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/18313336-8296642962549023556?l=java-x.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://java-x.blogspot.com/feeds/8296642962549023556/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://java-x.blogspot.com/2007/04/native-queries-with-hibernate.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/18313336/posts/default/8296642962549023556'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/18313336/posts/default/8296642962549023556'/><link rel='alternate' type='text/html' href='http://java-x.blogspot.com/2007/04/native-queries-with-hibernate.html' title='Native Queries with Hibernate Annotations'/><author><name>Abhilash Vuyyuru</name><uri>https://profiles.google.com/107328798079149921932</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh6.googleusercontent.com/-NxAgT10-nrk/AAAAAAAAAAI/AAAAAAAAB0o/Gz3VMjlEXb0/s512-c/photo.jpg'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-18313336.post-80380732802069619</id><published>2007-04-03T15:28:00.000-04:00</published><updated>2007-04-03T16:16:26.417-04:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='javascript'/><category scheme='http://www.blogger.com/atom/ns#' term='example/sample code'/><title type='text'>Using JSON from Java</title><content type='html'>JSON (JavaScript Object Notation) is a lightweight data-interchange format based on a subset of the JavaScript Programming Language. JSON object structure is built on two structures:&lt;ul&gt;&lt;li&gt;A collection of name/value pairs: An &lt;span style="font-style: italic;"&gt;associative array in Javascript which is analogous to the Java Map&lt;/span&gt;.&lt;/li&gt;&lt;li&gt;An ordered list of values: Analogous to a Java Array.&lt;/li&gt;&lt;/ul&gt;The rest of the datatypes (string, number, true/false etc.) have straight forward mappings to the Java datatypes.&lt;span class="fullpost"&gt;&lt;br /&gt;&lt;span style="font-size:130%;"&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-family: arial;"&gt;Producing JSON from Java&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Since JSON objects can be declared literally in JavaScript, it is possible to create these literals from Java classes. There are a couple of ways to achieve this&lt;ul&gt;&lt;li&gt;&lt;span style="font-weight: bold;"&gt;Using JSONObject&lt;/span&gt;: The source code for JSONObject and the supporting classes is available at &lt;a href="http://www.json.org/java/index.html"&gt;http://www.json.org/java/index.html&lt;/a&gt;. Using this you can create JSONObject from a Java class as shown below&lt;pre&gt;  JSONObject json = new JSONObject();&lt;br /&gt; json.put("name1",value1);&lt;br /&gt; json.put("name2",value2);&lt;/pre&gt; The JSONObject class will appear as a Javascript Associate array which looks like this&lt;pre&gt; {"name1": "value1",&lt;br /&gt; "name2": "value2"}&lt;/pre&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-weight: bold;"&gt;Using JSON-lib&lt;/span&gt;: This is a Java library (available on sourceforge) that provides utility methods to convert different Java objects into JSONObject. This method is used in the example shown below.&lt;/li&gt;&lt;/ul&gt;The following Java class and JSP demonstrate how to use JSON with Java&lt;ol&gt;&lt;li&gt;The Java class:&lt;pre&gt;package beans;&lt;br /&gt;&lt;br /&gt;import java.util.Map;&lt;br /&gt;import java.util.TreeMap;&lt;br /&gt;&lt;br /&gt;import net.sf.json.JSONObject;&lt;br /&gt;&lt;br /&gt;public class MapHolder {&lt;br /&gt; private Map&lt;string, string=""&gt; map;&lt;br /&gt;&lt;br /&gt; public Map getMap() {&lt;br /&gt;   return map;&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; public void setMap(Map&lt;string, string=""&gt; map) {&lt;br /&gt;   this.map = map;&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;  public JSONObject getJsonObject() {&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;    JSONObject obj = JSONObject.fromMap(map);&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;    return obj;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;  }&lt;/span&gt;&lt;br /&gt;&lt;br /&gt; public MapHolder() {&lt;br /&gt;   map = new TreeMap&lt;string, string=""&gt;();&lt;br /&gt;   map.put("name1", "value1");&lt;br /&gt;   map.put("name2", "value2");&lt;br /&gt;&lt;br /&gt; }&lt;br /&gt;}&lt;br /&gt;&lt;/string,&gt;&lt;/string,&gt;&lt;/string,&gt;&lt;/pre&gt;&lt;center&gt;MapHolder.java&lt;/center&gt;&lt;ul&gt;&lt;li&gt;The getJsonObject() method uses  JSON-lib to convert the Map to a JSONObject.&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;li&gt;The JSP: &lt;pre&gt;&amp;lt;%@page import="beans.MapHolder,net.sf.json.JSONObject"%&amp;gt;&lt;br /&gt;&amp;lt;html&amp;gt;&lt;br /&gt;&amp;lt;head&amp;gt;&lt;br /&gt;&amp;lt;title&amp;gt;Title&amp;lt;/title&amp;gt;&lt;br /&gt;&amp;lt;script&amp;gt;&lt;br /&gt;function getInfo(o)&lt;br /&gt;{&lt;br /&gt; var obj=o;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;  document.getElementById('test').innerHTML=obj.name1;&lt;/span&gt;&lt;br /&gt;}&lt;br /&gt;&amp;lt;/script&amp;gt;&lt;br /&gt;&amp;lt;/head&amp;gt;&lt;br /&gt;&amp;lt;body&amp;gt;&lt;br /&gt;&amp;lt;%&lt;br /&gt;JSONObject obj = (new MapHolder()).getJsonObject();&lt;br /&gt;%&amp;gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;&amp;lt;input type="button" value="submit" onclick='getInfo(&amp;lt;%=obj%&amp;gt;)' /&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;div id="test"&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;&amp;lt;/body&amp;gt;&lt;br /&gt;&amp;lt;/html&amp;gt;&lt;/pre&gt;&lt;center&gt;index.jsp&lt;/center&gt;&lt;ul&gt;&lt;li&gt;Note the "obj.name1" in the script to access the element from the associative array.&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;The JAR files: In order to use JSON-lib, you will need to include the following jars in your classpath&lt;ul&gt;&lt;li&gt;commons-lang.jar&lt;/li&gt;&lt;li&gt;commons-logging.jar&lt;/li&gt;&lt;li&gt;commons-beanutils.jar&lt;/li&gt;&lt;li&gt;ezmorph-1.0.1.jar (available from &lt;a href="http://ezmorph.sourceforge.net/"&gt;sourceforge ezmorph&lt;/a&gt;)&lt;/li&gt;&lt;li&gt;json-lib-1.1-jdk15.jar (from &lt;a href="http://json-lib.sourceforge.net/index.html"&gt;sourceforge json-lib&lt;/a&gt;).&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;/ol&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/18313336-80380732802069619?l=java-x.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://java-x.blogspot.com/feeds/80380732802069619/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://java-x.blogspot.com/2007/04/using-json-from-java.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/18313336/posts/default/80380732802069619'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/18313336/posts/default/80380732802069619'/><link rel='alternate' type='text/html' href='http://java-x.blogspot.com/2007/04/using-json-from-java.html' title='Using JSON from Java'/><author><name>Abhilash Vuyyuru</name><uri>https://profiles.google.com/107328798079149921932</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh6.googleusercontent.com/-NxAgT10-nrk/AAAAAAAAAAI/AAAAAAAAB0o/Gz3VMjlEXb0/s512-c/photo.jpg'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-18313336.post-789144238968269772</id><published>2007-03-30T17:04:00.000-04:00</published><updated>2007-03-30T17:14:16.054-04:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='ajax'/><category scheme='http://www.blogger.com/atom/ns#' term='example/sample code'/><title type='text'>Reverse Ajax with Direct Web Remoting (DWR)</title><content type='html'>&lt;span style="font-weight: bold;"&gt;Direct Web Remoting (DWR)&lt;/span&gt;, is an open source Java library that can be used to implement Ajax in Java web applications with minimal Javascript coding. Using DWR, we can invoke server-side Java methods from Javascript in the browser. DWR 2.0 introduces a new feature, dubbed "Reverse Ajax", using which server-side Java can "push" updates to the browser. In this post, I tried to use a simplistic web application that will demonstrate the use of DWR for "Reverse Ajax".&lt;br /&gt;In this example, I use a servlet that will be pushing information to the browser clients. Here is how to implement the example.&lt;span class="fullpost"&gt;&lt;ol&gt;&lt;li&gt;Download &lt;a href="http://getahead.org/dwr/download"&gt;DWR 2.0&lt;/a&gt; from here, dwr.jar file has to be included in the classpath.&lt;/li&gt;&lt;li&gt;&lt;span style="font-weight: bold;"&gt;Create the Service&lt;/span&gt;: This service generates messages which will be written to the browser. Here is the code for the Service.&lt;pre&gt;package utils;&lt;br /&gt;&lt;br /&gt;import java.util.ArrayList;&lt;br /&gt;import java.util.Collection;&lt;br /&gt;import java.util.List;&lt;br /&gt;&lt;br /&gt;import org.directwebremoting.ServerContext;&lt;br /&gt;import org.directwebremoting.proxy.dwr.Util;&lt;br /&gt;&lt;br /&gt;public class Service {&lt;br /&gt;&lt;br /&gt; private int count = 0;&lt;br /&gt;&lt;br /&gt; public void update(ServerContext wctx) {&lt;br /&gt;   List&amp;lt;Data&amp;gt; messages = new ArrayList&amp;lt;Data&amp;gt;();&lt;br /&gt;   messages.add(new Data("testing" + count++));&lt;br /&gt;&lt;br /&gt;   // Collection sessions = wctx.getAllScriptSessions();&lt;br /&gt;   Collection sessions = wctx.getScriptSessionsByPage("/ReverseAjax/index.html");&lt;br /&gt;   Util utilAll = new Util(sessions);&lt;br /&gt;   utilAll.addOptions("updates", messages, "value");&lt;br /&gt; }&lt;br /&gt;}&lt;/pre&gt;&lt;center&gt;Service.java&lt;/center&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-weight: bold;"&gt;Create the Message Container&lt;/span&gt;: The message container is a simple Java bean that holds the message.&lt;pre&gt;package utils;&lt;br /&gt;&lt;br /&gt;public class Data {&lt;br /&gt; private String value;&lt;br /&gt;&lt;br /&gt; public Data() {&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; public Data(String value) {&lt;br /&gt;   this.value = value;&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; public String getValue() {&lt;br /&gt;   return value;&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; public void setValue(String value) {&lt;br /&gt;   this.value = value;&lt;br /&gt; }&lt;br /&gt;}&lt;/pre&gt;&lt;center&gt;Data.java&lt;/center&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-weight: bold;"&gt;Create the Servlet&lt;/span&gt;: Here is the code for the Servlet.&lt;pre&gt;package servlets;&lt;br /&gt;&lt;br /&gt;import java.io.IOException;&lt;br /&gt;import java.io.PrintWriter;&lt;br /&gt;&lt;br /&gt;import javax.servlet.ServletException;&lt;br /&gt;import javax.servlet.http.HttpServletRequest;&lt;br /&gt;import javax.servlet.http.HttpServletResponse;&lt;br /&gt;&lt;br /&gt;import org.directwebremoting.ServerContext;&lt;br /&gt;import org.directwebremoting.ServerContextFactory;&lt;br /&gt;&lt;br /&gt;import utils.Service;&lt;br /&gt;&lt;br /&gt;public class TestServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {&lt;br /&gt;&lt;br /&gt; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {&lt;br /&gt;   Service service = new Service();&lt;br /&gt;   ServerContext wctx = ServerContextFactory.get(this.getServletContext());&lt;br /&gt;   for (int i = 0; i &amp;lt; 10; i++) {&lt;br /&gt;     service.update(wctx);&lt;br /&gt;     try {&lt;br /&gt;       Thread.sleep(1000);&lt;br /&gt;     }&lt;br /&gt;     catch (InterruptedException e) {&lt;br /&gt;       e.printStackTrace();&lt;br /&gt;     }&lt;br /&gt;   }&lt;br /&gt;   PrintWriter writer = response.getWriter();&lt;br /&gt;   writer.println("Done");&lt;br /&gt;   writer.close();&lt;br /&gt;&lt;br /&gt; }}&lt;/pre&gt;&lt;center&gt;TestServlet.java&lt;/center&gt;&lt;ul&gt;&lt;li&gt;The &lt;span style="font-weight: bold;"&gt;ServerContext&lt;/span&gt; is used by DWR to get information of the clients that have open sessions on the server.&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-weight: bold;"&gt;The Web Page&lt;/span&gt;: This is the code for the Web Page (index.html).&lt;pre&gt;&amp;lt;html xmlns="http://www.w3.org/1999/xhtml"&amp;gt;&lt;br /&gt;&amp;lt;head&amp;gt;&lt;br /&gt;&amp;lt;title&amp;gt;index&amp;lt;/title&amp;gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;&amp;lt;script type='text/javascript' src='dwr/engine.js'&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;&amp;lt;script type='text/javascript' src='dwr/interface/Service.js'&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;&amp;lt;script type='text/javascript' src='dwr/util.js'&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;&lt;br /&gt;&amp;lt;/head&amp;gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;&amp;lt;body onload="dwr.engine.setActiveReverseAjax(true);"&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;&amp;lt;ul id="updates"&amp;gt;&lt;/span&gt;&lt;br /&gt;&amp;lt;/ul&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;/body&amp;gt;&lt;br /&gt;&amp;lt;/html&amp;gt;&lt;/pre&gt;&lt;center&gt;index.html&lt;/center&gt;&lt;ul&gt;&lt;li&gt;engine.js handles all server communications.&lt;/li&gt;&lt;li&gt;util.js helps you alter web pages with the data you got from the server.&lt;/li&gt;&lt;li&gt;The path to the scripts is relative to the root of the web content. The DWR servlet (defined in the web.xml file) will provide these scripts.&lt;/li&gt;&lt;li&gt;dwr.engine.setActiveReverseAjax(true); is used to activate Reverse Ajax&lt;/li&gt;&lt;li&gt;The id of the list is the same as the parameter set in the Service.&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-weight: bold;"&gt;The Web Deployment Descriptor&lt;/span&gt;:&lt;pre&gt;&amp;lt;?xml version="1.0" encoding="UTF-8"?&amp;gt;&lt;br /&gt;&amp;lt;web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"&lt;br /&gt; xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&lt;br /&gt; xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"&amp;gt;&lt;br /&gt; &amp;lt;display-name&amp;gt;ReverseAjax&amp;lt;/display-name&amp;gt;&lt;br /&gt;&lt;br /&gt; &amp;lt;servlet&amp;gt;&lt;br /&gt;   &amp;lt;servlet-name&amp;gt;dwr-invoker&amp;lt;/servlet-name&amp;gt;&lt;br /&gt;   &amp;lt;servlet-class&amp;gt;org.directwebremoting.servlet.DwrServlet&amp;lt;/servlet-class&amp;gt;&lt;br /&gt;   &amp;lt;init-param&amp;gt;&lt;br /&gt;     &amp;lt;param-name&amp;gt;debug&amp;lt;/param-name&amp;gt;&lt;br /&gt;     &amp;lt;param-value&amp;gt;true&amp;lt;/param-value&amp;gt;&lt;br /&gt;   &amp;lt;/init-param&amp;gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;    &amp;lt;init-param&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;      &amp;lt;param-name&amp;gt;activeReverseAjaxEnabled&amp;lt;/param-name&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;      &amp;lt;param-value&amp;gt;true&amp;lt;/param-value&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;    &amp;lt;/init-param&amp;gt;&lt;/span&gt;&lt;br /&gt;   &amp;lt;load-on-startup&amp;gt;1&amp;lt;/load-on-startup&amp;gt;&lt;br /&gt; &amp;lt;/servlet&amp;gt;&lt;br /&gt; &amp;lt;servlet&amp;gt;&lt;br /&gt;   &amp;lt;servlet-name&amp;gt;TestServlet&amp;lt;/servlet-name&amp;gt;&lt;br /&gt;   &amp;lt;servlet-class&amp;gt;servlets.TestServlet&amp;lt;/servlet-class&amp;gt;&lt;br /&gt; &amp;lt;/servlet&amp;gt;&lt;br /&gt;&lt;br /&gt; &amp;lt;servlet-mapping&amp;gt;&lt;br /&gt;   &amp;lt;servlet-name&amp;gt;dwr-invoker&amp;lt;/servlet-name&amp;gt;&lt;br /&gt;   &amp;lt;url-pattern&amp;gt;/dwr/*&amp;lt;/url-pattern&amp;gt;&lt;br /&gt; &amp;lt;/servlet-mapping&amp;gt;&lt;br /&gt; &amp;lt;servlet-mapping&amp;gt;&lt;br /&gt;   &amp;lt;servlet-name&amp;gt;TestServlet&amp;lt;/servlet-name&amp;gt;&lt;br /&gt;   &amp;lt;url-pattern&amp;gt;/testServlet&amp;lt;/url-pattern&amp;gt;&lt;br /&gt; &amp;lt;/servlet-mapping&amp;gt;&lt;br /&gt;&lt;br /&gt; &amp;lt;welcome-file-list&amp;gt;&lt;br /&gt;   &amp;lt;welcome-file&amp;gt;index.html&amp;lt;/welcome-file&amp;gt;&lt;br /&gt; &amp;lt;/welcome-file-list&amp;gt;&lt;br /&gt;&amp;lt;/web-app&gt;&lt;/pre&gt;&lt;center&gt;WEB-INF/web.xml&lt;/center&gt;&lt;ul&gt;&lt;li&gt;The DWR Servlet has to be loaded on startup&lt;/li&gt;&lt;li&gt;Setting activeReverseAjaxEnabled to true sets Reverse Ajax to be active. In this case Reverse Ajax used will be through polling or comet requests (extended http requests). If this is false, then inactive Reverse Ajax (piggybacking) will be used. In this case, the server waits for requests from the client and piggybacks the updates with the response.&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-weight: bold;"&gt;The DWR Configuration&lt;/span&gt;: The DWR configuration is defined in the dwr.xml file&lt;pre&gt;&amp;lt;?xml version="1.0" encoding="UTF-8"?&amp;gt;&lt;br /&gt;&amp;lt;!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN" "http://getahead.org/dwr/dwr20.dtd"&amp;gt;&lt;br /&gt;&amp;lt;dwr&amp;gt;&lt;br /&gt; &amp;lt;allow&amp;gt;&lt;br /&gt;   &amp;lt;create creator="new" javascript="Service" scope="application"&amp;gt;&lt;br /&gt;     &amp;lt;param name="class" value="utils.Service" /&amp;gt;&lt;br /&gt;   &amp;lt;/create&amp;gt;&lt;br /&gt;   &amp;lt;convert converter="bean" match="utils.Data" /&amp;gt;&lt;br /&gt; &amp;lt;/allow&amp;gt;&lt;br /&gt;&amp;lt;/dwr&gt;&lt;/pre&gt;&lt;center&gt;WEB-INF/dwr.xml&lt;/center&gt;&lt;ul&gt;&lt;li&gt;"create" is used to define a Java object as being available to Javascript code.&lt;/li&gt;&lt;li&gt;The "javascript" attribute is the one that will be used in case of invoking the server-side methods from Javascript.&lt;/li&gt;&lt;li&gt;The converter definition allows utils.Data to be used as a parameter.&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;span style="font-weight: bold;"&gt;Environment&lt;/span&gt;: This example was tested using DWR 2.0 RC 3, on Tomcat 5.5&lt;/li&gt;&lt;br /&gt;&lt;/ol&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/18313336-789144238968269772?l=java-x.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://java-x.blogspot.com/feeds/789144238968269772/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://java-x.blogspot.com/2007/03/reverse-ajax-with-direct-web-remoting.html#comment-form' title='12 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/18313336/posts/default/789144238968269772'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/18313336/posts/default/789144238968269772'/><link rel='alternate' type='text/html' href='http://java-x.blogspot.com/2007/03/reverse-ajax-with-direct-web-remoting.html' title='Reverse Ajax with Direct Web Remoting (DWR)'/><author><name>Abhilash Vuyyuru</name><uri>https://profiles.google.com/107328798079149921932</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh6.googleusercontent.com/-NxAgT10-nrk/AAAAAAAAAAI/AAAAAAAAB0o/Gz3VMjlEXb0/s512-c/photo.jpg'/></author><thr:total>12</thr:total></entry><entry><id>tag:blogger.com,1999:blog-18313336.post-2670503055813893226</id><published>2007-03-15T10:25:00.000-04:00</published><updated>2007-03-15T10:54:30.350-04:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Guice'/><category scheme='http://www.blogger.com/atom/ns#' term='example/sample code'/><title type='text'>My First Guice Web Application</title><content type='html'>&lt;span style="font-weight: bold;"&gt;Google Guice&lt;/span&gt; is an open source Dependency Injection framework from Google. Guice is based on annotations and generics unlike Spring which depends on XML of Java for wiring dependencies. Guice injects constructors, fields and methods (any methods with any number of arguments, not just setters). Guice provides support for custom scopes, static member injection, Spring as well as Struts 2.x integration and AOP Alliance method interception. Guice is available at &lt;a href="http://code.google.com/p/google-guice/"&gt;Google Code&lt;/a&gt;. You can find the User guide and Javadocs from there. This post describes how I implemented a simple Web application using Guice. For injecting dependencies into Servlets, I used a Listener (this idea was from Hani Suleiman in the &lt;a href="http://groups.google.com/group/google-guice/browse_thread/thread/be2b215c57e049db"&gt;Guice developer mailing list&lt;/a&gt;). Follow these steps to run the example.&lt;span class="fullpost"&gt;&lt;ol&gt;&lt;li&gt;Download Guice from the Guice Site.&lt;/li&gt;&lt;li&gt;Create the Service Interface and Implementation: These are fairly simple as shown below&lt;pre&gt;package services;&lt;br /&gt;&lt;br /&gt;public interface Service {&lt;br /&gt; public String doWork();&lt;br /&gt;}&lt;/pre&gt;&lt;center&gt;Service.java&lt;/center&gt;&lt;pre&gt;package services;&lt;br /&gt;&lt;br /&gt;public class ServiceImpl implements Service {&lt;br /&gt;&lt;br /&gt; private int count  = 0;&lt;br /&gt; public String doWork() {&lt;br /&gt;  count++;&lt;br /&gt;  System.out.println("Service Impl Doing work " + count);&lt;br /&gt;   return("ServiceImpl : " + count);&lt;br /&gt; }&lt;br /&gt;}&lt;/pre&gt;&lt;center&gt;ServiceImpl.java&lt;/center&gt;&lt;/li&gt;&lt;li&gt;Create an Annotation: Guice allows you to select which implementation of an Interface you want to inject, by using annotations. You will see this used in the TestServlet. Here is the annotation I defined&lt;pre&gt;package annotations;&lt;br /&gt;&lt;br /&gt;import java.lang.annotation.ElementType;&lt;br /&gt;import java.lang.annotation.Retention;&lt;br /&gt;import java.lang.annotation.RetentionPolicy;&lt;br /&gt;import java.lang.annotation.Target;&lt;br /&gt;&lt;br /&gt;import com.google.inject.BindingAnnotation;&lt;br /&gt;&lt;br /&gt; &lt;span style="font-weight: bold;"&gt;@Retention(RetentionPolicy.RUNTIME)&lt;/span&gt;&lt;br /&gt; @Target( { ElementType.FIELD, ElementType.PARAMETER })&lt;br /&gt; &lt;span style="font-weight: bold;"&gt;@BindingAnnotation&lt;/span&gt;&lt;br /&gt; public @interface MyAnnotation {&lt;br /&gt;&lt;br /&gt;}&lt;/pre&gt;&lt;center&gt;MyAnnotation.java&lt;/center&gt;&lt;ul&gt;&lt;li&gt;&lt;span style="font-weight: bold;"&gt;@Rentention(RUNTIME)&lt;/span&gt; makes the annotation visible at runtime.(this is mandatory when using Guice)&lt;/li&gt;&lt;li&gt;&lt;span style="font-weight: bold;"&gt;@Target({FIELD, PARAMETER})&lt;/span&gt; prevents the annotation from being applied to methods, types, local variables, and other annotations.(This is optional)&lt;/li&gt;&lt;li&gt;&lt;span style="font-weight: bold;"&gt;@BindingAnnotation&lt;/span&gt; is a Guice-specific signal that you wish your annotation to be used in this way.  Guice will produce an error whenever user applies more than one binding annotation to the same injectable element. (This is necessary when used for binding with Guice)&lt;/li&gt;&lt;li&gt;Optionally, you can also use annotations with attributes to differentiate between bindings.&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;li&gt;Create a Guice Module: Guice Modules are used to define bindings within an applications. It is possible to have multiple modules per application (and multiple applications can share a common module too). Here is the simple Module that I implemented.&lt;pre&gt;package modules;&lt;br /&gt;&lt;br /&gt;import services.Service;&lt;br /&gt;import services.ServiceImpl;&lt;br /&gt;import annotations.MyAnnotation;&lt;br /&gt;&lt;br /&gt;import com.google.inject.AbstractModule;&lt;br /&gt;import com.google.inject.Scopes;&lt;br /&gt;&lt;br /&gt;public class MyModule extends AbstractModule {&lt;br /&gt;&lt;br /&gt; public void configure() {&lt;br /&gt;  &lt;span style="font-weight: bold;"&gt;bind(Service.class).annotatedWith(MyAnnotation.class).to(ServiceImpl.class).in(Scopes.SINGLETON);&lt;/span&gt;&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt;}&lt;/pre&gt;&lt;center&gt;MyModule.java&lt;/center&gt;&lt;ul&gt;&lt;li&gt;Guice provides the module with a binder which is used to bind the Interfaces with implementations.&lt;/li&gt;&lt;li&gt;annotatedWith(MyAnnotation.class) defines the binding to the implementation based on the annotation.&lt;/li&gt;&lt;li&gt;in(Scopes.SINGLETON) is used to define this implementation as a Singleton &lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;li&gt;Create a Listener: Here is the Code for a simple listener (for a more complete example see the above mentioned mailing list).&lt;pre&gt;package listeners;&lt;br /&gt;&lt;br /&gt;import javax.servlet.ServletContext;&lt;br /&gt;import javax.servlet.ServletContextEvent;&lt;br /&gt;import javax.servlet.ServletContextListener;&lt;br /&gt;&lt;br /&gt;import modules.MyModule;&lt;br /&gt;&lt;br /&gt;import com.google.inject.Guice;&lt;br /&gt;import com.google.inject.Injector;&lt;br /&gt;import com.google.inject.Module;&lt;br /&gt;import com.google.inject.servlet.ServletModule;&lt;br /&gt;&lt;br /&gt;public class GuiceServletContextListener implements ServletContextListener {&lt;br /&gt;&lt;br /&gt; public void contextDestroyed(ServletContextEvent servletContextEvent) {&lt;br /&gt;   ServletContext servletContext = servletContextEvent.getServletContext();&lt;br /&gt;   servletContext.removeAttribute(Injector.class.getName());&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; public void contextInitialized(ServletContextEvent servletContextEvent) {&lt;br /&gt;   &lt;span style="font-weight: bold;"&gt;Injector injector = Guice.createInjector(new Module[] { new MyModule(), new ServletModule() });&lt;/span&gt;&lt;br /&gt;   ServletContext servletContext = servletContextEvent.getServletContext();&lt;br /&gt;   servletContext.setAttribute(Injector.class.getName(), injector);&lt;br /&gt; }&lt;br /&gt;}&lt;/pre&gt;&lt;center&gt;GuiceServletContextListener.java&lt;/center&gt;&lt;ul&gt;&lt;li&gt;The listener is used to save the Injector in the servlet context to be used in the AbstractInjectableServlet.&lt;/li&gt;&lt;li&gt;An injector is to be built during startup and used to inject objects at runtime.&lt;/li&gt;&lt;li&gt;&lt;span style="font-weight: bold;"&gt;ServletModule&lt;/span&gt; is used for servlet-specific bindings. It adds a request/session scope, as well as allowing the injection of request,response, and request parameters into your beans. To use it you have to register GuiceFilter in your web.xml.&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;li&gt;Create an Abstract Servlet: The AbstractInjectableServlet will be super class of all the Servlets you use. This class uses the injector to inject dependencies in the concrete classes by the injector.injectMembers(this) method.&lt;pre&gt;package servlets;&lt;br /&gt;&lt;br /&gt;import javax.servlet.ServletConfig;&lt;br /&gt;import javax.servlet.ServletContext;&lt;br /&gt;import javax.servlet.ServletException;&lt;br /&gt;import javax.servlet.http.HttpServlet;&lt;br /&gt;&lt;br /&gt;import com.google.inject.Injector;&lt;br /&gt;&lt;br /&gt;public abstract class AbstractInjectableServlet extends HttpServlet {&lt;br /&gt;&lt;br /&gt; @Override&lt;br /&gt; public void init(ServletConfig config) throws ServletException{&lt;br /&gt;  &lt;br /&gt;   ServletContext context = config.getServletContext();&lt;br /&gt;   &lt;span style="font-weight: bold;"&gt;Injector injector = (Injector) context.getAttribute(Injector.class.getName());&lt;/span&gt;&lt;br /&gt;   if (injector == null) {&lt;br /&gt;     throw new ServletException("Guice Injector not found");&lt;br /&gt;   }&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;    injector.injectMembers(this);&lt;/span&gt;&lt;br /&gt;&lt;br /&gt; }&lt;br /&gt;}&lt;/pre&gt;&lt;center&gt;AbstractInjectableServlet.java&lt;/center&gt;&lt;/li&gt;&lt;li&gt;Create the Test Servlet: Here is the code for the TestServlet&lt;pre&gt;package servlets;&lt;br /&gt;&lt;br /&gt;import java.io.IOException;&lt;br /&gt;&lt;br /&gt;import javax.servlet.ServletException;&lt;br /&gt;import javax.servlet.http.HttpServletRequest;&lt;br /&gt;import javax.servlet.http.HttpServletResponse;&lt;br /&gt;&lt;br /&gt;import services.Service;&lt;br /&gt;import annotations.MyAnnotation;&lt;br /&gt;&lt;br /&gt;import com.google.inject.Inject;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;public class TestServlet extends AbstractInjectableServlet implements javax.servlet.Servlet {&lt;br /&gt; &lt;br /&gt; private Service service;&lt;br /&gt; public TestServlet() {&lt;br /&gt;  super();&lt;br /&gt; }    &lt;br /&gt; &lt;br /&gt;&lt;span style="font-weight: bold;"&gt;  @Inject&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;  public void setServiceInstance(@MyAnnotation Service service) {&lt;/span&gt;&lt;br /&gt;   this.service = service;&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {&lt;br /&gt;  response.getWriter().println("Service instance : " + service.doWork());&lt;br /&gt; }            &lt;br /&gt;}&lt;/pre&gt;&lt;center&gt;TestServlet.java&lt;/center&gt;&lt;ul&gt;&lt;li&gt;The @Inject is used to define methods to be injectable.&lt;/li&gt;&lt;li&gt;The @MyAnnotation for the Service parameter means that, the actual binding will be done with the implementation that is bound using the withAnnotation() method.&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;li&gt;The Web Deployment Descriptor&lt;pre&gt;&amp;lt;?xml version="1.0" encoding="UTF-8"?&amp;gt;&lt;br /&gt;&amp;lt;web-app id="WebApp_ID" version="2.4"&lt;br /&gt; xmlns="http://java.sun.com/xml/ns/j2ee"&lt;br /&gt; xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&lt;br /&gt; xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"&amp;gt;&lt;br /&gt; &amp;lt;display-name&amp;gt;GuiceWeb&amp;lt;/display-name&amp;gt;&lt;br /&gt; &amp;lt;listener&amp;gt;&lt;br /&gt;  &amp;lt;listener-class&amp;gt;listeners.GuiceServletContextListener&amp;lt;/listener-class&amp;gt;&lt;br /&gt; &amp;lt;/listener&amp;gt;&lt;br /&gt; &amp;lt;servlet&amp;gt;&lt;br /&gt;  &amp;lt;description&amp;gt;&amp;lt;/description&amp;gt;&lt;br /&gt;  &amp;lt;display-name&amp;gt;TestServlet&amp;lt;/display-name&amp;gt;&lt;br /&gt;  &amp;lt;servlet-name&amp;gt;TestServlet&amp;lt;/servlet-name&amp;gt;&lt;br /&gt;  &amp;lt;servlet-class&amp;gt;servlets.TestServlet&amp;lt;/servlet-class&amp;gt;&lt;br /&gt; &amp;lt;/servlet&amp;gt;&lt;br /&gt; &amp;lt;servlet-mapping&amp;gt;&lt;br /&gt;  &amp;lt;servlet-name&amp;gt;TestServlet&amp;lt;/servlet-name&amp;gt;&lt;br /&gt;  &amp;lt;url-pattern&amp;gt;/testServlet&amp;lt;/url-pattern&amp;gt;&lt;br /&gt;&lt;br /&gt; &amp;lt;/servlet-mapping&amp;gt;&lt;br /&gt;&lt;br /&gt; &amp;lt;welcome-file-list&amp;gt;&lt;br /&gt;  &amp;lt;welcome-file&amp;gt;default.jsp&amp;lt;/welcome-file&amp;gt;&lt;br /&gt; &amp;lt;/welcome-file-list&amp;gt;&lt;br /&gt;&amp;lt;/web-app&amp;gt;&lt;/pre&gt;&lt;center&gt;web.xml&lt;/center&gt;&lt;/li&gt;&lt;/ol&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/18313336-2670503055813893226?l=java-x.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://java-x.blogspot.com/feeds/2670503055813893226/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://java-x.blogspot.com/2007/03/my-first-guice-web-application.html#comment-form' title='9 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/18313336/posts/default/2670503055813893226'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/18313336/posts/default/2670503055813893226'/><link rel='alternate' type='text/html' href='http://java-x.blogspot.com/2007/03/my-first-guice-web-application.html' title='My First Guice Web Application'/><author><name>Abhilash Vuyyuru</name><uri>https://profiles.google.com/107328798079149921932</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh6.googleusercontent.com/-NxAgT10-nrk/AAAAAAAAAAI/AAAAAAAAB0o/Gz3VMjlEXb0/s512-c/photo.jpg'/></author><thr:total>9</thr:total></entry><entry><id>tag:blogger.com,1999:blog-18313336.post-5954367555941376104</id><published>2007-03-12T14:53:00.000-04:00</published><updated>2007-03-14T07:57:32.980-04:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Java EE 5'/><category scheme='http://www.blogger.com/atom/ns#' term='messaging'/><category scheme='http://www.blogger.com/atom/ns#' term='example/sample code'/><title type='text'>Message Driven Bean in Java EE 5 : Part 2</title><content type='html'>In a previous post, I described how to implement &lt;a href="http://java-x.blogspot.com/2006/12/message-driven-bean-in-java-ee-5.html"&gt;Messaging in Java EE 5&lt;/a&gt; using annotation. This post is an extension of that post to describe how to implement Messaging in Java EE 5 using a deployment descriptor. To implement this, you can use the same client that was described in the other post. The only change needed is in the MDB part. Here's how to implement a Message Driven bean using the deployment descriptor.&lt;span class="fullpost"&gt;&lt;ol&gt;&lt;li&gt;Create the Message driven bean: The message driven bean here is a simple Java class that implements the MessageListener interface. All the configuration will be done in the deployment descriptors.&lt;pre&gt;package jms;&lt;br /&gt;&lt;br /&gt;import javax.jms.JMSException;&lt;br /&gt;import javax.jms.Message;&lt;br /&gt;import javax.jms.MessageListener;&lt;br /&gt;import javax.jms.TextMessage;&lt;br /&gt;&lt;br /&gt;import org.apache.log4j.Level;&lt;br /&gt;import org.apache.log4j.Logger;&lt;br /&gt;&lt;br /&gt;public class Messaging3Mdb implements MessageListener {&lt;br /&gt;&lt;br /&gt;  static final Logger logger = Logger.getLogger("MDB");&lt;br /&gt;&lt;br /&gt;  public Messaging3Mdb() {&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  public void onMessage(Message inMessage) {&lt;br /&gt;    TextMessage msg = null;&lt;br /&gt;    logger.setLevel(Level.ALL);&lt;br /&gt;&lt;br /&gt;    try {&lt;br /&gt;      if (inMessage instanceof TextMessage) {&lt;br /&gt;        msg = (TextMessage) inMessage;&lt;br /&gt;        logger.info("MESSAGE BEAN: Message received: " + msg.getText());&lt;br /&gt;      } else {&lt;br /&gt;        logger.warn("Message of wrong type: " + inMessage.getClass().getName());&lt;br /&gt;      }&lt;br /&gt;    } catch (JMSException e) {&lt;br /&gt;      logger.error("MessageBean.onMessage: JMSException: " + e.toString());&lt;br /&gt;    } catch (Throwable te) {&lt;br /&gt;      logger.error("MessageBean.onMessage: Exception: " + te.toString());&lt;br /&gt;    }&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;center&gt;Messaging3Mdb.java&lt;/center&gt;&lt;/li&gt;&lt;li&gt;The sun-ejb-jar.xml file: This file contains the mapping of the JMS resources.&lt;pre&gt;&amp;lt;?xml version="1.0" encoding="UTF-8"?&amp;gt;&lt;br /&gt;&amp;lt;sun-ejb-jar&amp;gt;&lt;br /&gt;  &amp;lt;enterprise-beans&amp;gt;&lt;br /&gt;    &amp;lt;name&amp;gt;Ejb3DD&amp;lt;/name&amp;gt;&lt;br /&gt;    &amp;lt;ejb&amp;gt;&lt;br /&gt;      &amp;lt;ejb-name&amp;gt;Messaging3Mdb&amp;lt;/ejb-name&amp;gt;&lt;br /&gt;      &amp;lt;jndi-name&amp;gt;jms/testQueue&amp;lt;/jndi-name&amp;gt;&lt;br /&gt;      &amp;lt;mdb-connection-factory&amp;gt;&lt;br /&gt;        &amp;lt;jndi-name&amp;gt;jms/connectionFactory&amp;lt;/jndi-name&amp;gt;&lt;br /&gt;      &amp;lt;/mdb-connection-factory&amp;gt;&lt;br /&gt;    &amp;lt;/ejb&amp;gt;&lt;br /&gt;  &amp;lt;/enterprise-beans&amp;gt;&lt;br /&gt;&amp;lt;/sun-ejb-jar&amp;gt;&lt;/pre&gt;&lt;center&gt;META-INF/sun-ejb-jar.xml&lt;/center&gt;&lt;/li&gt;&lt;/ol&gt;&lt;li&gt;The EJB deployment descriptor: &lt;pre&gt;&amp;lt;?xml version="1.0" encoding="UTF-8" ?&amp;gt;&lt;br /&gt;&amp;lt;ejb-jar&amp;gt;&lt;br /&gt;  &amp;lt;enterprise-beans&amp;gt;&lt;br /&gt;    &amp;lt;message-driven&amp;gt;&lt;br /&gt;      &amp;lt;ejb-name&amp;gt;Messaging3Mdb&amp;lt;/ejb-name&amp;gt;&lt;br /&gt;      &amp;lt;ejb-class&amp;gt;jms.Messaging3Mdb&amp;lt;/ejb-class&amp;gt;&lt;br /&gt;      &amp;lt;messaging-type&amp;gt;javax.jms.MessageListener&amp;lt;/messaging-type&amp;gt;&lt;br /&gt;      &amp;lt;message-destination-type&amp;gt;javax.jms.Queue&amp;lt;/message-destination-type&amp;gt;&lt;br /&gt;&lt;br /&gt;    &amp;lt;/message-driven&amp;gt;&lt;br /&gt;  &amp;lt;/enterprise-beans&amp;gt;&lt;br /&gt;&amp;lt;/ejb-jar&amp;gt;&lt;/pre&gt;&lt;center&gt;META-INF/ejb-jar.xml&lt;/center&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Environment: This example was implemented on Glassfish v1, Milestone 7&lt;/li&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/18313336-5954367555941376104?l=java-x.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://java-x.blogspot.com/feeds/5954367555941376104/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://java-x.blogspot.com/2007/03/message-driven-bean-in-java-ee-5-part-2.html#comment-form' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/18313336/posts/default/5954367555941376104'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/18313336/posts/default/5954367555941376104'/><link rel='alternate' type='text/html' href='http://java-x.blogspot.com/2007/03/message-driven-bean-in-java-ee-5-part-2.html' title='Message Driven Bean in Java EE 5 : Part 2'/><author><name>Abhilash Vuyyuru</name><uri>https://profiles.google.com/107328798079149921932</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh6.googleusercontent.com/-NxAgT10-nrk/AAAAAAAAAAI/AAAAAAAAB0o/Gz3VMjlEXb0/s512-c/photo.jpg'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-18313336.post-4088106765173276821</id><published>2007-03-09T14:18:00.000-05:00</published><updated>2007-03-09T14:20:04.159-05:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='spring'/><category scheme='http://www.blogger.com/atom/ns#' term='security'/><category scheme='http://www.blogger.com/atom/ns#' term='example/sample code'/><title type='text'>Securing Middle tier Objects with Acegi Security Framework</title><content type='html'>Previously, I posted an example on implementing Security using Acegi Security Framework for applications using Spring framework. This post will describe an example on how to secure Middle-tier objects using Acegi, with Role-based authorization. Here is how to implement the example.&lt;span class="fullpost"&gt;&lt;ol&gt;&lt;li&gt;Create the example project as shown in "&lt;a href="http://java-x.blogspot.com/2006/12/spring-security-with-acegi-security.html"&gt;Spring security with Acegi Security Framework&lt;/a&gt;". This will be the starting point.&lt;/li&gt;&lt;li&gt;Create the Secure Object: The secure object here is SecureDAO, which is a dummy object that has the common create, read, update, delete methods.&lt;pre&gt;package test;&lt;br /&gt;&lt;br /&gt;public class SecureDAO {&lt;br /&gt; public String create() {&lt;br /&gt;   return "create";&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; public String read() {&lt;br /&gt;   return "read";&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; public String update() {&lt;br /&gt;   return "update";&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; public String delete() {&lt;br /&gt;   return "delete";&lt;br /&gt; }&lt;br /&gt;}&lt;/pre&gt;&lt;center&gt;SecureDAO.java&lt;/center&gt;&lt;/li&gt;&lt;li&gt;Create the Test Servlet: The servlet is used to invoke the secure DAO object.&lt;pre&gt;package servlets;&lt;br /&gt;&lt;br /&gt;import java.io.IOException;&lt;br /&gt;&lt;br /&gt;import javax.servlet.ServletException;&lt;br /&gt;import javax.servlet.http.HttpServletRequest;&lt;br /&gt;import javax.servlet.http.HttpServletResponse;&lt;br /&gt;&lt;br /&gt;import org.springframework.web.context.WebApplicationContext;&lt;br /&gt;import org.springframework.web.context.support.WebApplicationContextUtils;&lt;br /&gt;&lt;br /&gt;import test.SecureDAO;&lt;br /&gt;&lt;br /&gt;public class TestServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {&lt;br /&gt;&lt;br /&gt; public TestServlet() {&lt;br /&gt;   super();&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {&lt;br /&gt;   String method = request.getParameter("method");&lt;br /&gt;&lt;br /&gt;   WebApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());&lt;br /&gt;&lt;br /&gt;   SecureDAO obj = (SecureDAO) context.getBean("secureDAO");&lt;br /&gt;   String result = "";&lt;br /&gt;   if (method.equals("create")) {&lt;br /&gt;     result = obj.create();&lt;br /&gt;   }&lt;br /&gt;   if (method.equals("read")) {&lt;br /&gt;     result = obj.read();&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   if (method.equals("update")) {&lt;br /&gt;     result = obj.update();&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   if (method.equals("delete")) {&lt;br /&gt;     result = obj.delete();&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   response.getWriter().println(result);&lt;br /&gt;&lt;br /&gt; }&lt;br /&gt;}&lt;/pre&gt;&lt;center&gt;TestServlet.java&lt;/center&gt;&lt;/li&gt;&lt;li&gt;Update the authenticatedusers.jsp file to invoke the TestSerlvet, as shown below&lt;pre&gt;&amp;lt;%@ page import="org.acegisecurity.context.SecurityContextHolder"%&amp;gt;&lt;br /&gt;&amp;lt;h1&amp;gt;Welcome: &amp;lt;%=SecurityContextHolder.getContext().getAuthentication().getName()%&amp;gt;&amp;lt;/h1&amp;gt;&lt;br /&gt;&amp;lt;p&amp;gt;&amp;lt;a href="../"&amp;gt;Home&amp;lt;/a&amp;gt;&lt;br /&gt;&amp;lt;form action="/SpringSecurity/TestServlet"&amp;gt;&amp;lt;select name="method"&amp;gt;&lt;br /&gt; &amp;lt;option value="create"&amp;gt;create&amp;lt;/option&amp;gt;&lt;br /&gt; &amp;lt;option value="read"&amp;gt;read&amp;lt;/option&amp;gt;&lt;br /&gt; &amp;lt;option value="update"&amp;gt;update&amp;lt;/option&amp;gt;&lt;br /&gt; &amp;lt;option value="delete"&amp;gt;delete&amp;lt;/option&amp;gt;&lt;br /&gt;&amp;lt;/select&amp;gt; &amp;lt;input type="submit" name="submit" /&amp;gt;&amp;lt;/form&amp;gt;&lt;br /&gt;&amp;lt;p&amp;gt;&amp;lt;a href="../j_acegi_logout"&amp;gt;Logout&amp;lt;/a&amp;gt;&lt;/pre&gt;&lt;center&gt;authenticatedusers.jsp&lt;/center&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Update the applicationContext.xml file to include the security definitions by adding the following bean definitions as shown below&lt;pre&gt;&amp;lt;bean id="methodSecurityInterceptor" class="org.acegisecurity.intercept.method.aopalliance.MethodSecurityInterceptor"&amp;gt;&lt;br /&gt;&amp;lt;property name="authenticationManager"&amp;gt;&lt;br /&gt; &amp;lt;ref bean="authenticationManager" /&amp;gt;&lt;br /&gt;&amp;lt;/property&amp;gt;&lt;br /&gt;&amp;lt;property name="accessDecisionManager"&amp;gt;&lt;br /&gt; &amp;lt;bean class="org.acegisecurity.vote.AffirmativeBased"&amp;gt;&lt;br /&gt; &amp;lt;property name="allowIfAllAbstainDecisions" value="false" /&amp;gt;&lt;br /&gt; &amp;lt;property name="decisionVoters"&amp;gt;&lt;br /&gt;   &amp;lt;list&amp;gt;&lt;br /&gt;   &amp;lt;bean class="org.acegisecurity.vote.RoleVoter" /&amp;gt;&lt;br /&gt;   &amp;lt;bean class="org.acegisecurity.vote.AuthenticatedVoter" /&amp;gt;&lt;br /&gt;   &amp;lt;/list&amp;gt;&lt;br /&gt; &amp;lt;/property&amp;gt;&lt;br /&gt; &amp;lt;/bean&amp;gt;&lt;br /&gt;&amp;lt;/property&amp;gt;&lt;br /&gt;&amp;lt;property name="objectDefinitionSource"&amp;gt;&lt;br /&gt; &amp;lt;value&amp;gt;&lt;br /&gt; test.SecureDAO.*=IS_AUTHENTICATED_REMEMBERED&lt;br /&gt; test.SecureDAO.delete=ROLE_ADMIN&lt;br /&gt; &amp;lt;/value&amp;gt;&lt;br /&gt;&amp;lt;/property&amp;gt;&lt;br /&gt;&amp;lt;/bean&amp;gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;bean id="autoProxyCreator" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"&amp;gt;&lt;br /&gt;&amp;lt;property name="interceptorNames"&amp;gt;&lt;br /&gt; &amp;lt;list&amp;gt;&lt;br /&gt; &amp;lt;value&amp;gt;methodSecurityInterceptor&amp;lt;/value&amp;gt;&lt;br /&gt; &amp;lt;/list&amp;gt;&lt;br /&gt;&amp;lt;/property&amp;gt;&lt;br /&gt;&amp;lt;property name="beanNames"&amp;gt;&lt;br /&gt; &amp;lt;list&amp;gt;&lt;br /&gt; &amp;lt;value&amp;gt;secureDAO&amp;lt;/value&amp;gt;&lt;br /&gt; &amp;lt;/list&amp;gt;&lt;br /&gt;&amp;lt;/property&amp;gt;&lt;br /&gt;&amp;lt;/bean&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;bean id="secureDAO" class="test.SecureDAO" /&amp;gt;&lt;/pre&gt;Note&lt;ul&gt;&lt;li&gt;The MethodSecurityInterceptor is used to define the mapping between the Methods and the roles that are allowed to invoke these methods.&lt;/li&gt;&lt;li&gt;The BeanNameAutoProxyCreator is used to create a proxy for the secureDAO object, so that an authorization check may be applied to every invocation on the object.&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;li&gt;Add the cglib JAR file available in the Spring download as a dependency to your project. This is used for creating the Proxy.&lt;/li&gt;&lt;/ol&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/18313336-4088106765173276821?l=java-x.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://java-x.blogspot.com/feeds/4088106765173276821/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://java-x.blogspot.com/2007/03/securing-middle-tier-objects-with-acegi.html#comment-form' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/18313336/posts/default/4088106765173276821'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/18313336/posts/default/4088106765173276821'/><link rel='alternate' type='text/html' href='http://java-x.blogspot.com/2007/03/securing-middle-tier-objects-with-acegi.html' title='Securing Middle tier Objects with Acegi Security Framework'/><author><name>Abhilash Vuyyuru</name><uri>https://profiles.google.com/107328798079149921932</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh6.googleusercontent.com/-NxAgT10-nrk/AAAAAAAAAAI/AAAAAAAAB0o/Gz3VMjlEXb0/s512-c/photo.jpg'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-18313336.post-1931557869613156409</id><published>2007-03-06T14:20:00.000-05:00</published><updated>2007-03-06T15:04:52.570-05:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='charts'/><category scheme='http://www.blogger.com/atom/ns#' term='example/sample code'/><title type='text'>Charts with JFreeChart 1</title><content type='html'>JFreeChart is a free Java chart library that can be used to display charts from Java applications. It features:&lt;ul&gt;&lt;li&gt;A wide range of chart types&lt;/li&gt;&lt;li&gt;support for many output types, including Swing components, image files (including PNG and JPEG), and vector graphics file formats (including PDF, EPS and SVG);&lt;/li&gt;&lt;li&gt;It is distributed under the terms of the LGPL, which permits use in proprietary applications.&lt;/li&gt;&lt;/ul&gt; In this post, I will describe how to use JFreeChart to display a comparative barchart, with data that contains multiple series which have to be grouped (G1, G2) per date. This effectively gives three levels of information (series, group and date). In order to display this data in a chart, we can use a StackedBarChart from JFreeChart. Please note that this is a result of my playing around with JFreeChart, and is not from the official documentation, which means that some of my assumptions may not be accurate. Follow these steps to run the example&lt;span class="fullpost"&gt;&lt;ol&gt;&lt;li&gt;Download JFreeChart from &lt;a href="http://www.jfree.org/jfreechart/"&gt;here&lt;/a&gt;.&lt;/li&gt;&lt;li&gt;Create the DataSet: The StakedBarChart takes a CategoryDataset as input. The following piece of code shows how to add data to the CategoryDataset.&lt;pre&gt;  DefaultCategoryDataset dataSet = new DefaultCategoryDataset();&lt;br /&gt;&lt;br /&gt;dataSet.addValue(100, "Series1 G1", "Jan 04");&lt;br /&gt;dataSet.addValue(150, "Series1 G2", "Jan 04");&lt;br /&gt;&lt;/pre&gt;It is important to note that the order in which you add data to the Dataset, as this would affect how you assign colors to the different series.&lt;/li&gt;&lt;li&gt;Create the renderer and assign series colors: &lt;pre&gt;  GroupedStackedBarRenderer renderer = new GroupedStackedBarRenderer();&lt;br /&gt;renderer.setItemMargin(0.01);&lt;br /&gt;renderer.setDrawBarOutline(false);&lt;br /&gt;&lt;br /&gt;renderer.setSeriesPaint(0, Color.blue);&lt;br /&gt;renderer.setSeriesPaint(1, Color.blue);&lt;/pre&gt;The order of the series in the Dataset, will dictate series number when assigning colors. You will see this in the full code shown below.&lt;/li&gt;&lt;li&gt;Map series to groups: This is done using the KeyToGroupMap.&lt;pre&gt;  KeyToGroupMap keytogroupmap = new KeyToGroupMap("G1");&lt;br /&gt;keytogroupmap.mapKeyToGroup("Series1 G1", "G1");&lt;br /&gt;keytogroupmap.mapKeyToGroup("Series2 G1", "G1");&lt;/pre&gt;&lt;/li&gt;&lt;li&gt;Create the Legend: Since I wanted to give same color to a given series in different groups, I had to add a legend collection that maps colors to the series&lt;pre&gt; private static LegendItemCollection createLegendItems() {&lt;br /&gt;  LegendItemCollection legenditemcollection = new LegendItemCollection();&lt;br /&gt;  LegendItem legenditem = new LegendItem("Series1", "-", null, null, Plot.DEFAULT_LEGEND_ITEM_BOX, Color.blue);&lt;br /&gt;  LegendItem legenditem1 = new LegendItem("Series2", "-", null, null, Plot.DEFAULT_LEGEND_ITEM_BOX, Color.cyan);&lt;br /&gt;  LegendItem legenditem2 = new LegendItem("Series3", "-", null, null, Plot.DEFAULT_LEGEND_ITEM_BOX, Color.green);&lt;br /&gt;  LegendItem legenditem3 = new LegendItem("Series4", "-", null, null, Plot.DEFAULT_LEGEND_ITEM_BOX, Color.yellow);&lt;br /&gt;  legenditemcollection.add(legenditem);&lt;br /&gt;  legenditemcollection.add(legenditem1);&lt;br /&gt;  legenditemcollection.add(legenditem2);&lt;br /&gt;  legenditemcollection.add(legenditem3);&lt;br /&gt;  return legenditemcollection;&lt;br /&gt; }&lt;/pre&gt;&lt;/li&gt;&lt;li&gt;The code&lt;pre&gt;package chart;&lt;br /&gt;&lt;br /&gt;import java.awt.Color;&lt;br /&gt;&lt;br /&gt;import org.jfree.chart.ChartFactory;&lt;br /&gt;import org.jfree.chart.ChartFrame;&lt;br /&gt;import org.jfree.chart.JFreeChart;&lt;br /&gt;import org.jfree.chart.LegendItem;&lt;br /&gt;import org.jfree.chart.LegendItemCollection;&lt;br /&gt;import org.jfree.chart.axis.SubCategoryAxis;&lt;br /&gt;import org.jfree.chart.plot.CategoryPlot;&lt;br /&gt;import org.jfree.chart.plot.Plot;&lt;br /&gt;import org.jfree.chart.plot.PlotOrientation;&lt;br /&gt;import org.jfree.chart.renderer.category.GroupedStackedBarRenderer;&lt;br /&gt;import org.jfree.data.KeyToGroupMap;&lt;br /&gt;import org.jfree.data.category.CategoryDataset;&lt;br /&gt;import org.jfree.data.category.DefaultCategoryDataset;&lt;br /&gt;&lt;br /&gt;public class StackedBarChartTest {&lt;br /&gt;&lt;br /&gt;private static CategoryDataset getData() {&lt;br /&gt;DefaultCategoryDataset dataSet = new DefaultCategoryDataset();&lt;br /&gt;&lt;br /&gt;dataSet.addValue(100, "Series1 G1", "Jan 04");&lt;br /&gt;dataSet.addValue(150, "Series1 G2", "Jan 04");&lt;br /&gt;&lt;br /&gt;dataSet.addValue(60, "Series2 G1", "Feb 04");&lt;br /&gt;dataSet.addValue(90, "Series2 G2", "Feb 04");&lt;br /&gt;&lt;br /&gt;dataSet.addValue(70, "Series3 G2", "Feb 04");&lt;br /&gt;dataSet.addValue(99, "Series3 G1", "Feb 04");&lt;br /&gt;&lt;br /&gt;dataSet.addValue(100, "Series4 G1", "Feb 04");&lt;br /&gt;dataSet.addValue(80, "Series4 G2", "Feb 04");&lt;br /&gt;&lt;br /&gt;dataSet.addValue(20, "Series1 G1", "Feb 04");&lt;br /&gt;dataSet.addValue(50, "Series1 G2", "Feb 04");&lt;br /&gt;&lt;br /&gt;dataSet.addValue(10, "Series2 G1", "Jan 04");&lt;br /&gt;dataSet.addValue(40, "Series2 G2", "Jan 04");&lt;br /&gt;&lt;br /&gt;dataSet.addValue(10, "Series3 G2", "Jan 04");&lt;br /&gt;dataSet.addValue(50, "Series3 G1", "Jan 04");&lt;br /&gt;&lt;br /&gt;dataSet.addValue(10, "Series4 G1", "Jan 04");&lt;br /&gt;dataSet.addValue(60, "Series4 G2", "Jan 04");&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 153, 153);"&gt;  // dataSet.addValue(100, "Series1 G1", "Jan 04");&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 153, 153);"&gt;  // dataSet.addValue(20, "Series1 G1", "Feb 04");&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 153, 153);"&gt;  // dataSet.addValue(10, "Series2 G1", "Jan 04");&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 153, 153);"&gt;  // dataSet.addValue(60, "Series2 G1", "Feb 04");&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 153, 153);"&gt;  // dataSet.addValue(99, "Series3 G1", "Feb 04");&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 153, 153);"&gt;  // dataSet.addValue(50, "Series3 G1", "Jan 04");&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 153, 153);"&gt;  // dataSet.addValue(100, "Series4 G1", "Feb 04");&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 153, 153);"&gt;  // dataSet.addValue(10, "Series4 G1", "Jan 04");&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 153, 153);"&gt;  //&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 153, 153);"&gt;  //  &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 153, 153);"&gt;  // dataSet.addValue(150, "Series1 G2", "Jan 04");&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 153, 153);"&gt;  // dataSet.addValue(50, "Series1 G2", "Feb 04");&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 153, 153);"&gt;  // dataSet.addValue(40, "Series2 G2", "Jan 04");&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 153, 153);"&gt;  // dataSet.addValue(90, "Series2 G2", "Feb 04");&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 153, 153);"&gt;  // dataSet.addValue(10, "Series3 G2", "Jan 04");&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 153, 153);"&gt;  // dataSet.addValue(70, "Series3 G2", "Feb 04");&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 153, 153);"&gt;  // dataSet.addValue(60, "Series4 G2", "Jan 04");&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 153, 153);"&gt;  // dataSet.addValue(80, "Series4 G2", "Feb 04");&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;return dataSet;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;private static JFreeChart createChart(CategoryDataset categorydataset) {&lt;br /&gt;JFreeChart jfreechart = ChartFactory.createStackedBarChart("Sample", null, "value", categorydataset, PlotOrientation.VERTICAL, true, true, false);&lt;br /&gt;GroupedStackedBarRenderer renderer = new GroupedStackedBarRenderer();&lt;br /&gt;renderer.setItemMargin(0.01);&lt;br /&gt;renderer.setDrawBarOutline(false);&lt;br /&gt;&lt;br /&gt;renderer.setSeriesPaint(0, Color.blue);&lt;br /&gt;renderer.setSeriesPaint(1, Color.blue);&lt;br /&gt;&lt;br /&gt;renderer.setSeriesPaint(2, Color.cyan);&lt;br /&gt;renderer.setSeriesPaint(3, Color.cyan);&lt;br /&gt;&lt;br /&gt;renderer.setSeriesPaint(4, Color.green);&lt;br /&gt;renderer.setSeriesPaint(5, Color.green);&lt;br /&gt;&lt;br /&gt;renderer.setSeriesPaint(6, Color.yellow);&lt;br /&gt;renderer.setSeriesPaint(7, Color.yellow);&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 153, 153);"&gt;  // renderer.setSeriesPaint(0, Color.blue);&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 153, 153);"&gt;  // renderer.setSeriesPaint(4, Color.blue);&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 153, 153);"&gt;  //&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 153, 153);"&gt;  // renderer.setSeriesPaint(1, Color.cyan);&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 153, 153);"&gt;  // renderer.setSeriesPaint(5, Color.cyan);&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 153, 153);"&gt;  //&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 153, 153);"&gt;  // renderer.setSeriesPaint(2, Color.green);&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 153, 153);"&gt;  // renderer.setSeriesPaint(6, Color.green);&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 153, 153);"&gt;  //&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 153, 153);"&gt;  // renderer.setSeriesPaint(3, Color.yellow);&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 153, 153);"&gt;  // renderer.setSeriesPaint(7, Color.yellow);&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;KeyToGroupMap keytogroupmap = new KeyToGroupMap("G1");&lt;br /&gt;keytogroupmap.mapKeyToGroup("Series1 G1", "G1");&lt;br /&gt;keytogroupmap.mapKeyToGroup("Series2 G1", "G1");&lt;br /&gt;keytogroupmap.mapKeyToGroup("Series3 G1", "G1");&lt;br /&gt;keytogroupmap.mapKeyToGroup("Series4 G1", "G1");&lt;br /&gt;&lt;br /&gt;keytogroupmap.mapKeyToGroup("Series1 G2", "G2");&lt;br /&gt;keytogroupmap.mapKeyToGroup("Series2 G2", "G2");&lt;br /&gt;keytogroupmap.mapKeyToGroup("Series3 G2", "G2");&lt;br /&gt;keytogroupmap.mapKeyToGroup("Series4 G2", "G2");&lt;br /&gt;&lt;br /&gt;renderer.setSeriesToGroupMap(keytogroupmap);&lt;br /&gt;SubCategoryAxis subcategoryaxis = new SubCategoryAxis("Series/Month");&lt;br /&gt;subcategoryaxis.setCategoryMargin(0.03);&lt;br /&gt;subcategoryaxis.addSubCategory("G1");&lt;br /&gt;subcategoryaxis.addSubCategory("G2");&lt;br /&gt;&lt;br /&gt;CategoryPlot categoryplot = (CategoryPlot) jfreechart.getPlot();&lt;br /&gt;categoryplot.setDomainAxis(subcategoryaxis);&lt;br /&gt;categoryplot.setRenderer(renderer);&lt;br /&gt;categoryplot.setFixedLegendItems(createLegendItems());&lt;br /&gt;&lt;br /&gt;return jfreechart;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;private static LegendItemCollection createLegendItems() {&lt;br /&gt;LegendItemCollection legenditemcollection = new LegendItemCollection();&lt;br /&gt;LegendItem legenditem = new LegendItem("Series1", "-", null, null, Plot.DEFAULT_LEGEND_ITEM_BOX, Color.blue);&lt;br /&gt;LegendItem legenditem1 = new LegendItem("Series2", "-", null, null, Plot.DEFAULT_LEGEND_ITEM_BOX, Color.cyan);&lt;br /&gt;LegendItem legenditem2 = new LegendItem("Series3", "-", null, null, Plot.DEFAULT_LEGEND_ITEM_BOX, Color.green);&lt;br /&gt;LegendItem legenditem3 = new LegendItem("Series4", "-", null, null, Plot.DEFAULT_LEGEND_ITEM_BOX, Color.yellow);&lt;br /&gt;legenditemcollection.add(legenditem);&lt;br /&gt;legenditemcollection.add(legenditem1);&lt;br /&gt;legenditemcollection.add(legenditem2);&lt;br /&gt;legenditemcollection.add(legenditem3);&lt;br /&gt;return legenditemcollection;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public static void main(String args[]) {&lt;br /&gt;&lt;br /&gt;StackedBarChartTest demo = new StackedBarChartTest();&lt;br /&gt;&lt;br /&gt;ChartFrame frame = new ChartFrame("First", demo.createChart(demo.getData()));&lt;br /&gt;frame.pack();&lt;br /&gt;frame.setVisible(true);&lt;br /&gt;}&lt;br /&gt;}&lt;/pre&gt;Note:&lt;ul&gt;&lt;li&gt;The SubCategoryAxis is used to assign labels to Category Axis' subcategories (i.e. groups). &lt;/li&gt;&lt;li&gt;The ChartFrame is used to display the results. The results could also be written to an output stream using the ChartUtilities.writeChartAsJPEG or ChartUtilities.writeChartAsPNG methods.&lt;/li&gt;&lt;li&gt;The commented lines show how the series color is affected by the order in which the data is entered into the Dataset.&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;/ol&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/18313336-1931557869613156409?l=java-x.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://java-x.blogspot.com/feeds/1931557869613156409/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://java-x.blogspot.com/2007/03/charts-with-jfreechart.html#comment-form' title='17 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/18313336/posts/default/1931557869613156409'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/18313336/posts/default/1931557869613156409'/><link rel='alternate' type='text/html' href='http://java-x.blogspot.com/2007/03/charts-with-jfreechart.html' title='Charts with JFreeChart 1'/><author><name>Abhilash Vuyyuru</name><uri>https://profiles.google.com/107328798079149921932</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh6.googleusercontent.com/-NxAgT10-nrk/AAAAAAAAAAI/AAAAAAAAB0o/Gz3VMjlEXb0/s512-c/photo.jpg'/></author><thr:total>17</thr:total></entry><entry><id>tag:blogger.com,1999:blog-18313336.post-1813231147351070214</id><published>2007-02-28T12:34:00.000-05:00</published><updated>2007-02-28T12:50:48.319-05:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Java EE 5'/><category scheme='http://www.blogger.com/atom/ns#' term='tips'/><title type='text'>Java EE best practices</title><content type='html'>A recent article in IBM developerworks discusses best practices in Java EE development. This is an update of the 2004 article on J2EE best practices. Here's the complete list:&lt;span class="fullpost"&gt;&lt;ol&gt; &lt;li&gt;Always use MVC.&lt;/li&gt; &lt;li&gt;Don't reinvent the wheel.&lt;/li&gt; &lt;li&gt;Apply automated unit tests and test harnesses at every layer.&lt;/li&gt; &lt;li&gt;Develop to the specifications, not the application server.&lt;/li&gt; &lt;li&gt;Plan for using Java EE security from Day One.&lt;/li&gt; &lt;li&gt;Build what you know.&lt;/li&gt; &lt;li&gt;Always use session facades whenever you use EJB components.&lt;/li&gt; &lt;li&gt;Use stateless session beans instead of stateful session beans.&lt;/li&gt; &lt;li&gt;Use container-managed transactions.&lt;/li&gt; &lt;li&gt;Prefer JSPs as your first choice of presentation technology.&lt;/li&gt; &lt;li&gt;When using HttpSessions, store only as much state as you need for the current business transaction and no more.&lt;/li&gt; &lt;li&gt;Take advantage of application server features that do not require your code to be modified.&lt;/li&gt; &lt;li&gt;Play nice within existing environments.&lt;/li&gt; &lt;li&gt;Embrace the qualities of service provided by the application server environment.&lt;/li&gt; &lt;li&gt;Embrace Java EE, don't fake it.&lt;/li&gt; &lt;li&gt;Plan for version updates.&lt;/li&gt; &lt;li&gt;At all points of interest in your code, log your program state using a standard logging framework.&lt;/li&gt; &lt;li&gt;Always clean up after yourself.&lt;/li&gt; &lt;li&gt;Follow rigorous procedures for development and testing.&lt;/li&gt;&lt;/ol&gt;Links&lt;ol&gt;&lt;li&gt;&lt;a href="http://www.ibm.com/developerworks/websphere/techjournal/0701_botzum/0701_botzum.html?ca=dgr-jw17Java-EE-Best-Practices#sec10"&gt;Top Java EE Best Practices&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.ibm.com/developerworks/websphere/techjournal/0405_brown/0405_brown.html"&gt;The top 10 (more or less) J2EE best practices&lt;/a&gt;&lt;/li&gt;&lt;/ol&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/18313336-1813231147351070214?l=java-x.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://java-x.blogspot.com/feeds/1813231147351070214/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://java-x.blogspot.com/2007/02/java-ee-best-practices.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/18313336/posts/default/1813231147351070214'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/18313336/posts/default/1813231147351070214'/><link rel='alternate' type='text/html' href='http://java-x.blogspot.com/2007/02/java-ee-best-practices.html' title='Java EE best practices'/><author><name>Abhilash Vuyyuru</name><uri>https://profiles.google.com/107328798079149921932</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh6.googleusercontent.com/-NxAgT10-nrk/AAAAAAAAAAI/AAAAAAAAB0o/Gz3VMjlEXb0/s512-c/photo.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-18313336.post-1777217708477144960</id><published>2007-02-26T16:37:00.000-05:00</published><updated>2007-02-26T16:38:58.246-05:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='news'/><title type='text'>Custom Search Update</title><content type='html'>Back from the vacation. Updated the Java custom search - Now includes over 100 Java related sites.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/18313336-1777217708477144960?l=java-x.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://java-x.blogspot.com/feeds/1777217708477144960/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://java-x.blogspot.com/2007/02/custom-search-update.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/18313336/posts/default/1777217708477144960'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/18313336/posts/default/1777217708477144960'/><link rel='alternate' type='text/html' href='http://java-x.blogspot.com/2007/02/custom-search-update.html' title='Custom Search Update'/><author><name>Abhilash Vuyyuru</name><uri>https://profiles.google.com/107328798079149921932</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh6.googleusercontent.com/-NxAgT10-nrk/AAAAAAAAAAI/AAAAAAAAB0o/Gz3VMjlEXb0/s512-c/photo.jpg'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-18313336.post-398323772261026034</id><published>2007-02-02T12:06:00.000-05:00</published><updated>2007-02-02T12:08:28.770-05:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='news'/><title type='text'>Google Related links</title><content type='html'>Added a related links control on the top. Let's see if we get any use out of this.&lt;br /&gt;&lt;br /&gt;This will be the last post for a couple of weeks, as I will be on vacat&lt;a href="javascript:void(0)" onclick="return false;" tabindex="7"&gt;&lt;span&gt;&lt;/span&gt;&lt;/a&gt;ion in India.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/18313336-398323772261026034?l=java-x.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://java-x.blogspot.com/feeds/398323772261026034/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://java-x.blogspot.com/2007/02/google-related-links.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/18313336/posts/default/398323772261026034'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/18313336/posts/default/398323772261026034'/><link rel='alternate' type='text/html' href='http://java-x.blogspot.com/2007/02/google-related-links.html' title='Google Related links'/><author><name>Abhilash Vuyyuru</name><uri>https://profiles.google.com/107328798079149921932</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh6.googleusercontent.com/-NxAgT10-nrk/AAAAAAAAAAI/AAAAAAAAB0o/Gz3VMjlEXb0/s512-c/photo.jpg'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-18313336.post-8236621638930861734</id><published>2007-01-29T14:31:00.000-05:00</published><updated>2007-01-29T14:38:08.426-05:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='persistence'/><category scheme='http://www.blogger.com/atom/ns#' term='spring'/><category scheme='http://www.blogger.com/atom/ns#' term='struts'/><category scheme='http://www.blogger.com/atom/ns#' term='example/sample code'/><title type='text'>Integrating Struts 2.0 with Spring</title><content type='html'>In the past, I posted an example on how to use Displaytag with &lt;span style="font-weight: bold;"&gt;Struts &lt;/span&gt;and Spring, using &lt;span style="font-weight: bold;"&gt;Spring JDBC&lt;/span&gt; for data access(&lt;a href="http://java-x.blogspot.com/2006/12/data-access-with-spring-and-struts-part.html"&gt;1&lt;/a&gt;, &lt;a href="http://java-x.blogspot.com/2006/12/data-access-with-spring-and-struts-part_05.html"&gt;2&lt;/a&gt;). In this post, I will describe how to do the same using &lt;span style="font-weight: bold;"&gt;Struts 2.0&lt;/span&gt;. The only major step that needs to be done here is to override the default Struts 2.0 &lt;span style="font-weight: bold;"&gt;OjbectFactory&lt;/span&gt;. Changing the ObjectFactory to Spring give control to Spring framework to instantiate action instances etc. Most of the code is from the previous post, but I will list only the additional changes here.&lt;span class="fullpost"&gt;&lt;ol&gt;&lt;li&gt;&lt;span style="font-weight: bold;"&gt;Changing the default Object factory&lt;/span&gt;: In order to change the Ojbect factory to Spring, you have to add a declaration in the struts.properties file.&lt;pre&gt;&lt;span style="font-weight: bold;"&gt;struts.objectFactory = spring&lt;/span&gt;&lt;br /&gt;struts.devMode = true&lt;br /&gt;struts.enable.DynamicMethodInvocation = false&lt;/pre&gt;&lt;center&gt;src/struts.properties&lt;/center&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-weight: bold;"&gt;The Action class&lt;/span&gt;: Here is the code for the action class&lt;pre&gt;package actions;&lt;br /&gt;&lt;br /&gt;import java.util.List;&lt;br /&gt;&lt;br /&gt;import business.BusinessInterface;&lt;br /&gt;&lt;br /&gt;import com.opensymphony.xwork2.ActionSupport;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;public class SearchAction extends ActionSupport {&lt;/span&gt;&lt;br /&gt;private BusinessInterface businessInterface;&lt;br /&gt;&lt;br /&gt;private String minSalary;&lt;br /&gt;&lt;br /&gt;private String submit;&lt;br /&gt;&lt;br /&gt;private List data;&lt;br /&gt;&lt;br /&gt;public String getSubmit() {&lt;br /&gt; return submit;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public void setSubmit(String submit) {&lt;br /&gt; this.submit = submit;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public BusinessInterface getBusinessInterface() {&lt;br /&gt; return businessInterface;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;  public String execute() throws Exception {&lt;/span&gt;&lt;br /&gt; try {&lt;br /&gt;   long minSal = Long.parseLong(getMinSalary());&lt;br /&gt;   System.out.println("Business Interface: " + businessInterface + "Minimum salary : " + minSal);&lt;br /&gt;   data = businessInterface.getData(minSal);&lt;br /&gt;   System.out.println("Data : " + data);&lt;br /&gt;&lt;br /&gt; } catch (Exception e) {&lt;br /&gt;   e.printStackTrace();&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; return SUCCESS;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public void setBusinessInterface(BusinessInterface bi) {&lt;br /&gt; businessInterface = bi;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public String getMinSalary() {&lt;br /&gt; return minSalary;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public void setMinSalary(String minSalary) {&lt;br /&gt; this.minSalary = minSalary;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public List getData() {&lt;br /&gt; return data;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public void setData(List data) {&lt;br /&gt; this.data = data;&lt;br /&gt;}&lt;br /&gt;}&lt;/pre&gt;&lt;center&gt;SearchAction.java&lt;/center&gt;&lt;ul&gt;&lt;li&gt;The Action class here does not have access to the HttpServetRequest and HttpServletResponse. Hence the action class itself was changed to the session scope for this example (see below)&lt;/li&gt;&lt;li&gt;In order for the action class to be aware of the Http Session, the action class has to implement the &lt;span style="font-weight: bold;"&gt;ServletRequestAware&lt;/span&gt; interface, and define a setServletRequest method, which will be used to inject the ServletRequest into the action class.&lt;/li&gt;&lt;li&gt;The BusinessInterface property is injected by Spring framework.&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-weight: bold;"&gt;The struts Configuration&lt;/span&gt;:&lt;pre&gt;&amp;lt;!DOCTYPE struts PUBLIC&lt;br /&gt;     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"&lt;br /&gt;     "http://struts.apache.org/dtds/struts-2.0.dtd"&amp;gt;&lt;br /&gt;&amp;lt;struts&amp;gt;&lt;br /&gt;&amp;lt;package name="Struts2Spring" namespace="/actions" extends="struts-default"&amp;gt;&lt;br /&gt; &amp;lt;action name="search" class="actions.SearchAction"&amp;gt;&lt;br /&gt;   &amp;lt;result&amp;gt;/search.jsp&amp;lt;/result&amp;gt;&lt;br /&gt; &amp;lt;/action&amp;gt;&lt;br /&gt;&amp;lt;/package&amp;gt;&lt;br /&gt;&amp;lt;/struts&amp;gt;&lt;/pre&gt;&lt;center&gt;src/struts.xml&lt;/center&gt;&lt;ul&gt;&lt;li&gt;The action's class attribute has to map the id attribute of the bean defined in the spring bean factory definition.&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-weight: bold;"&gt;The Spring bean factory definition&lt;/span&gt;&lt;pre&gt;&amp;lt;?xml version="1.0" encoding="UTF-8"?&amp;gt;&lt;br /&gt;&amp;lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&lt;br /&gt;xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"&lt;br /&gt;default-autowire="autodetect"&amp;gt;&lt;br /&gt;&amp;lt;bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"&amp;gt;&lt;br /&gt; &amp;lt;property name="driverClassName"&amp;gt;&lt;br /&gt;   &amp;lt;value&amp;gt;oracle.jdbc.driver.OracleDriver&amp;lt;/value&amp;gt;&lt;br /&gt; &amp;lt;/property&amp;gt;&lt;br /&gt; &amp;lt;property name="url"&amp;gt;&lt;br /&gt;   &amp;lt;value&amp;gt;jdbc:oracle:thin:@localhost:1521:orcl&amp;lt;/value&amp;gt;&lt;br /&gt; &amp;lt;/property&amp;gt;&lt;br /&gt; &amp;lt;property name="username"&amp;gt;&lt;br /&gt;   &amp;lt;value&amp;gt;scott&amp;lt;/value&amp;gt;&lt;br /&gt; &amp;lt;/property&amp;gt;&lt;br /&gt; &amp;lt;property name="password"&amp;gt;&lt;br /&gt;   &amp;lt;value&amp;gt;tiger&amp;lt;/value&amp;gt;&lt;br /&gt; &amp;lt;/property&amp;gt;&lt;br /&gt;&amp;lt;/bean&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;!-- Configure DAO --&amp;gt;&lt;br /&gt;&amp;lt;bean id="empDao" class="data.DAO"&amp;gt;&lt;br /&gt; &amp;lt;property name="dataSource"&amp;gt;&lt;br /&gt;   &amp;lt;ref bean="dataSource"&amp;gt;&amp;lt;/ref&amp;gt;&lt;br /&gt; &amp;lt;/property&amp;gt;&lt;br /&gt;&amp;lt;/bean&amp;gt;&lt;br /&gt;&amp;lt;!-- Configure Business Service --&amp;gt;&lt;br /&gt;&amp;lt;bean id="businessInterface" class="business.BusinessInterface"&amp;gt;&lt;br /&gt; &amp;lt;property name="dao"&amp;gt;&lt;br /&gt;   &amp;lt;ref bean="empDao"&amp;gt;&amp;lt;/ref&amp;gt;&lt;br /&gt; &amp;lt;/property&amp;gt;&lt;br /&gt;&amp;lt;/bean&amp;gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;&amp;lt;bean id="actions.SearchAction" name="search" class="actions.SearchAction" scope="session"&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;    &amp;lt;property name="businessInterface" ref="businessInterface" /&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;  &amp;lt;/bean&amp;gt;&lt;/span&gt;&lt;br /&gt;&amp;lt;/beans&amp;gt;&lt;/pre&gt;&lt;center&gt;WEB-INF/applicationContext.xml&lt;/center&gt;&lt;ul&gt;&lt;li&gt;The bean definition for the action class contains the id attribute which matches the class attribute of the action in struts.xml&lt;/li&gt;&lt;li&gt;Spring 2's bean scope feature can be used to scope an Action instance to the session, application, or a custom scope, providing advanced customization above the default per-request scoping.&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;span style="font-weight: bold;"&gt;The web deployment descriptor&lt;/span&gt;&lt;pre&gt;&amp;lt;?xml version="1.0" encoding="UTF-8"?&amp;gt;&lt;br /&gt;&amp;lt;web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&lt;br /&gt;xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;display-name&amp;gt;Struts2Spring&amp;lt;/display-name&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;filter&amp;gt;&lt;br /&gt; &amp;lt;filter-name&amp;gt;struts2&amp;lt;/filter-name&amp;gt;&lt;br /&gt; &amp;lt;filter-class&amp;gt;org.apache.struts2.dispatcher.FilterDispatcher&amp;lt;/filter-class&amp;gt;&lt;br /&gt;&amp;lt;/filter&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;filter-mapping&amp;gt;&lt;br /&gt; &amp;lt;filter-name&amp;gt;struts2&amp;lt;/filter-name&amp;gt;&lt;br /&gt; &amp;lt;url-pattern&amp;gt;/*&amp;lt;/url-pattern&amp;gt;&lt;br /&gt;&amp;lt;/filter-mapping&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;listener&amp;gt;&lt;br /&gt; &amp;lt;listener-class&amp;gt;org.springframework.web.context.ContextLoaderListener&amp;lt;/listener-class&amp;gt;&lt;br /&gt;&amp;lt;/listener&amp;gt;&lt;br /&gt;&amp;lt;listener&amp;gt;&lt;br /&gt; &amp;lt;listener-class&amp;gt;org.springframework.web.context.request.RequestContextListener&amp;lt;/listener-class&amp;gt;&lt;br /&gt;&amp;lt;/listener&amp;gt;&lt;br /&gt;&amp;lt;welcome-file-list&amp;gt;&lt;br /&gt; &amp;lt;welcome-file&amp;gt;index.html&amp;lt;/welcome-file&amp;gt;&lt;br /&gt;&amp;lt;/welcome-file-list&amp;gt;&lt;br /&gt;&amp;lt;/web-app&amp;gt;&lt;/pre&gt;&lt;center&gt;web.xml&lt;/center&gt;The only significant addition here is that of the &lt;span style="font-weight: bold;"&gt;RequestContextListener&lt;/span&gt;. &lt;span style="font-style: italic;"&gt;This listener allows Spring framework, access to the HTTP session information&lt;/span&gt;.&lt;/li&gt;&lt;li&gt;&lt;span style="font-weight: bold;"&gt;The JSP file&lt;/span&gt;: The JSP file is shown below. The only change here is that the action class, instead of the Data list is accessed from the session.&lt;pre&gt;&amp;lt;%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%&amp;gt;&lt;br /&gt;&amp;lt;%@ taglib uri="http://displaytag.sf.net" prefix="display"%&amp;gt;&lt;br /&gt;&amp;lt;%@ taglib prefix="s" uri="/struts-tags"%&amp;gt;&lt;br /&gt;&amp;lt;%@ page import="actions.SearchAction,beans.Employee,business.Sorter,java.util.List,org.displaytag.tags.TableTagParameters,org.displaytag.util.ParamEncoder"%&amp;gt;&lt;br /&gt;&amp;lt;html&amp;gt;&lt;br /&gt;&amp;lt;head&amp;gt;&lt;br /&gt;&amp;lt;title&amp;gt;Search page&amp;lt;/title&amp;gt;&lt;br /&gt;&amp;lt;link rel="stylesheet" type="text/css" href="/StrutsPaging/css/screen.css" /&amp;gt;&lt;br /&gt;&amp;lt;/head&amp;gt;&lt;br /&gt;&amp;lt;body bgcolor="white"&amp;gt;&lt;br /&gt;&amp;lt;s:form action="/actions/search.action"&amp;gt;&lt;br /&gt;&amp;lt;table&amp;gt;&lt;br /&gt;  &amp;lt;tr&amp;gt;&lt;br /&gt;    &amp;lt;td&amp;gt;Minimum Salary:&amp;lt;/td&amp;gt;&lt;br /&gt;    &amp;lt;td&amp;gt;&amp;lt;s:textfield label="minSalary" name="minSalary" /&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;  &amp;lt;/tr&amp;gt;&lt;br /&gt;  &amp;lt;tr&amp;gt;&lt;br /&gt;    &amp;lt;td colspan="2"&amp;gt;&amp;lt;s:submit name="submit" /&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;  &amp;lt;/tr&amp;gt;&lt;br /&gt;&amp;lt;/table&amp;gt;&lt;br /&gt;&amp;lt;/s:form&amp;gt;&lt;br /&gt;&amp;lt;jsp:scriptlet&amp;gt;&lt;br /&gt;&lt;br /&gt; &lt;span style="font-weight: bold;"&gt;SearchAction action = (SearchAction)session.getAttribute("actions.SearchAction");&lt;/span&gt;&lt;br /&gt; session.setAttribute("empList", action.getData());&lt;br /&gt;  if (session.getAttribute("empList") != null) {&lt;br /&gt;   String sortBy = request.getParameter((new ParamEncoder("empTable")).encodeParameterName(TableTagParameters.PARAMETER_SORT));&lt;br /&gt;   Sorter.sort((List) session.getAttribute("empList"), sortBy);&lt;br /&gt;  &lt;br /&gt; &amp;lt;/jsp:scriptlet&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;display:table name="sessionScope.empList" pagesize="4" id="empTable" sort="external" defaultsort="1" defaultorder="ascending" requestURI=""&amp;gt;&lt;br /&gt;&amp;lt;display:column property="empId" title="ID" sortable="true" sortName="empId" headerClass="sortable" /&amp;gt;&lt;br /&gt;&amp;lt;display:column property="empName" title="Name" sortName="empName" sortable="true" headerClass="sortable" /&amp;gt;&lt;br /&gt;&amp;lt;display:column property="empJob" title="Job" sortable="true" sortName="empJob" headerClass="sortable" /&amp;gt;&lt;br /&gt;&amp;lt;display:column property="empSal" title="Salary" sortable="true" headerClass="sortable" sortName="empSal" /&amp;gt;&lt;br /&gt;&amp;lt;/display:table&amp;gt;&lt;br /&gt;&amp;lt;jsp:scriptlet&amp;gt;&lt;br /&gt;  }&lt;br /&gt; &amp;lt;/jsp:scriptlet&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;/body&amp;gt;&lt;br /&gt;&amp;lt;/html:html&amp;gt;&lt;/pre&gt;&lt;center&gt;search.jsp&lt;/center&gt; &lt;/li&gt;&lt;li&gt;&lt;span style="font-weight: bold;"&gt;The Other required classes&lt;/span&gt;: The following other classes have been used for the example, and they can be obtained from the previous posts (&lt;a href="http://java-x.blogspot.com/2006/12/data-access-with-spring-and-struts-part.html"&gt;1&lt;/a&gt;, &lt;a href="http://java-x.blogspot.com/2006/12/data-access-with-spring-and-struts-part_05.html"&gt;2&lt;/a&gt;).&lt;ul&gt;&lt;li&gt;Employee.java&lt;/li&gt;&lt;li&gt;BusinessInterface.java&lt;/li&gt;&lt;li&gt;Sorter.java&lt;/li&gt;&lt;li&gt;DAO.java&lt;/li&gt;&lt;li&gt;EmpMapper.java&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;/ol&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/18313336-8236621638930861734?l=java-x.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://java-x.blogspot.com/feeds/8236621638930861734/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://java-x.blogspot.com/2007/01/integrating-struts-20-with-spring.html#comment-form' title='10 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/18313336/posts/default/8236621638930861734'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/18313336/posts/default/8236621638930861734'/><link rel='alternate' type='text/html' href='http://java-x.blogspot.com/2007/01/integrating-struts-20-with-spring.html' title='Integrating Struts 2.0 with Spring'/><author><name>Abhilash Vuyyuru</name><uri>https://profiles.google.com/107328798079149921932</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh6.googleusercontent.com/-NxAgT10-nrk/AAAAAAAAAAI/AAAAAAAAB0o/Gz3VMjlEXb0/s512-c/photo.jpg'/></author><thr:total>10</thr:total></entry><entry><id>tag:blogger.com,1999:blog-18313336.post-1843076558077684635</id><published>2007-01-23T15:41:00.000-05:00</published><updated>2007-01-23T15:53:22.250-05:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='spring'/><category scheme='http://www.blogger.com/atom/ns#' term='example/sample code'/><title type='text'>Spring: Prototype beans with Singletons - 2</title><content type='html'>The &lt;a style="font-weight: bold;" href="http://java-x.blogspot.com/2007/01/spring-prototype-beans-with-singletons.html"&gt;previous post&lt;/a&gt; described how to implement Lookup method injection and ServiceLocatorFactoryBean and BeanFactoryAware to enable singleton classes to use Prototype beans. This post will describe how to use &lt;span style="font-weight: bold;"&gt;Scoped beans&lt;/span&gt; (session or request or globalSession) from Spring managed beans in a Web application and also give the client servlet for testing the previous and the current examples.&lt;span class="fullpost"&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;font-size:130%;" &gt;&lt;span style="font-family:arial;"&gt;Using in Web Applications&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;This is not for Singleton or Prototype beans. This solution is for using beans that are saved in the request of session scope. To implement this solution,  you must inject an AOP proxy in place of the scoped bean, the proxy will then delegate the method call to the object that is in currently in the specified scope. All you have to do is to put the following in the bean definition&lt;pre&gt;&amp;lt;aop:scoped-proxy/&amp;gt;&lt;/pre&gt;In the Web deployment descriptor (Shown in a following section), you will have to put a &lt;span style="font-family:courier new;"&gt;RequestContextListener&lt;/span&gt; in addition to the ContextLoaderListener. &lt;span style="font-weight: bold;"&gt;The RequestContextListener binds the HTTP request object to the Thread that is servicing that request&lt;/span&gt;. This makes beans that are request and session-scoped available further down the call chain (i.e. for spring managed beans invoked in the same thread of execution). For the Web Application scenario, I used a different &lt;span style="font-weight: bold;"&gt;Command bean&lt;/span&gt; shown below&lt;pre&gt;package beans;&lt;br /&gt;&lt;br /&gt;public class NewCommand {&lt;br /&gt;private String someProperty;&lt;br /&gt;&lt;br /&gt;public String getSomeProperty() {&lt;br /&gt;return someProperty;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public void setSomeProperty(String someProperty) {&lt;br /&gt;this.someProperty = someProperty;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public String execute() {&lt;br /&gt;return "Command property : " + someProperty;&lt;br /&gt;}&lt;br /&gt;}&lt;/pre&gt;&lt;center&gt;NewCommand.java&lt;/center&gt;&lt;br /&gt;The &lt;span style="font-weight: bold;"&gt;Command Manager&lt;/span&gt; is shown below&lt;pre&gt;package beans;&lt;br /&gt;&lt;br /&gt;public class CmdMgrWeb {&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;private NewCommand command;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;public Object process(String prop) {&lt;br /&gt;command.setSomeProperty(prop);&lt;br /&gt;return command.execute();&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public NewCommand getCommand() {&lt;br /&gt;return command;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public void setCommand(NewCommand command) {&lt;br /&gt;this.command = command;&lt;br /&gt;}&lt;br /&gt;}&lt;/pre&gt;&lt;center&gt;CmdMgrWeb.java&lt;/center&gt;&lt;br /&gt;&lt;span style="font-weight: bold;font-size:130%;" &gt;&lt;span style="font-family:arial;"&gt;The Servlet&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;The following is the servlet used for testing the beans used here.&lt;pre&gt;package servlets;&lt;br /&gt;&lt;br /&gt;import java.io.IOException;&lt;br /&gt;import java.io.PrintWriter;&lt;br /&gt;&lt;br /&gt;import javax.servlet.ServletException;&lt;br /&gt;import javax.servlet.http.HttpServletRequest;&lt;br /&gt;import javax.servlet.http.HttpServletResponse;&lt;br /&gt;&lt;br /&gt;import org.springframework.beans.factory.BeanFactory;&lt;br /&gt;import org.springframework.web.context.WebApplicationContext;&lt;br /&gt;import org.springframework.web.context.support.WebApplicationContextUtils;&lt;br /&gt;&lt;br /&gt;import beans.CmdMgrNoBFA;&lt;br /&gt;import beans.CmdMgrServiceFactory;&lt;br /&gt;import beans.CmdMgrWeb;&lt;br /&gt;import beans.CommandManager;&lt;br /&gt;import beans.NewCommand;&lt;br /&gt;&lt;br /&gt;public class MethodInjectionServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {&lt;br /&gt;public MethodInjectionServlet() {&lt;br /&gt;super();&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {&lt;br /&gt;WebApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;request.getSession().setAttribute("command", new NewCommand());&lt;/span&gt;&lt;br /&gt;BeanFactory factory = (BeanFactory) context;&lt;br /&gt;PrintWriter writer = response.getWriter();&lt;br /&gt;&lt;br /&gt;CommandManager commandManager = (CommandManager) factory.getBean("commandManager");&lt;br /&gt;writer.println(commandManager.process("test"));&lt;br /&gt;&lt;br /&gt;CmdMgrNoBFA cmdMgrNoBFA = (CmdMgrNoBFA) factory.getBean("cmdMgrNoBFA");&lt;br /&gt;writer.println(cmdMgrNoBFA.process("test"));&lt;br /&gt;&lt;br /&gt;CmdMgrServiceFactory cmdMgrServiceFactory = (CmdMgrServiceFactory) factory.getBean("cmdMgrServiceFactory");&lt;br /&gt;writer.println(cmdMgrServiceFactory.process("test"));&lt;br /&gt;&lt;br /&gt;CmdMgrWeb cmdMgrWeb = (CmdMgrWeb) factory.getBean("cmdMgrWeb");&lt;br /&gt;writer.println(cmdMgrWeb.process("test"));&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;/pre&gt;&lt;center&gt;MethodInjectionServlet.java&lt;/center&gt;&lt;br /&gt;&lt;span style="font-weight: bold;font-size:130%;" &gt;&lt;span style="font-family:arial;"&gt;The Web Deployment Descriptor&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;The web.xml file is shown below. You can see the two listeners required for using spring and the scoped beans.&lt;pre&gt;&amp;lt;?xml version="1.0" encoding="UTF-8"?&amp;gt;&lt;br /&gt;&amp;lt;web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&lt;br /&gt;xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"&amp;gt;&lt;br /&gt;&amp;lt;display-name&amp;gt;MethodInjectionWeb&amp;lt;/display-name&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;listener&amp;gt;&lt;br /&gt;&amp;lt;listener-class&amp;gt;org.springframework.web.context.ContextLoaderListener&amp;lt;/listener-class&amp;gt;&lt;br /&gt;&amp;lt;/listener&amp;gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;&amp;lt;listener&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;  &amp;lt;listener-class&amp;gt;org.springframework.web.context.request.RequestContextListener&amp;lt;/listener-class&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;&amp;lt;/listener&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;servlet&amp;gt;&lt;br /&gt;&amp;lt;description&amp;gt;&amp;lt;/description&amp;gt;&lt;br /&gt;&amp;lt;display-name&amp;gt;MethodInjectionServlet&amp;lt;/display-name&amp;gt;&lt;br /&gt;&amp;lt;servlet-name&amp;gt;MethodInjectionServlet&amp;lt;/servl
