Showing posts with label Struts Tags. Show all posts
Showing posts with label Struts Tags. Show all posts

Thursday, 5 December 2013

HttpSessionListener , HttpSessionAttributeListener , ServletContextListener in java example


1. HttpSessionListener :- This interface may are notified of changes to the list of active sessions in a web application. To receive notification events, implementation class must be configured in the deployment descriptor (web.xml) for the web application. HttpSessionListener is used to keep track the the active session. When a new user login session created & add 1 in counter when invalidate logout minus 1 in counter.



create a web application with user login & user logout feature where you can see how this will be work.

implementing HttpSessionListener interface invoked at two method:-

1. public void sessionCreated(HttpSessionEvent httpSessionEvent)
    //Receives notification that a session has been created. LOGIN
    //Parameters: httpSessionEvent- containing the session.

2. public void sessionDestroyed(HttpSessionEvent httpSessionEvent)
   //Receives notification that a session is invalidated. LOGOUT
   //Parameters:httpSessionEvent - containing the session.

Sample Code :-

package com.javastoreroom.listener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import org.apache.log4j.Logger;

public class SessionListener implements HttpSessionListener {
 private static final Logger LOGGER = Logger.getLogger(SessionListener.class);
 private static int  activeSessionCounter;
 
 public static int getActiveSessionCounter() {
  return activeSessionCounter;
 }

 public static void setActiveSessionCounter(int activeSessionCounter) {
  SessionListener.activeSessionCounter = activeSessionCounter;
 }

 public void sessionCreated(HttpSessionEvent arg0) {
  activeSessionCounter++;
  LOGGER.info("session created ------ one add into counter -------> :"+activeSessionCounter);

 }

 public void sessionDestroyed(HttpSessionEvent arg0) {
  activeSessionCounter--;
  LOGGER.info("session destroy ----- remove one into counter -----> :"+activeSessionCounter);

 }

}

As we write above "implementation class must be configured in the deployment descriptor (web.xml)"


  com.javastoreroom.listener.SessionListener
 

now when you run your web application you can see in log :-
while user login :-session created ------ one add into counter -------> :1
While user Logout:-session destroy ----- remove one into counter -----> :0

2. HttpSessionAttributeListener:- This interface receiving notification events about HttpSession attribute changes.To receive these notification events, the implementation class must be either declared in the deployment descriptor or annotated with WebListener.HttpSessionAttributeListener is used to keep monitor session attribute. When a new user login session created & put username , id some other attribute in session to access globally in application. Using this interface we can check when attribute add , update and remove from session.

implementing HttpSessionAttributeListener interface invoked at three method:-

1. public void attributeAdded(HttpSessionBindingEvent bindingEvent)
    //Receives notification that an attribute has been added to a session. LOGIN & set USERNAME IN SESSION
    //Parameters:bindingEvent- containing the session and the name and value of the attribute that was added

2. public void attributeRemoved(HttpSessionBindingEvent bindingEvent)
    //Receives notification that an attribute has been removed from a session.
    //Parameters:bindingEvent-containing the session and the name and value of the attribute that was removed

3. public void attributeReplaced(HttpSessionBindingEvent bindingEvent)
    //Receives notification that an attribute has been replaced in a session.
    //Parameters:bindingEvent- containing the session and the name and (old) value of replaced with new one.

Sample Code:-
package com.javastoreroom.listener;

import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import org.apache.log4j.Logger;

public class SessionAttributeListener implements HttpSessionAttributeListener {
 public static final Logger LOGGER = Logger.getLogger(SessionAttributeListener.class);

 public void attributeAdded(HttpSessionBindingEvent bindingEvent) {
  String attributeName = bindingEvent.getName();
  Object object = bindingEvent.getValue();
              LOGGER.info("attribute added in session -->" + attributeName + "&"+ object);
 }

 public void attributeRemoved(HttpSessionBindingEvent bindingEvent) {
  String attributeName=bindingEvent.getName();
  Object object=bindingEvent.getValue();
  LOGGER.info("attribute removed in session -->" + attributeName + "&"+ object);

 }

 public void attributeReplaced(HttpSessionBindingEvent bindingEvent) {
  String attributeName=bindingEvent.getName();
  Object object=bindingEvent.getValue();
  LOGGER.info("attribute replace in session -->" + attributeName + "&"+ object);
 }
}

"implementation class must be configured in the deployment descriptor (web.xml)"


  com.javastoreroom.listener.SessionAttributeListener
 

3.ServletContextListener:-This interface receive notifications about changes to the servlet context of the web application they are part of. To receive notification events, the implementation class must be configured in the deployment descriptor for the web application.If you want to run your code before the web application is start.Here you want to start sending notification when application start either user login or not.

implementing ServletContextListenerinterface invoked at two method:-

1. public void contextInitialized(ServletContextEvent servletContextEvent)
    //Notification that the web application initialization process is starting. All ServletContextListeners are notified of context initialization before any filter or servlet in the web application is initialized.

2. public void contextDestroyed(ServletContextEvent servletContextEvent)
    //Notification that the servlet context is about to be shut down. All servlets and filters have been destroy()ed before any //ServletContextListeners are notified of context destruction. 

Sample Code:-

package com.javastoreroom.listener;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;

import org.apache.log4j.Logger;

public class applicationListener implements ServletContextListener {
 public static final Logger LOGGER = Logger.getLogger(SessionAttributeListener.class);

 @Override
 public void contextDestroyed(ServletContextEvent arg0) {
  LOGGER.info("context destory when stop ");
  
 }

 @Override
 public void contextInitialized(ServletContextEvent arg0) {
  LOGGER.info("context started before aplication started ");
  
 }
}

When you start a tomcat server see log "context started before aplication started" line will be shown before application start.

:) Enjoy JAVA Love it

Thursday, 21 November 2013

Struts Tags s:checkboxlist , s:select , s:inputtransferselect , s:updownselect etc..

Struts2 framework provides a tag library from the view technology. In this tutorial ,
we discuss few tag that are mostly we use. Most tags are supported in all template languages (jsp , velocity , freemarker)
but some are currently only specific to one language.



The types of Struts2tags can be broken in to two types: generic and UI. According to function and responsibility.

Generic Tags :- Generic tags are used for controlling the execution flow when the pages render.
Such as Control Tags provide control flow, such as if, else, and iterator. Data Tags allow for data manipulation
or creation, such as bean, push, and i18n.



UI Tags :-Unlike Generic Tags, UI tags do not provide much control structure or logic. Rather,
they are focussed on using data, either from your action/value stack or from the Data Tags, and
displaying data in rich and reusable HTML. All UI tags are driven by templates and themes. While
generic tags simply output some content directly from the tag (if there is any content to output),
the UI tags defer to a template, often grouped together as a theme, to do the actual rendering.

Here is Example to show few UI tag Working :-

web.xml


 TestStrs


 
  struts2
  org.apache.struts2.dispatcher.FilterDispatcher
 

    
   struts2 
   /* 
    
 
 
  index.jsp
 



struts.xml
 


 
 


  
   /result.jsp
  

 
 

Bean
package com.javastoreroom;

public class StudentBean {

 private int id;
 private String name;

 public StudentBean(int id, String name) {
  this.id = id;
  this.name = name;
 }

 public int getId() {
  return id;
 }

 public void setId(int id) {
  this.id = id;
 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }
}

Action Class
package com.javastoreroom;

import java.util.ArrayList;
import java.util.List;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class RetrieveAction extends ActionSupport {

 /**
  * @author Arun
  */

 private List nameList = null;

 public String fetchName() {
  nameList = new ArrayList();
  {
   nameList.add(new StudentBean(1, "Arun"));
   nameList.add(new StudentBean(2, "Anu"));
   nameList.add(new StudentBean(3, "Aryan"));
   nameList.add(new StudentBean(4, "Anu"));
   nameList.add(new StudentBean(5, "Aranv"));
   nameList.add(new StudentBean(6, "Arun"));
  }
  return SUCCESS;
 }

 public String execute() {
  return SUCCESS;
 }

 public List getNameList() {
  return nameList;
 }

 public void setNameList(List nameList) {
  this.nameList = nameList;
 }

}

index.jsp
<%response.sendRedirect("fetchName.action");%> 

result.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@taglib prefix="s" uri="/struts-tags"%>



Struts 2 UI Tag







while executing pproject UI tag are shown like this :-
:)