When, upon a trial in a proceeding pursuant to this chapter, it appears that the plaintiff is entitled to the possession of the premises, judgment and execution for the possession shall be awarded in his favor, with costs; and if the plaintiff becomes nonsuit or fails to prove his right to the possession, the defendant shall have judgment and execution for his costs.