/etc/init.d/mysqld stop
mysqld_safe --skip-grant-tables & mysql -u root
mysql> use mysql;
mysql> update user set password=PASSWORD("newrootpassword") where User='root';
mysql> flush privileges;
mysql> quit
/etc/init.d/mysqld stop
/etc/init.d/mysqld start
2daytech
Tuesday, October 5, 2010
Monday, September 27, 2010
missing return statement
class A{
public int getValue(){
if(true)
return 1;
}
}
ans:Error: missing return statement
compile time error
----------------------------------------
class A{
public int getValue(){
if(true)
return 1;
else
return 2;
}
}
output: 1
--------------------------------------------
class A{
public int getValue(){
if(true)
return 1;
return 2;
}
}
output: 1
-----------------------------------------------------
class A{
public int getValue(){
if(!true)
return 1;
else
return 2;
}
}
output: 2
------------------------------------------------------
public class Missing {
public static void main(String[] args) {
System.out.println(new A().getValue());
}
}
public int getValue(){
if(true)
return 1;
}
}
ans:Error: missing return statement
compile time error
----------------------------------------
class A{
public int getValue(){
if(true)
return 1;
else
return 2;
}
}
output: 1
--------------------------------------------
class A{
public int getValue(){
if(true)
return 1;
return 2;
}
}
output: 1
-----------------------------------------------------
class A{
public int getValue(){
if(!true)
return 1;
else
return 2;
}
}
output: 2
------------------------------------------------------
public class Missing {
public static void main(String[] args) {
System.out.println(new A().getValue());
}
}
Thursday, September 23, 2010
ant script for web apps
before getting started our ant script you should know development and deployment structures of web application.
lib- all jar
src- all java or POJO
web or WebContent- web folder
etc-config file
dist-
build-
1. create a "build.properties"
root.dir=..
lib.dir=lib
src.dir=src
dist.dir=dist
build.dir=build
web.content=WebContent
build.classpath=build/WEB-INF/classes
project.name=Opgear
2. create a "build.xml" for automated the war deployment
our target
1.clean
2.makedir
3.compile
4.copy dependencies
5.war
< ?xml version="1.0" encoding="UTF-8" standalone="no"? >
< project basedir="." default="build" name="opgear" >
< property file="build.properties"/ >
< property environment="env"/ >
< property name="ECLIPSE_HOME" value="../../eclipse"/ >
< property name="debuglevel" value="source,lines,vars"/ >
< property name="target" value="1.6"/ >
< property name="source" value="1.6"/ >
< path id="java.classpath" >
< pathelement location="bin"/ >
< /path >
< !-- First, I create my classpath (build.classpath) from all the jar files in my lib directory -- >
< path id="build.classpath" >
< fileset dir="lib" >
< include name="**/*.jar" / >
< /fileset >
< /path >
< !-- Deletes or clear the existing build, dist directory-- >
< target name="clean" >
< delete dir="${build.dir}" / >
< delete dir="${build.classpath}" / >
< delete dir="${dist.dir}" / >
< /target >
< !-- Creates the build, docs and dist directory-- >
< target name="makedir" >
< mkdir dir="${build.dir}" / >
< mkdir dir="${build.classpath}" / >
< mkdir dir="${dist.dir}" / >
< /target >
< !-- Compiles the java code -- >
< target name="compile" depends="clean, makedir" >
< echo > Compile the source files< /echo >
< javac srcdir="${src.dir}" destdir="${build.dir}/WEB-INF/classes" classpathref="build.classpath" >
< /javac >
< /target >
< !--copy the compiled classess and webcontent to build dir -- >
< target name="copy" depends="compile" >
< echo > todir ${build.dir} < /echo >
< copy todir="${build.dir}" >
< fileset dir="${web.content}"/ >
< /copy >
< copy todir="${build.dir}/WEB-INF/lib" >
< fileset dir="${lib.dir}"/ >
< /copy >
< /target >
< !--Creates the deployable jar file -- >
< target name="war" depends="copy" >
< echo > Building the war file< /echo >
< war destfile="${dist.dir}/${project.name}.war" webxml="${build.dir}/WEB-INF/web.xml" >
< fileset dir="${build.dir}"/ >
< /war >
< /target >
< target name="build" depends="compile,war" >
< description > Main target< /description >
< /target >
< /project >
Wednesday, September 22, 2010
Singleton Pattern
The Singleton Pattern ensures a class has only one instance, and provides a global point of access to it.
Singleton Code Example
public final class MySingleton {
private static MySingleton instance;
private MySingleton(){ }
public static MySingleton getInstance(){
if(instance==null){
instance = new MySingleton();
}
return instance;
}
}
note:
Singleton Code Example
public final class MySingleton {
private static MySingleton instance;
private MySingleton(){ }
public static MySingleton getInstance(){
if(instance==null){
instance = new MySingleton();
}
return instance;
}
}
note:
Thread safety warning!
As shown above, the singleton is not thread safe. Used properly, singletons can be beneficial. Some people believe they are just evil. There are two camps, as usual. An alternative to the Singleton pattern is using a factory class to manage singletons. In other words, inside of a factory class, you can ensure that one and only one object is created.
If you are thinking about implementing this pattern, please review the following article first:
- Two approaches to creating thread-safe singletons
- "Effective Object-Oriented Design," and " Class Instances,
Regular Expressions syntax
Commonly, use of Regular Expressions in Java will look something like this:
Pattern pattern = Pattern.compile("INSERT REGULAR EXPRESSION");
Matcher matcher = pattern.matcher("INSERT INPUT STRING TO SEARCH");
boolean found = false;
while (matcher.find()) {
...do something...
}
def:
* A Pattern object is a compiled representation of a regular expression.
* A Matcher object is the engine that interprets the pattern and performs match operations against an input string.
Pattern pattern = Pattern.compile("INSERT REGULAR EXPRESSION");
Matcher matcher = pattern.matcher("INSERT INPUT STRING TO SEARCH");
boolean found = false;
while (matcher.find()) {
...do something...
}
def:
* A Pattern object is a compiled representation of a regular expression.
* A Matcher object is the engine that interprets the pattern and performs match operations against an input string.
Prepare the Tomcat Manager application
In order to deploy a web app to your Tomcat server, you will need to ensure that you can access the Tomcat Manager application at: http://localhost:8080/manager/html.
Typically, you just need to ensure that your/conf/tomcat-users.xml file has the following defined:
< ?xml version='1.0' encoding='utf-8'?>
< tomcat-users>
< role rolename="manager"/>
< role rolename="admin"/>
< user username="admin" password="admin" roles="admin,manager"/>
< /tomcat-users>
In this case, we will be logging in to the Tomcat Manager app using:
username admin
password admin
Typically, you just need to ensure that your
< ?xml version='1.0' encoding='utf-8'?>
< tomcat-users>
< role rolename="manager"/>
< role rolename="admin"/>
< user username="admin" password="admin" roles="admin,manager"/>
< /tomcat-users>
In this case, we will be logging in to the Tomcat Manager app using:
username admin
password admin
Tuesday, September 21, 2010
ant superb
Apache Ant is a powerful way to convert your developmental structures to deployment structures. It is declarative and all the command line tasks used for deploying an application are represented by simple XML elements. Without much details, this tutorial would breeze you through the steps on how to build a web application using a single XML build file and nothing else. If you have not yet understood what is the use of Ant, read my article on Development and Deployment Structures – the perfect way to build web applications.
I would use the same analogy of my development structure as mentioned in the above linked article i.e. my development structure consists of the following directories:
1. web – place for all my JSP, HTML, JavaScripts and Stylesheets. You can provide subdirectories as required for each of the different set of files
2. src – place for my java class files consisting of POJO’s or servlets.
3. etc – place for all config files like the most common web.xml
4. lib - place for all the necessary jar files to run my application. I even have included servlet-api.jar in this directory since you may have to deploy your web application in a remote server after compiling.
5. build – a temporary directory for keeping the compiled files
6. dist – a place to put in the finally packaged war file (distribution)
7. ant- place for my build.xml file and external properties file
All these directories are present in a parent directory called “WebApp“. The three things you have to keep in mind for making an Ant build file are -
1. tasks - which correspond to your command line tasks like javac, war etc. A group of tasks can be executed in a sequence by specifying targets.
2. target – It is like a function where you put reusable tasks so that it can be called later without duplicating them.
3. property – this element is used to define variables in your build file which is useful when the value like project_name or folder_name keeps on changing.
One of the most awesome features of Ant is that you can keep the properties file externally inside of defining all the variables within the build file. This properties file consists of all the required variables and their values in the form of name-value pairs and is simple text file. In this tutorial I would be using an external properties file.
For the tutorial I have used the following:
1. Java version 1.5
2. Apache Tomcat 5.0
3. Apache Ant 1.6.5
Okay we are now good to go with the great Ant tutorial. For this make a file with the name “build.properties“. This would be our external variables file.
root.dir=..
lib.dir=lib
src.dir=com
conf.dir=etc
web.content=web
project.name=WebApp
build.dir=build
NOTE: you must hit the ENTER key for starting a new line after the last line or Ant will give you an error “BUILD FAILED for some unknown reason”
It is not necessary to put in the name with the dot. You can have a name as “projectName” but there should be no quotes for the value. i.e it should NOT be projectName=”WebApp”
To make a build file we have to remember what is to be done for the deployments. To make things simple just make the modules (targets) necessary for the complete web application deployment which are
1. clean – remove all prior deployments of the same application if made
2. init – make the necessary structure for deployment as per the vendor’s
3. compile – compile your servlets or POJO’s
4. copy – put the compiled files and web content in the deployment structre as created during init
5. war – make the war file and open the browser
Start making the build.xml file in the ant folder as follows:
< project name="AppBuilder" default="war" basedir="..">
< property file="ant/build.properties"/>
< /project>
Now we have to set the classpath for the servlet-api.jar to compile our servlets so put the servlet-api.jar in the lib directory of your development structure. Check the property “default” in the element. It states the LAST module (target) inside the build file. In our case it is “war”
All the following XML elements will now go inside the element created above. So put the classpath setting element,
< path id="classpath">
< fileset dir="${lib.dir}" includes="servlet-api.jar"/>
< /path>
Here you need to set all the necessary jars to your classpath for successful compilation of the java source files. ${lib.dir} is used to retrive the value of the “lib.dir” i.e lib
Now we will start with out targets (modules) as mentioned in the list above :
1. clean
< target name="clean">
< echo>Cleaning the ${build.dir}
< delete dir="${build.dir}"/>
< /target>
Here I am removing my build dir in case it was present earlier with compiled files
The element is only for displaying what you are doing in the command line.
2. init
< target name="init" depends="clean">
< echo>Creating the build directory
< mkdir dir="${build.dir}/WEB-INFclasses"/>
< mkdir dir="${build.dir}/WEB-INFlib"/>
< /target>
Here I am creating the normal deployment structure required for tomcat namely WebApp/WEB-INF/classes, etc. It doesn’t matter if the folder WEB-INF existed before making the folder classes. Ant automatically creates all the necessary parent folders if they don’t exist.
3. compile
< target name="compile" depends="init">
< echo>Compile the source files
< javac srcdir="${src.dir}" destdir="${build.dir}/WEB-INF/classes">
< /javac>
< /target>
Most important step and the most error giving step. Make sure that all the classpath have been set in the above element. If all are proper then the all files in “src.dir” will be compiled successfully and moved to the build\WebApp\WEB-INF\classes. Just check the property “depends” of the element here. This property is used to chain the modules (targets) in a sequential manner.
4. copy
< target name="copy" depends="compile">
< copy todir="${build.dir}/WEB-INF">
< fileset dir="${conf.dir}"/>
< /copy>
< copy todir="${build.dir}">
< fileset dir="${web.content}"/>
< /copy>
< copy todir="${build.dir}/WEB-INF/lib">
< fileset dir="${lib.dir}"/>
< /copy>
< /target>
Here I am just copying my complied classes and web content files inside the corresponding deployment structure.
5. war
< target name="war" depends="copy">
< echo>Building the war file
< war destfile="${dist.dir}/${project.name}.war" webxml="${build.dir}/WEB-INF/web.xml">
< fileset dir="${build.dir}"/>
< /war>
< /target>
This is the final module (target) in my build.xml file which makes the WebApp.war required for the deployment. “war” is an Ant task for which you provide the path of the web.xml file and the directory which contains the deployment structure i.e in our case “build” directory. The destfile is the final location and name of the war file which would become dist\WebApp.war after the script has run.
I would use the same analogy of my development structure as mentioned in the above linked article i.e. my development structure consists of the following directories:
1. web – place for all my JSP, HTML, JavaScripts and Stylesheets. You can provide subdirectories as required for each of the different set of files
2. src – place for my java class files consisting of POJO’s or servlets.
3. etc – place for all config files like the most common web.xml
4. lib - place for all the necessary jar files to run my application. I even have included servlet-api.jar in this directory since you may have to deploy your web application in a remote server after compiling.
5. build – a temporary directory for keeping the compiled files
6. dist – a place to put in the finally packaged war file (distribution)
7. ant- place for my build.xml file and external properties file
All these directories are present in a parent directory called “WebApp“. The three things you have to keep in mind for making an Ant build file are -
1. tasks - which correspond to your command line tasks like javac, war etc. A group of tasks can be executed in a sequence by specifying targets.
2. target – It is like a function where you put reusable tasks so that it can be called later without duplicating them.
3. property – this element is used to define variables in your build file which is useful when the value like project_name or folder_name keeps on changing.
One of the most awesome features of Ant is that you can keep the properties file externally inside of defining all the variables within the build file. This properties file consists of all the required variables and their values in the form of name-value pairs and is simple text file. In this tutorial I would be using an external properties file.
For the tutorial I have used the following:
1. Java version 1.5
2. Apache Tomcat 5.0
3. Apache Ant 1.6.5
Okay we are now good to go with the great Ant tutorial. For this make a file with the name “build.properties“. This would be our external variables file.
root.dir=..
lib.dir=lib
src.dir=com
conf.dir=etc
web.content=web
project.name=WebApp
build.dir=build
NOTE: you must hit the ENTER key for starting a new line after the last line or Ant will give you an error “BUILD FAILED for some unknown reason”
It is not necessary to put in the name with the dot. You can have a name as “projectName” but there should be no quotes for the value. i.e it should NOT be projectName=”WebApp”
To make a build file we have to remember what is to be done for the deployments. To make things simple just make the modules (targets) necessary for the complete web application deployment which are
1. clean – remove all prior deployments of the same application if made
2. init – make the necessary structure for deployment as per the vendor’s
3. compile – compile your servlets or POJO’s
4. copy – put the compiled files and web content in the deployment structre as created during init
5. war – make the war file and open the browser
Start making the build.xml file in the ant folder as follows:
< project name="AppBuilder" default="war" basedir="..">
< property file="ant/build.properties"/>
< /project>
Now we have to set the classpath for the servlet-api.jar to compile our servlets so put the servlet-api.jar in the lib directory of your development structure. Check the property “default” in the
All the following XML elements will now go inside the
< path id="classpath">
< fileset dir="${lib.dir}" includes="servlet-api.jar"/>
< /path>
Here you need to set all the necessary jars to your classpath for successful compilation of the java source files. ${lib.dir} is used to retrive the value of the “lib.dir” i.e lib
Now we will start with out targets (modules) as mentioned in the list above :
1. clean
< target name="clean">
< echo>Cleaning the ${build.dir}
< delete dir="${build.dir}"/>
< /target>
Here I am removing my build dir in case it was present earlier with compiled files
The
2. init
< target name="init" depends="clean">
< echo>Creating the build directory
< mkdir dir="${build.dir}/WEB-INFclasses"/>
< mkdir dir="${build.dir}/WEB-INFlib"/>
< /target>
Here I am creating the normal deployment structure required for tomcat namely WebApp/WEB-INF/classes, etc. It doesn’t matter if the folder WEB-INF existed before making the folder classes. Ant automatically creates all the necessary parent folders if they don’t exist.
3. compile
< target name="compile" depends="init">
< echo>Compile the source files
< javac srcdir="${src.dir}" destdir="${build.dir}/WEB-INF/classes">
< /javac>
< /target>
Most important step and the most error giving step. Make sure that all the classpath have been set in the above
4. copy
< target name="copy" depends="compile">
< copy todir="${build.dir}/WEB-INF">
< fileset dir="${conf.dir}"/>
< /copy>
< copy todir="${build.dir}">
< fileset dir="${web.content}"/>
< /copy>
< copy todir="${build.dir}/WEB-INF/lib">
< fileset dir="${lib.dir}"/>
< /copy>
< /target>
Here I am just copying my complied classes and web content files inside the corresponding deployment structure.
5. war
< target name="war" depends="copy">
< echo>Building the war file
< war destfile="${dist.dir}/${project.name}.war" webxml="${build.dir}/WEB-INF/web.xml">
< fileset dir="${build.dir}"/>
< /war>
< /target>
This is the final module (target) in my build.xml file which makes the WebApp.war required for the deployment. “war” is an Ant task for which you provide the path of the web.xml file and the directory which contains the deployment structure i.e in our case “build” directory. The destfile is the final location and name of the war file which would become dist\WebApp.war after the script has run.
Subscribe to:
Posts (Atom)