Tuesday 27 August 2013

java.lang.OutOfMemoryError : Permgen space in eclipse

Step 1   Click on server
Step 2:    Click on Open launch configuration
Step 3:    Enter the following arguments in VM arguments.
-Dprogram.name=run.bat -Djava.endorsed.dirs="D:/jboss-4.2.3.GA/bin/../lib/endorsed" 
-Xms512m -Xmx1024m -XX:+UseParallelGC -XX:PermSize=256M -XX:MaxPermSize=512M
To change JBoss port while running
-Djboss.service.binding.set="ports-03
Program arguments:
-c default   or -c default –b 0.0.0.0
Step 4:   Copy the below statements into your jboss server- run.conf file.
# Specify options to pass to the Java VM.
if [ "x$JAVA_OPTS" = "x" ]; then
   JAVA_OPTS="-Xms512m -Xmx512m -XX:MaxPermSize=1024m -Dsun.rmi.dgc.client.gcInterval=3600000 -Dsun.rmi.dgc.server.gcInterval=3600000"
fi

Step 5:   Copy the below statements into your JBoss Server- run.bat file
rem With Sun JVMs reduce the RMI GCs to once per hour

set JAVA_OPTS=%JAVA_OPTS% -Dsun.rmi.dgc.client.gcInterval=3600000 -Dsun.rmi.dgc.server.gcInterval=3600000


JSP & CUSTOM TAG LIBRARY


JSP Implicit Objects :

  1. Request
  2. Response
  3. Session
  4. Application
  5. Config
  6. Page Context
  7. Out
  8. Page
  9. Exception


Custom Tag Library :

Step 1: Write a TagHandler Class by implements TagHandler or extends TagSupport
-> Write Setter & getter for attributes which are going to use in TLD file.
Step 2: Write tag Descriptor File called TLD and keep it in WEB-INF Directory.
Step 3: Now write a jsp page and use the custom taglib.

Example:

CustomTagClass.java 

Package com.customtagexaples.CustomTagClass
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
public class CustomTagClass extends TagSupport {
    private String input;
    private int start;
    private int end;
     
    @Override
    public int doStartTag() throws JspException {
         
        try {
            //Get the writer object for output.
            JspWriter out = pageContext.getOut();
            //Perform substr operation on string.
            out.println(input.substring(start, end));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return SKIP_BODY;
    }
    public String getInput() {
        return input;
    }
    public void setInput(String input) {
        this.input = input;
    }
    public int getStart() {
        return start;
    }
    public void setStart(int start) {
        this.start = start;
    }
    public int getEnd() {
        return end;
    }
    public void setEnd(int end) {
        this.end = end;
    }
}
CustomTagHandlerTLD.tld 


<?xml version="1.0" encoding="UTF-8"?>
<taglib>
<tlibversion>1.0</tlibversion>
<jspversion>1.1</jspversion>
<shortname>CustTag</shortname>
<info>Sample taglib for Custom tag operation</info>

<tag>
    <name>CustTagExample</name>
    <tagclass>com.customtagexaples.CustomTagClass</tagclass>
    <info>CustTagExample function.</info>
    <attribute>
      <name>input</name>
      <required>true</required>
    </attribute>
    <attribute>
      <name>start</name>
      <required>true</required>
    </attribute>
    <attribute>
      <name>end</name>
      <required>true</required>
    </attribute>
</tag>
</taglib
CustomeTagExample.jsp
<%@taglib prefix="test" uri="/WEB-INF/SubstrDescriptor.tld"%>
<html>
<head>
    <title>JSP Custom Taglib example: Substr function</title>
</head>
<body>
    SUBSTR(GOODMORNING, 1, 6) is :
    <font color="blue">
    <test:substring input="GOODMORNING" start="1" end="6"/>
    </font>
</body>
</html>
OutPut: 

SUBSTR(GOODMORNING, 1, 6) is: OODMO


Tuesday 13 August 2013

Communications link failure:

Communication Link Failure:
While login into application, we are getting communication link failure with the below error message.


10:22:35,453 ERROR JDBCExceptionReporter:78 - Communications link failure
The last packet successfully received from the server was 51,898,257 milliseconds ago.  The last packet sent successfully to the server was 47 milliseconds ago.
10:22:35,453 ERROR LoginDAOImpl:150 - LoginDAOImpl - getLogin()-org.hibernate.exception.JDBCConnectionException: could not execute query
10:22:35,453 ERROR JDBCTransaction:168 - JDBC rollback failed
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after connection closed.Connection was implicitly closed by the driver.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)




Solution 1: Implementing the Connection Pool mechanism
Solution 2: change default value in MySQL configuration file (connect-timeout option in mysqld section) -
[mysqld]
connect-timeout=100
Solution 4: Reconnect the session again after closing the session - For this need to right some code in your application.


Getting the session using hibernate :
public Session getSession() {

Long sessionFactoryCreationTime = (Long) ServletActionContext.getServletContext().getAttribute(HibernateListener.SEESSION_FACTORY_CREATION_TIME);
long currentMilliSeconds = System.currentTimeMillis();
long milliSeconds = currentMilliSeconds - sessionFactoryCreationTime.longValue();
int hours   = (int) ((milliSeconds / (1000*60*60)) % 24);
if (hours > 6) {
reCreateSessionFactory();
}
Session session = null;
SessionFactory sessionFactory =
         (SessionFactory) ServletActionContext.getServletContext()
                  .getAttribute(HibernateListener.KEY_NAME);
session = sessionFactory.openSession();
return session;
}

public void reCreateSessionFactory() {

Configuration config;
SessionFactory factory;
try {
       String urlPath =(String)             ServletActionContext.getServletContext().getAttribute(HibernateListener.CFG_PATH);
       URL url = HibernateListener.class.getResource(urlPath);
       config = new Configuration().configure(url);
       factory = config.buildSessionFactory();
       ServletActionContext.getServletContext().setAttribute(HibernateListener.KEY_NAME, factory);
       ServletActionContext.getServletContext().setAttribute(HibernateListener.SEESSION_FACTORY_CREATION_TIME, Long.valueOf(System.currentTimeMillis()));
 } catch (Exception e) {
 log.error("AbstractDaoImpl::reCreateConnection Exception"+e.getMessage());
  }
 log.info("AbstractDaoImpl::reCreateSessionFactory==>Exit");
}

HibernateListener.java:
public class HibernateListener  implements ServletContextListener {
private Logger log =LoggerFactory.getLogger(HibernateListener.class);
private String path = "hibernate.cfg.xml";
private static Class clazz = HibernateListener.class;

public static final String KEY_NAME = clazz.getName();
public static final String SEESSION_FACTORY_CREATION_TIME = "SEESSION_FACTORY_CREATION_TIME";
public static final String CFG_PATH = "CFG_PATH";

public void contextDestroyed(ServletContextEvent event) {
 //
}

/**
*
* This method initializes Hibernate Session.
* @param event
*
*/
public void contextInitialized(ServletContextEvent event) {
log.info("HibernateListener::contextInitialized==>Entry");
Configuration config;
 SessionFactory factory;
try {
       URL url = HibernateListener.class.getResource(path);
       config = new Configuration().configure(url);
       factory = config.buildSessionFactory();
       //save the Hibernate session factory into serlvet context
       event.getServletContext().setAttribute(KEY_NAME, factory);
       event.getServletContext().setAttribute(CFG_PATH, path);
       event.getServletContext().setAttribute(SEESSION_FACTORY_CREATION_TIME, Long.valueOf(System.currentTimeMillis()));
} catch (Exception e) {
log.error("HibernateListener::contextInitialized Exception"+e.getMessage());
}
log.info("HibernateListener::contextInitialized==>Exit");
}

}



Need to add more solutions...

Tuesday 6 August 2013

Disable the back button on Browser

Disable the back button on Browser:

Solution 1:

Step 1: Body onload write a function and call the below code.

<body onLoad="backButtonOverride()" onpageshow="if (event.persisted) backButtonOverride();" onunload="">

// write your html code.............

</body>

<script>
function backButtonOverride() {

window.history.forward();
setTimeout("backButtonOverride()", 1);

}
</script>

Here, window.history.go(1) same as window.history.forward(); 
          setTimeout calls the backButtonOverride() on every second. 1000 = 1 sec

Step 2: Add below code in servlet or any interceptor

   if(response!=null){
response.setHeader("Cache-control","private, no-cache, no-store");
response.setHeader("Pragma","no-cache");
response.setHeader("Expires","0");
response.setDateHeader("Expires",-1);
}  
        

Solution 2:

Some times you will get Expired Page Problem on click of Back button. 
For this you have send the method ="get" instead of "Post". 

For security reasons we have to write the encryption and decryption code for user sensitive data.
Please check the below link for reference program for encryption and decryption: 
http://www.roseindia.net/answers/viewqa/JSP-Servlet/25684-Password-encryption-and-decryption.html

Solution 3:

Using Struts 2 Token Interceptor concept we can solve this issue.

SpringBoot

SpringBoot SpringBoot Application :  Pros & Cons :  SpringBoot Application creation using spring.io :  SpringBoot Application Annotation...