Monday, August 9, 2010

Java Servlet Technology which is the widely accepted technology for creating dynamic java web applications and which is behind all the modern java web frameworks. This major release tries to address the most wanted featues from the community as a JSR and is approved in the form of JSR 315 and is planned to be part of JEE6 (JSR 316). The main focus for this release in helping in ease of developemnt using Java annotations, Pluggability and extendability of using fragments, Asynchoronos Servlet support and security enhancements. Ease of DevelopmentUsing annoations Servlets join the band wagon of using declarative-style programming along with JPA, EJB3, Spring and the rest. This helpa developing Servlets, Filters more easily with the code and the related configuration in the same place and thus making the web.xml an optional. It's the Servlet 3.0 supported contianer which will process all the classes at boot time to find out all the annotated servlets, filters and listener classes and made them available for serving. If you don't want the container to process all the class files to figure out the annotated classes, you can turn it off using the new web.xml configuration parameter metadata-complete on web-app element to true. The other important addition which helps in ease of development and configuration is programmatic setup of serlvets and filters.

Note that the class still needs to extends HttpServlet (It is a mandatory requirement) Servlet filters and Listeners follow the same pattern except that the class needs to implement Filter and ServletContextListener classes and the annoation @WebFilter, @WebListener respectively

Asynchronous Support Asynchronous Http Request Processing (aka Comet) is a way that allows you to process a single Http Request using non-blocking I/O. More details about Coment and Asynchronous programming here(http://en.wikipedia.org/wiki/Comet_%28programming%29). Basically all this means is your servlet can continue working on the request while waiting for some other resource to be released or some other operation to be done like a database update. To enable async support for your servlet, you can enable it using asyncSupported attribute of @WebServlet to true. Enabling this will give you support for few new methods in the Servletrequest itself inclluding startAsync(servletRequest, servletResponse), startAsync(), and getAsyncContext(). Once async attribute is set on the Servlet class you should either call startAsync(servletRequest, servletResponse) or startAsync() method to make an asynchronous request. A simple example:

public class AsyncWebService implements Runnable { AsyncContext ctx; public AsyncWebService(AsyncContext ctx) { this.ctx = ctx; } public void run() { // Invoke web service and save result in request attribute // Dispatch the request to render the result to a JSP. ctx.dispatch("/render.jsp"); }}

More detailed tutorial on writing Asynchronous Servlets in my next entry.

PluggabilityA new notion called web fragments has been introduced in servlet 3.0 specification which helps reduce the configuration. A webfragment is a part or all of web.xml configuration that can be specified by any framework or a jar file dropped into the web application. In this way the framework can by default configure itself to work with the application with out the application developer confioguring it. <web-fragment> <servlet> <description></description> <display-name>TestServlet</display-name> <servlet-name>TestServlet</servlet-name> <servlet-class>sample.TestServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>TestServlet</servlet-name> <url-pattern>/TestServlet</url-pattern> </servlet-mapping></web-fragment>

Thursday, September 11, 2008

Ocassionally, some of your methods in your classes need to call some other public methods in the same class to get the job done. But when writing unit tests for the calling methods, you want to keep your tests isolated from the calls to other public methods. I encountered such a scenario recently and found that Easymock can help you by mocking some of the methods while keeping the rest of the methods intact.

Let us see an example,The BlogSearchService has two public methods, first method filterBlogsByAuthor takes an author name and a list of blogs and returns all the blogs written by that particular order author.The other business method searchBlogs will take a search paramter and the author, it will get all the blogs by the search parameter using a custom logic and do some other house keeping tasks like sorting them by published date and number of comments and then filter the methods by author.

package com.teja.blogs;

importjava.util.Collections;importjava.util.List;

/**** @author Teja*/publicclass BlogSearchService {

publicList<Blog> searchBlogs(String searchString, String author){

//do some custom logic here on these blogs like sorting them by posted date.return filterBlogsByAuthor(author, Collections.EMPTY_LIST); }

When writing the test cases for the second method, I don't want to test the other public method filterByAuthor as(i)it has already been tested by other test cases,(ii)I want to keep my test cases more unit.

So in my test case where I am testing my searchBlogs, I am creating the mock service class which has mock methods only for filterBlogsByAuthor, but the actual mehtod(searchBlogs) which is about to be tested is not.As usual you can do expectations on the mock class, replay and verify as if it is some other external resource being mocked.

To create a partial mock class of the service, the extra parameter you need to pass is the name of the method to be mocked and the parameter types it accepts (coz, it need to know which version of the method you want to mock if you have overloaded methods)

Saturday, September 6, 2008

Pagination is a technique of presenting large amount of data on the UI, allowing the users to navigate between pages/data in an easy way.Being a java developer for so many years, I remember doing dirty logic to achieve pagination to using some very useful and advanced tag libraries like displaytag. What ever may be the technique, pagination is never been an easy task.But when I started playing around with rails and I got a situation where I need to do some pagination. And I readily found a gem will_paginate and doing pagination on rails is a breeze.

Lets get started first by installing the gem.

sudo gem install will_paginate

To check whether the plugin installed successfully or not, you can do the following in your rails console.

Monday, August 25, 2008

Spring web services a product of spring community, helps in developing web services using contract first web services and helps in manipulating the xml in many ways. In this tutorial I am about to show a step by step guide in developing spring web services using JAXB as mashalling technology.

Spring strongly supports only contract first web services for several reasons like Fragility, performance, re usability and versioning.You can find more information visit this link

So lets start first by developing our XSD....

<?xmlversion="1.0"encoding="UTF-8"?>

<xsd:schemaxmlns:xsd="http://www.w3.org/2001/XMLSchema"

targetNamespace="http://com.tejakantamneni/schemas"

xmlns:tns="http://com.tejakantamneni/schemas"

elementFormDefault="qualified">

</xsd:schema>

This is a plain regular xsd where we will define all our xml elements going to create our wsdl file.Lets define our blog object, the blog is just a complex type object with three properties the "Title" for the blog, the "AbstractDetail" of the blog and the date when the blog is published "PublishedDate". This can be defined in the xml complex typeas follows...

Next step is defining the search criteria object, I am wrapping the search criteria into a seperate object so that in future, we can extend the search criteria by adding more elements like author, subject. Here the xsd definition of the search criteriaobject with two fields title and published date.

The next stepa are creating the actual request object for our web service, which is basically a wrapper on the search criteria and our response object which is just a array/list of blog objects (which can be either zero size, if search results nothingor can be of size n if search returns n objects).Here is our completed xsd,

The next in developing our web services is defining our contract, the WSDL file.Here is the complete version the WSDL file.Essentilly we are improting the xsd file which we had created in the earlier step to use them in the wsdlThen create the message parts, the port and the binding operation for the blog search service. This is self explanatory.

next step is creating the JAXB objects for your xsd elements using xjc compiler. you can either craft the JAXB objects by hand or justuse the xjc compiler. I prefer the later one. see https://jaxb.dev.java.net/guide/ for more details. (My Intellij Idea supports generating JAXB objects directly from xsd).

(Posting all the code is not practical, So I am moving here ahead posting the important parts)Here is the endpoint

now the last and more important step, putting all the things together to deploy the service,configuring the spring-ws-servlet.xml

First and the most important point here is I am autowiring the endpoint by using the annotation @Endpoint.

So in the context.xml, we are defining the wsdl to be exposed as a bean using the class org.springframework.ws.wsdl.wsdl11.SimpleWsdl11Definition.I am specifying the JAXBMarshaller and injecting all the jaxb classes.I am also configuring a couple of beans for validating and logging each and every request and response.