Thursday, February 17, 2011

MultiActionController in Spring MVC

Under Spring MVC package, i could find an API “MultiActionController”, through which i can forward request to any of the action method in a controller class. Have put a simple code snippet briefing the configuration of MultiActionController.

• Write a java class extending “MultiActionController”.
Extend MultiActionController in SessionController class and group multiple action methods related to session. Eg: login and logout.

package com.jb.web;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction. MultiActionController;
 
public class SessionController extends MultiActionController {
     
    public ModelAndView login(HttpServletRequest request,
            HttpServletResponse response) throws Exception {
   String message = “Login method called”;
        System.out.println(message);
        return new ModelAndView("user", "message", message);
    }
     
    public ModelAndView logout(HttpServletRequest request,
            HttpServletResponse response) throws Exception {
     String message = “Logout method called”;
        System.out.println(message);
        return new ModelAndView("user", "message", message);
    }
}

• Configure the controller in spring bean file.
Here is a spring bean configuration file to map a request url to MultiActionController. An asterisk character can be used to define request url.
Characters toning asterisk will be considered as method name in SessionController.



 
    
         
    
 


• Invoking login and logout action methods from jsp.
Login and Logout methods can be invoked by posting request on following url from jsp.
  Login 
  Logout 

Sunday, February 6, 2011

Servlet 3.0 (part-1)

The widely accepted technology to build web application, Servlets, has seen new features and APIs in its store with the release of version Servlet 3.0 specs. This release is crammed with stirring feature for new age web development.

The specification focuses on following feature:

• Ease of development
• Plug-ability and extensibility
• Asynchronous support
• Security enhancement

In this article(Servlet 3.0 part-1), I would keep the focus on defining Servlet, Filters and Listeners using annotations and will try to put my thougts on programmatically adding Servlet, Filters and Listeners to ServletContext object in Servlet 3.0 (part-2).

According to specs 3.0, deployment descriptor is optional. Servlets and other components can be configured using annotations.

All annotations used in Servlet 3.0 can be found under package ‘javax.servlet.annotation’.
For comparison, i have put code snippets for writing servlet and its components in old Servlet 2.5 API and same components in new Servlet 3.0. In 2.5 Web container will initialize the components only if you configure the details in deployment descriptor.

• Servlet:

public class ServletA extends HttpServlet {
//A GET method
    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException{
        //code to process request
    }

Deployment Descriptor (web.xml) Entry 


        ServletA
        com.jb.ServletA

    
        ServletA
        /ServletA
    
 

Here is a much simplified code for servlet written in Servlet 3.0

@WebServlet(name="ServletA", urlPatterns={"/ServletA"})
public class ServletA extends HttpServlet {
    @ Override
    public void doGet (HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException{
        //code to process request.
    }
}

Deployment Descriptor (web.xml) Entry
Optional


• Filters

public class FilterA implements Filter {
    public void doFilter(ServletRequest request, ServletResponse response,
                         FilterChain chain)
 throws IOException, ServletException {
 //code snippets …
}

Deployment Descriptor (web.xml) Entry

    
        FilterA
        com.jb.FilterA
    
    
        FilterA
        /ServletA
    


Configuring Filter in Servlet 3.0
@WebFilter(filterName="FilterA", urlPatterns={"/ServletA"})
public class FilterA implements Filter {
 public void doFilter(ServletRequest request, ServletResponse response,
                         FilterChain chain)
 throws IOException, ServletException {
  //filter code here…
     }
}  
 

Deployment Descriptor (web.xml) Entry
Optional



• Listeners
public class ListenerA implements ServletContextListener {

    public void contextInitialized(ServletContextEvent sce) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}

Deployment Descriptor (web.xml) Entry

   
        com.jb.ListenerA    
    


Configuring Listeners in Servlet 3.0

@WebListener()
public class ListenerA implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        throw new UnsupportedOperationException("Not supported yet.");
    } 
}    

Deployment Descriptor (web.xml) Entry
Optional


• Passing Init Params in Servlet 3.0 has a simple syntax too.

@WebServlet(name="ServletA",
        urlPatterns={"/ServletA"},
        initParams={@WebInitParam(name="param1", value="paramvalue")} // passing init params to servlet ServletA
)
public class ServletA extends HttpServlet {
 //Servlet code …
}
 

Continued in Servlet 3.0 (part-2)...