Showing posts with label servlet. Show all posts

An Example of HttpSessionAttributeListener in Java


HttpSessionAttributeListener, is used to keep track of every HttpSession attribute Added, Replaced or Removed in your current web application.

Source Code
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;

public class SessionAttributeListener implements HttpSessionAttributeListener {
    /**
     * Called when any servlet/jsp in application add the Attribute in HttpSession 
     * @param event 
     */
    @Override
    public void attributeAdded(HttpSessionBindingEvent event) {
        /**
         * Get the attribute name.
         * Attribute name is always in String
         */
        String strAttributeName = event.getName();
        /**
         * Get the attribute value
         * Attribute value can be String, Object, etc...
         */
        Object objAttributeValue = event.getValue();
        /* Print the value of AttributeName and AttributeValue */
        System.out.println("Attribute added: [" + strAttributeName + " : " + objAttributeValue + "]");
    }

    /**
     * Called when any servlet/jsp in application override/replace/add_same_attribute_to the HttpSession
     * @param event 
     */
    @Override
    public void attributeRemoved(HttpSessionBindingEvent event) {
        /**
         * Get the attribute name.
         * Attribute name is always in String
         */
        String strAttributeName = event.getName();
        /**
         * Get the attribute value
         * Attribute value can be String, Object, etc...
         */
        Object objAttributeValue = event.getValue();
        /* Print the value of AttributeName and AttributeValue */
        System.out.println("Attribute removed: [" + strAttributeName + " : " + objAttributeValue + "]");
    }

    /**
     * Called when any servlet/jsp in application try to remove the attribute from HttpSession
     * @param event 
     */
    @Override
    public void attributeReplaced(HttpSessionBindingEvent event) {
        /**
         * Get the attribute name.
         * Attribute name is always in String
         */
        String strAttributeName = event.getName();
        /**
         * Get the attribute value
         * Attribute value can be String, Object, etc...
         */
        Object objAttributeValue = event.getValue();
        /* Print the value of AttributeName and AttributeValue */
        System.out.println("Attribute replaced: [" + strAttributeName + " : " + objAttributeValue + "]");

    }
}

Attach SessionAttributeListener in your web application through web.xml . Add following code in you web.xml between web-app tag.
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
...
...
    <servlet>
        <servlet-name>SessionAttributeServlet</servlet-name>
        <servlet-class>com.javaquery.servlet.SessionAttributeServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>SessionAttributeServlet</servlet-name>
        <url-pattern>/SessionAttributeServlet</url-pattern>
    </servlet-mapping>
    <listener>
        <listener-class>com.javaquery.Listeners.SessionAttributeListener</listener-class>
    </listener>
</web-app>

Source Code (Sample Servlet)
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class SessionAttributeServlet extends HttpServlet {
    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            HttpSession httpSession = request.getSession();
            /* SessionAttributeListener.attributeAdded(HttpSessionBindingEvent event) will be called */
            httpSession.setAttribute("username", "vicky.thakor"); 
            /* SessionAttributeListener.attributeRemoved(HttpSessionBindingEvent event) will be called */
            httpSession.setAttribute("username", "rajpriya.chudasama");
            /* SessionAttributeListener.attributeReplaced(HttpSessionBindingEvent event) will be called */
            httpSession.removeAttribute("username");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    }
}

java.lang.IllegalStateException: More than the maximum number of request parameters (GET plus POST) for a single request ([512]) were detected.


This is very unusual error thrown by Jboss server. Error it self explain that you are passing more than 512 parameters in request. Which is not good practice but if situation drives you in that way. There is solution available for it.

"To change this limit, set the maxParameterCount attribute on the Connector." this message seems informative but actually its wrong message in case of JBoss 7 or may be in future versions.

Error message you've
java.lang.IllegalStateException: More than the maximum number of request parameters (GET plus POST) for a single request ([512]) were detected. Any parameters beyond this limit have been ignored. To change this limit, set the maxParameterCount attribute on the Connector.
 org.apache.tomcat.util.http.Parameters.addParameter(Parameters.java:199)
 org.apache.tomcat.util.http.Parameters.processParameters(Parameters.java:383)
 org.apache.tomcat.util.http.Parameters.processParameters(Parameters.java:229)
 org.apache.catalina.connector.Request.parseParameters(Request.java:2874)
 org.apache.catalina.connector.Request.getParameter(Request.java:1291)
 org.apache.catalina.connector.RequestFacade.getParameter(RequestFacade.java:363)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:754)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
Solution:
We need to use org.apache.tomcat.util.http.* library to solve this error. You don't need to import or download this library. It already available in Jboss. All you need to increase value of Parameter Count.

Jboss 7
Step 1: Open standalone.xml file available in "jboss7\standalone\configuration" folder.
Step 2: Paste below code after </extensions> tag and change value as per your requirement.
<system-properties>
   <property name="org.apache.tomcat.util.http.Parameters.MAX_COUNT" value="2000"/>
</system-properties>
Jboss 6 or lower (I didn't try but it has to work in lower version of Jboss as suggested by informative message)
Step 1: Open standalone.xml file available in "jboss6\standalone\configuration" folder.
Step 2: Find connector tag same as follow and add maxParameterCount attribute.
<connector name="http" protocol="HTTP/1.1" scheme="http" socket-binding="http" maxParameterCount="1000" />