Justin Watt, a Web engineer, was browsing the Web in his room at the Courtyard Marriott in Midtown Manhattan this week when he saw something strange. On his personal blog, a mysterious gap was appearing at the top of the page.After some sleuthing, Mr. Watt, who has a background in developing Web advertising tools, realized that the quirk was not confined to his site. The hotel’s Internet service was secretly injecting lines of code into every page he visited, code that could allow it to insert ads into any Web page without the knowledge of the site visitor or the page’s creator. He did not actually see any such ads.Mr. Watt posted about the discovery on his blog, and that soon spawned a conversation on Hacker News, a discussion site for tech topics, about the ethics of this technique. One commenter described it as “icky,” and another asked, “Why aren’t they putting ads in my pillow?”Mr. Watt had strong feelings about it himself. He said in an interview that he had never seen an Internet provider modifying Web pages that a person visits. “Imagine the U.S.P.S., or FedEx, for that matter, opening your Amazon boxes and injecting ads into the packages,” Mr. Watt said.A test of the Courtyard Marriott’s wireless network on Friday verified Mr. Watt’s claims. The code was embedded in the pages of several Web sites visited, including Reddit, GigaOM and TechMeme.The lines of code include references to “rxg,” which stands for Revenue eXtraction Gateway, a service aimed at generating money from Internet access points. On its Web site, a company called RG Nets, which makes Revenue eXtraction Gateway, explains that its system rewrites every Web page on the fly so that it can include a banner ad. “As you can see, the pervasive nature of the advertising banner on all Web pages guarantees banner advertising impression,” a narrator says in the video.