What role did nationalism and imperialism play in the road to World War I?

What role did nationalism and imperialism play in the road to World War I?.Imperialism can be defined as a country’s desire (and efforts) to conquer an empire.  European countries that were imperialistic typically wanted to go to Africa and Asia and carve out imperial possessions for themselves.  They tended to think that having a large empire was a sign of national strength.  They also thought that having a large empire would help to make them richer and more militarily powerful.  For these reasons, countries competed with one another to gain the largest empires.  The competition led to animosity between countries.  This animosity helped lead to WWI.

Nationalism can be defined as the feeling that one’s nation (one’s people) is superior to other nations.  This helped lead to WWI in two ways.  First, nationalism can breed arrogance because it makes people feel superior.  It can lead them to want to prove their superiority through fighting.  It can also lead them to think that they will surely win any war because of their superiority.  Second, nationalism helped lead to WWI because it caused conflict.  Archduke Franz Ferdinand was assassinated because of nationalism.  He was an ethnic German ruling an empire that included ethnic Slavs.  Slavic nationalists did not like being ruled by Germans and killed the archduke in an attempt to help rid themselves of German rule.  This sort of ethnic conflict helped to cause WWI.

Thus, both nationalism and imperialism were important factors in bringing WWI about. Nationalism reached a fevered pitch in Europe prior to the first World War. As a political tool, Nationalism was the belief that European technological, cultural, economic and military superiority was just cause for the subjugation of more backward cultures and economies. The British believed in the invincibility of their powerful Navy, unlike any other in the world (at the time), and had enjoyed almost two centuries of naval and commercial dominance in the world; the French were convinced their superior fortresses and defense mechanisms were untouchable by their enemies; the Germans trusted in their Schlieffen Plan (a military strategy named after the Chief of the German General Staff in 1905; it was a two prong attack: the first on France to be carried out in less than 6 weeks and then after the defeat of the French, transferring German soldiers to the Russian front). The unsuccessful Schlieffen plan also ushered in the trench warfare of WW1 for which it will always be known. The Germans also had great pride in their submarines and U-Boats. All these European powers were convinced in their own impregnable defenses, as defeat had not humiliated their armies in a long while. Their military strength was second only to their suspicion of each other. Germany considered Britain to be a hypocritical power-hungry nation: while it pressed forward in advancing its own territories, it disputed German colonist ambitions in Africa and Asia. The British in turn, considered the Germans unwelcome competitive warriors in their quest for world dominance. The German belief in conflating military might with cultural superiority was very evidently seen in Nationalistic literary and musical works. Wagner was the quintessential German composer: his nationalistic music was Hitler's favorite and today his music is still associated with that of German fascism.
The final nail in the coffin came from the assassination of Arch Ferdinand by a Serbian nationalist in Bosnia in June 1914. The Austrian Hungarian colonization of the former Turkish (Ottoman Empire) province of Bosnia caused deep anger from the Slavic quarter. The Slavs believed that Bosnia belonged to them. To protect their position, the Austrian-Hungarians made alliances with Germany and Italy to strengthen themselves against Russia (this was called The Triple Alliance) who had its own eye on Serbia . WW1 began with European countries taking sides either for or against Austria-Hungary. The Austrian Hungarians saw their chance to expand territory well into the Balkans; the Russians counteracted by fielding their forces against Austrian audacity and by working to secure access to the Black Sea; Germany was interested in its completion of its Berlin-Baghdad railway. Even the French and British had their eye on the former territories of the failing Ottoman Empire in Eastern Europe.
Meanwhile, Imperialist Britain, the main power of the late 1800's, wished to control global trade between England and the Asian and African continents. The Spanish had colonies in Phillipines and large parts of South America. France had IndoChina and colonies in parts of Africa. The Russians ruled over some Central Asian countries and parts of Europe, notably Finland and Poland. Both nationalism and imperialism were the two brands which fueled the fires of WW1. With the belief in righteous European expansionist policies (nationalism) and in the right of empires to keep colonies in line (imperialism) prominent in the European psyche, it's no wonder that the void left by the weakened Ottoman Empire in the second half of the 1800's led these countries to swoop in and define their power zones, thereby ushering in WW1.
Austria-Hungary hoped to expand into the Balkans; Russia moved to limit Austrian expansion while securing access to the Black Sea; Germany wanted to ensure the security and completion of its Berlin-to-Baghdad railway. Britain and France also had colonial and trade interests in the region. 
 Sources: