Redirecting a web page means, taking user to new location. Many website use redirect for many different reasons, e.g. Some website redirect user from old domain to new domain, some redirect from one page to another e.g. a more relevant page. If you are Java programmer, and worked previously with Servlet and JSP, then you might be familiar with SendRedirect and Forward methods, which are used to redirect user in web applications. Actually there are two kinds of redirect, Server side redirect and client side redirect. In Server side redirect, server initiate redirection, while in client side redirect, client code does the redirection. Redirecting a web page using JavaScript and JQuery is a client side redirection. Well, HTTP redirection is a big topic and different people do it differently. e.g. bloggers mostly used platform like WordPress, Blogger feature to redirect a page, though this might be restricted to same domain. In this JavaScript and JQuery tutorial, we will learn a new JQuery tip to redirect a page.
Wednesday, December 25, 2013
Monday, December 23, 2013
Restore master database with Dell NetVault Litespeed for SQL Server
The DBA teams asked if I could create a job in our maintenance routines to do a native backup of the master database instead of a Litespeed backup. Their reason was they weren't able to restore master using Litespeed because the database server had to be in single-user mode to do such a restore and Litespeed required two connections to the server to do the task.
I mentioned that there are instructions in the Litespeed help files for a master database restore.
The team told me that it didn't work for them. So, I took the task to validate the instructions found here https://support.quest.com/search/SolutionDetail.aspx?id=SOL13484
I took a backup of the master database at 9:44 AM.
Next, I created a login on the server after the backup called ThisLoginWillNotBeHereAfterMasterDatabaseRestore to prove the master database restore did in fact happen.
This login will no longer exist after the upcoming master database restore because it was not captured by the backup taken at 9:44 AM.
I stopped the SQL Server services for the instance. Either SSMS or SQL Configuration Manager can be used to stop the services. When you stop the database instance, the corresponding SQL Agent will also be stopped.
I started the database instance in single user mode in one command window.
The instance has started in single user mode.
I open a second command window and ran the database restore command.
The restore runs successfully in seconds.
Additional messages are written to the first command window and the command prompt returns.
I restart the database instance using the SQL Configuration Manager.
I start SQL Server Management Studio to find the login created after the backup no longer exists proving the master database has been restored.
The database restore was completed in a few seconds. The entire process probably takes less than 5 minutes.
I took a bit longer because I was taking screenshots. ;-)
Thursday, December 19, 2013
JUnit Testing Tips - Constructor is Called Before Executing Test Methods
Most of Java programmers either use JUnit or TestNG for there unit testing need, along with some mock object generation libraries e.g. Mockito, but not everyone spend time and effort to learn subtle details of these testing libraries, at least not in proportion of any popular framework e.g. Spring or Hibernate. In this blog post, I am sharing one of such details, which has puzzled me couple of years ago. At that time, though I had been using JUnit for significant time, I wasn't aware that code written inside constructor of Test class is executed before each test method. This behaviour of JUnit has caused, some of my test to failed and putting hours of investigation in my code, without realizing that this is happening because of JUnit is initializing object by calling constructor before executing test method annotated with @Test annotation. I had following code to test my vending machine implementation as coding exercise. If you look at closely, I have initialized vending machine in class body, which is executed as part of constructor. I was assuming one instance of vending machine is shared between all test methods, as I was not using @Before and @After, JUnit 4 annotation for setup() and tearDown(). In first test, one item from Inventory is consumed and in second test another item, but when you assert count of items based upon previous test, it will fail, because you are testing a different vending machine instance, which has different inventory. So always, remember that JUnit calls constructor of test class before executing test method. You can verify it by putting a System.out.println message in constructor itself.
Read more �
Sunday, December 15, 2013
Typing master
Use TypingMaster Touch Typing Tutor to Double Your Typing Speed. Fast, Personalized Training. Download Free Trial!
http://www.typingmaster.com/fr/pro/demo.asp
Thursday, December 12, 2013
How to configure Log4j in Java program without XML or Properties File
Sometime configuring Log4j using XML or properties file looks annoying, especially if your program not able to find them because of some classpath issues, wouldn't it be nice if you can configure and use Log4j without using any configuration file e.g. XML or properties. Well, Log4j people has thought about it and they provide a BasicConfigurator class to configure log4j programmatically, thought this is not as rich as there XML and properties file version is, but it's really handy for quickly incorporating Log4j in your Java program. One of the reason programmer prefer to use System.out.println() over Log4j, of-course for testing purpose, because it doesn't require any configuration, you can just use it, without bothering about XML or properties file configuration, but most programmer will agree that, they would prefer to use Log4j over println statements, even for test programs if it's easy to set them up. By the way, for production use prefer SLF4J over Log4J. In this Java tutorial, we will see a nice little tip to use Log4j right away by configuring in couple of lines. In fact you can configure Log4J in just one line, if you intend to use there basic configuration which set's the log level as DEBUG.
Read more �
Tuesday, December 10, 2013
There is more than one way to find database backup files
I was told during the initial implementation of Ola Hallengren's Maintenance Solution at my workplace that it was too hard to find database backup files in the default folder structure his solution creates. The concern was that it took too long to find the oldest file or the biggest file when trying to resolve a space issue on a database server.
Ola's solution creates a folder tree with these elements. Drive:\root\ServerName\DatabaseName\TypeOfBackup
You specify the backup drive and root folder using the Directory parameter of the DatabaseBackup stored procedure.
When I originally solved this problem, Windows XP was in use. I wish I had spent more time figuring this out because I had to modify Ola's database backup procedure to dump all the backups into one folder in order for the DBA teams to sign off on the initial deployment.
It only takes a couple of minutes to search using Windows Explorer in Windows XP but finding files in a folder tree has gotten much easier on Windows 7 and higher.
Given Windows XP's imminent retirement, I will only cover Windows 7 or higher.
GUI
Start Windows Explorer and navigate to the backup root folder on the server.
Type *.bak in the Search field in the upper right hand corner of Windows Explorer on Windows 7 or 8 and press Enter.
By default, Windows Explorer in Windows 7 and higher searches sub-folders.
The Search result returns all the database backups
in the entire folder tree.
The two requirements given were finding files by size or date.
Both are easily solved by clicking on the appropriate header.
In the search result, headers are clickable and allow sorting by any column.
So, click on Date Modified to find files by date or click on Size to find files by size.
Finding files to delete by size or date is done in less than a minute.
You can even save searches now.
http://www.howtogeek.com/howto/5316/how-to-save-searches-in-windows-7/
Microsoft's GUI interfaces are sufficient if you only have a few servers to check. But GUIs don't scale when you need to do the same task on a few hundred servers. So, you'll need to do a bit of scripting.
PowerShell
Google answered this question pretty quickly.
Find the ten largest files in a directory.
http://stackoverflow.com/questions/798040/find-the-10-largest-files-within-a-directory-structure
get-childitem -path C:\Backup -recurse | ?{ -not $_.PSIsContainer } | sort-object Length -desc | select-object fullname -f 10
gci C:\Backup -r | ?{ -not $_.PSIsContainer } | sort Length -desc | select fullname -f 10
Finding the oldest ten files in a folder tree by date
get-childitem C:\Backup -recurse | ?{ -not $_.PSIsContainer } | sort-object LastWriteTime | select-object fullname -f 10
gci C:\Backup -r | ?{ -not $_.PSIsContainer } | sort LastWriteTime | select fullname -f 10
gci \\ServerName\FolderName\dump_data -r | ?{ -not $_.PSIsContainer } | sort Length -desc | select fullname -f 10
Including ?{ -not $_.PSIsContainer } in the pipeline ensures only files not folders are included in the results.
If you want to run these commands on multiple servers, wrap them in a foreach loop that reads a list of text files from a server. Check out my PowerShell posts for examples.
Old School
If you are not comfortable with PowerShell, the venerable dir command from the Windows command line is still available.
Oldest files first in a folder tree
dir /S /OD | more
Files ordered by size largest first in folder tree
dir /S /O-S | more
So, it doesn't take very long to find the largest or oldest database backup using any of these methods. Which means I didn't need to modify Ola's scripts for the next release. ;-)
Ola's solution creates a folder tree with these elements. Drive:\root\ServerName\DatabaseName\TypeOfBackup
You specify the backup drive and root folder using the Directory parameter of the DatabaseBackup stored procedure.
When I originally solved this problem, Windows XP was in use. I wish I had spent more time figuring this out because I had to modify Ola's database backup procedure to dump all the backups into one folder in order for the DBA teams to sign off on the initial deployment.
It only takes a couple of minutes to search using Windows Explorer in Windows XP but finding files in a folder tree has gotten much easier on Windows 7 and higher.
Given Windows XP's imminent retirement, I will only cover Windows 7 or higher.
GUI
Start Windows Explorer and navigate to the backup root folder on the server.
Type *.bak in the Search field in the upper right hand corner of Windows Explorer on Windows 7 or 8 and press Enter.
By default, Windows Explorer in Windows 7 and higher searches sub-folders.
The Search result returns all the database backups
in the entire folder tree.
The two requirements given were finding files by size or date.
Both are easily solved by clicking on the appropriate header.
In the search result, headers are clickable and allow sorting by any column.
So, click on Date Modified to find files by date or click on Size to find files by size.
Finding files to delete by size or date is done in less than a minute.
You can even save searches now.
http://www.howtogeek.com/howto/5316/how-to-save-searches-in-windows-7/
Microsoft's GUI interfaces are sufficient if you only have a few servers to check. But GUIs don't scale when you need to do the same task on a few hundred servers. So, you'll need to do a bit of scripting.
PowerShell
Google answered this question pretty quickly.
Find the ten largest files in a directory.
http://stackoverflow.com/questions/798040/find-the-10-largest-files-within-a-directory-structure
get-childitem -path C:\Backup -recurse | ?{ -not $_.PSIsContainer } | sort-object Length -desc | select-object fullname -f 10
gci C:\Backup -r | ?{ -not $_.PSIsContainer } | sort Length -desc | select fullname -f 10
Finding the oldest ten files in a folder tree by date
get-childitem C:\Backup -recurse | ?{ -not $_.PSIsContainer } | sort-object LastWriteTime | select-object fullname -f 10
gci C:\Backup -r | ?{ -not $_.PSIsContainer } | sort LastWriteTime | select fullname -f 10
gci \\ServerName\FolderName\dump_data -r | ?{ -not $_.PSIsContainer } | sort Length -desc | select fullname -f 10
Including ?{ -not $_.PSIsContainer } in the pipeline ensures only files not folders are included in the results.
If you want to run these commands on multiple servers, wrap them in a foreach loop that reads a list of text files from a server. Check out my PowerShell posts for examples.
Old School
If you are not comfortable with PowerShell, the venerable dir command from the Windows command line is still available.
Oldest files first in a folder tree
dir /S /OD | more
Files ordered by size largest first in folder tree
dir /S /O-S | more
So, it doesn't take very long to find the largest or oldest database backup using any of these methods. Which means I didn't need to modify Ola's scripts for the next release. ;-)
Wednesday, December 4, 2013
When to make a method final in Java
Final keyword in Java is not as mysterious as volatile or transient, but still it creates lot of doubts on programmers mind. I often receives questions like, When to make a method final in Java or When to make a method static in Java, later I answered in my earlier post. Questions like this is not trivial, knowing what a keyword does is just small part of mastering that functionality. Similar to real world, where knowing that a sword can cut lot of things is not enough for a warrior to survive, you need to know how to use that and more importantly, use if effectively. Final keyword can be applied to class, methods and variable and has different meaning for each of them, but the motive remains same, it state completeness, it opposes change. For example, final class can not be extended, value of final variable can not be change and a final method can not be overridden in Java. This gives you first hint, when to make a method final in Java, obviously to prevent subclass for changing it's definition, technical preventing it from overridden. There are lot of clues on How to use final keyword on methods in Java API itself, one of the prime example of this java.lang.Object class, which declares a lot of method final including wait method with timeout. Another example of making a method final is template method, which outlines algorithm in Template method design pattern. Since, you don't want a sub class to change the outline of algorithm, making it final will prevent any accidental or malicious overriding. In this tutorial, we will learn few things, which will help you to effectively use final keywords with Java methods.
Read more �
Sunday, December 1, 2013
Friday, November 29, 2013
Java vs Python - Which Programming Language Should Learn First
Java and Python are two of most popular and powerful programming language of present time. Beginner programmer often get confused, one of the most frequently asked question is should I learn Java or Python, Is Python is good programming language to start with, Which programming language would you recommend for beginners to learn first etc. Since I am a Java developer, my opinion is biased, I will always suggest you to start with Java and then learn Python, but if you ask this question to a Python developer, you might get just opposite answer. I have well documented my reasons as Why Java is best Programming language and Why a programmer should learn Java. One of the most important reason you would see on that blog post is strong Java community, which will help you though out your Java career. You can ask some beginner stuff starting from how to set PATH and classpath to advanced stuff about debugging Java program in Eclipse, no matter what kind of question is, there is always some one is Java community, who is ready to answer and help you. This is one of the reason that StackOverflow is full of Java questions. By the way Python is not spring chicken anymore, it has fully grown and giving strong competition to main stream language like Java and C++. When I first come across Python, I thought it's a scripting language, but that is an understatement. You can do object oriented programming in Python as well. On beginners point of view, I always suggest pick a language which is easier to learn, powerful to attract you and have strong community support, now both Java and Python fits this bill, and until you do some really good comparative analysis, you can not decide which language to learn from Java vs Python. Thankfully, we have an infographic, which highlights some important difference between Python and Java, I am sure after taking a look on this Infographic, you will be able to decide which is the right programming language to start with.
Read more �
Thursday, November 28, 2013
Difference between static vs non static method in Java
In this article, we will take a look at difference between static and non static method in Java, one of the frequently asked doubt from Java beginner. In fact, understanding static keyword itself is one of the main programming fundamental, thankfully it's well defined in Java programming language . A static method in Java belongs to class, which means you can call that method by using class name e.g. Arrays.equals(), you don't need to create any object to access these method, which is what you need to do to access non static method of a class. Static method are treated differently by compiler and JVM than non static methods, static methods are bonded during compile time, as opposed to binding of non static method, which happens at runtime. Similarly you can not access non static members inside static context, which means you can not use non static variables inside static methods, you can not call non static methods from static ones, all those will result in compile time error. Apart from these significant differences between static and non static methods, we will also take a look at few more points in this Java tutorial, with a code example, which shows some of these differences in action.By the way this is the second article on static method, in first article, we have learned about when to use static method in Java
Read more �
Labels:
core java,
core java interview question,
programming
Tuesday, November 26, 2013
Monday, November 25, 2013
Informatique-solution de l'algorithm récursif : series d’exercice N01
Ex
01 :
Fonction Somme(n :entier) :entier
Début
Si n=0
alors
Somme <== 0
sinon
Somme <==Somme(n+1)+n
Fin
Ex
02 :
a)Fonction
quotion(a,b :entier) :entier
Début
Si
b<>0 alors
Début
Si a<b alors
quotion<== 0
Sinon
quotion <==quotion(a-b,b)+1
fin
sinon
quotion <== -1
Fin
b) Fonction
reste(a,b :entier) :entier
Début
Si a<b alors
reste <== a
Sinon
Reste <== reste (a-b,b)+1
Fin
Ex 03 :
Procédure
parcours (tete :pointeur)
Variable p :pointeur
i :entier
Début
p <== tete
i <== 0
Si p< > NIL alors
Début
…….(traiter(p))
Parcours(suivant(p))
Fin
Fin
Ex
04 :
Fonction
longer (tete :pointeur) :entier
Variable p :pointeur
Début
p <== tete
Si p= NIL alors
Longer <== 0
Sinon
Longer <==Longer(suivant(p)+1)
Fin
Ex
05 :
Fonction
count (tête :pointeur, val=entier) :entier
Variable p :pointeur
Début
p <== tête
Si p= NIL alors
Count<== 0
Sinon
count<== count(suivant(p)+1)
sinon
count<== count(suivant(p))
Fin
Ex
06 :
a)Fonction
Rech (tête :pointeur, val=entier) :entier
Variable p :pointeur
a:entier
Début
p<==tet
Si p= NIL alors
Rech<== NIL
Sinon
Si a=val alors
Rech <==p
Sinon
Rech<== Rech(suivant(p),val)
Fin
b)
Fonction
position (tête : pointeur, pos=entier) : pointeur
Variable p :pointeur
Début
p<== tête
Si
pos> 0 alors
début
Si pos=1
alors
position <== p
Sinon
Si p=NIL alors
position <== NIL
Sinon
position<== position(suivant(p),pos-1)
fin
Fin
Ex
07 :
Fonction
s-liste (tete-l,tete-sl :pointeur) :booléen
Variable p,q :pointeur
Début
p <== tète -l
q<== tète -sl
Si p= NIL
alors
s-liste <==faux
Si corespond(p,q) alors
s-liste <== vrai
Sinon
s-liste<== s-liste(suivant(p),q)
fin
Ex
08 :
Fonction somme(a,b :entier) :entier
Début
Si a< b
alors
Début
c<== a
a<== b
b<== c
fin
si b=0 alors
somme<== a
sinon
somme<== somme(a+1,b-1)
Fin
Ex
09 :
a)Fonction prod(t :tableau de
entier,n :entier) :entier
variable
i :entier
Début
Si n=1
alors
prod<== t[i]
sinon
prod <==prod(t,n-1)*t[n]
Fin
b)Fonction
moyenne(t :tableau[] :entier,n :entier) :entier
Début
Si n=1
alors
moyenne <==t[1]
sinon
moyenne <==((n-1)*moyenne (t,n-1)+t[n]) /n
Fin
Quand utiliser la récursivité ?
*Quand la
décomposition est simple à trouver et à comprendre .
Quand ne pas utiliser la récursivité ?
*Quand il
existe une solution itérative simple et intuitive.
*Quand la problème
de la récursivité est trop grand (dépassement possible de la pile d’exécution).