Thursday 6 August 2015

What is the main difference between servlet context and servlet config?

ServletConfig

1.ServletConfig available in javax.servlet.*; package
2.ServletConfig object is one per servlet class
3.destroyed once the servlet execution is completed.
4.We should give request explicitly, in order to create ServletConfig object for the first time
5.Object of ServletConfig will be created during initialization process of the servlet
6.This Config object is public to a particular servlet only

ServletContext

1.ServletContext available in javax.servlet.*; package
2.ServletContext object is global to entire web application
3.Object of ServletContext will be created at the time of web application deployment
4.Scope: As long as web application is executing, ServletContext object will be available, and 5.it will be destroyed once the application is removed from the server.
6.ServletContext object will be available even before giving the first request

Sunday 19 April 2015

Remote Connection Settings for MySQL

Configuring your MySQL to access it from outside i.e. from any machine across intranet / internet is one of the basic needs of the developer.

The following sequence of steps are needed to achieve this.

We have to do the following changes,
  1. Change bind address in MySQL configuration file my.cnf to 0.0.0.0  This allows mysql to bind to all interfaces. Previous value may be  127.0.0.1
  2. In mysql DB we have added new user  root@%  and granted all permissions to it.
Command for reference,
  • select host,user,password from mysql.user
  • create user 'root'@'%' identified by 'pass';
  • GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;
  • GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION;

We ideally will not require to do  anything in iptables (Linux Firewall)  but in some cases we would need to open 3306 port in the firewall settings. 

Saturday 18 April 2015

Getting JVM heap size, used memory, total memory using Java Runtime

Getting JVM heap size, used memory, total memory using Java Runtime.

Reading runtime java virtual memory usage is useful when the system/application is struggling in getting resources. System Memory is one of the main resource that an application developer has to consider while managing the application.

Java’s Runtime class provide lot of information about the resource details of Java Virtual Machine or JVM. The memory consumed by the JVM can be read by different methods in Runtime class. Following is the  example of reading JVM Heap Size, Total Memory and used memory using Java Runtime api.

 public class MemoryUtilization {  
   public static void main(String [] args) {  
     int conversion = 1024*1024;       
     Runtime runtime = Runtime.getRuntime();       
     System.out.println("##### Heap utilization #####");       
     System.out.println("Used Memory:" + (runtime.totalMemory() - runtime.freeMemory()) / conversion);  
     System.out.println("Free Memory:" + runtime.freeMemory() / conversion);  
     System.out.println("Total Memory:" + runtime.totalMemory() / conversion);       
     System.out.println("Max Memory:" + runtime.maxMemory() /conversion);  
   }  
 }