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.

An online store selling the hardware to provide this service even lists “Web experience manipulation” as a feature. It is not clear whether the technology is in use at any other Marriott hotels.

The Courtyard Marriott’s marketing director referred inquiries to Marriott’s New York office, where a spokeswoman said she would have to talk to the national office. The automated phone system for RG Nets quickly hung up on calls, and the company did not immediately respond to an e-mail.

Even though this ad-serving system was apparently not serving ads, it was the principle of the thing that upset the online critics. Mr. Watt said that the technique not only affected people browsing the Web, but also the content creators, because they would not get a cut of the revenue and their own ads could be blocked.

“Imagine the hotel delivering complimentary issues of The New York Times to every room, except some articles have been accidentally blacked out, all the ads have been cut out, and on every page there’s a new ad that’s been stuck on top,” he said.