Please note that rooting voids the warranty of your device and I'm not responsible for any damage your device might incur with this tutorial.
Unlocking the bootloader wipes ur data... If u have unlocked ur bootloader this tutorial will not wipe ur data...Back up your data for any case
Steps to Root Xperia P on Android 4.1.2 Jelly Bean
1.Make sure you have unlocked the bootloader of your device from unlockbootloader.sonymobile.com
2.Download the flashable Superuser zip file and place it on your device’s SD card.
3. Download ThP_JB_kernel_v10.2.zip and unzip its contents.
4. You will find a .elf file. Place it in the Folder with ADB Files
***turn off the windows firewall and antivirus softwares installed in the computer.
5. Turn Off your phone, Open command prompt in the location where you have the ADB files and the .elf file ( Hold Shift in the folder and right click- Open Command Promt) and type the command “fastboot flash boot [name of kernel].elf” (without quotes) (if your elf file is named "kernel-unsigned.elf" type this command fastboot flash boot kernel-unsigned.elf ) Press enter in command and then boot your phone into fastboot mode ( To do this, press and hold volume up button and connect it to USB. If you see Blue LED, your device is in fastboot mode )
6. This gives you CWM
7. Unplug USB and reboot your phone. When you see "weird" Sony logo, keep pressing Volume down button continuously(DONT HOLD IT, PRESS IT FEW TIMES) and you will see purple LED and you are now in recovery mode.
8.In CWM, select “install zip from SD card>Choose zip from SD card” and select the zip file you placed in your SD card in Step 2.( In CMW use VOLUME UP and VOLUME DOWN buttons to go "up and down"and POWER Button like "continue"
9. After you flash it, go back and Select “advanced” and choose “wipe dalvik cache”.
Return to main menu. Then select “wipe data factory reset”.
Return and this time choose “wipe cache partition”.
Return and select “reboot system now”.
Your handset should start in Android OS now.