While page views are often sold instantly through real-time auctions when users visit websites, they can also be sold in advance via guaranteed contracts. In this paper, we present a dynamic programming model to study how an online publisher should optimally allocate and price page views between guaranteed and spot markets. The problem is challenging because the allocation and pricing of guaranteed contracts affect how advertisers split their purchases between the two markets, and the terminal value of the model is endogenously determined by the updated dual force of supply and demand in auctions. We take the advertisers' purchasing behaviour into consideration, i.e., risk aversion and stochastic demand arrivals, and present a scalable and efficient algorithm for the optimal solution. The model is also empirically validated with a commercial dataset. The experimental results show that selling page views via both channels can increase the publisher's expected total revenue, and the optimal pricing and allocation strategies are robust to different market and advertiser types.