"No nation in the world can be attacked incessantly ... without responding," he said.
McCain, on what he described as a fact-finding mission to the Middle East, expressed empathy for residents of Sderot. The town, situated near the border with Gaza, is a frequent target of Palestinian rocket fire. Israel has responded to the attacks with frequent military raids in Gaza.
McCain, accompanied by U.S. Senator Joe Lieberman, toured the city and spoke to residents of Sderot. McCain thanked Barak and complimented him on his bravery in defending the state of Israel. The presidential hopeful added that the constant rocket fire on Israeli towns underlines the desperate need for a peace agreement between Israel and the Palestinians.
"The fact is that I come from a border state, and if people were rocketing my state, I think that the citizens from my state would advocate a very vigorous response," the Arizona senator told reporters.
McCain also voiced doubts that a peace deal could be reached by the end of the year, saying that "I am not sure whether it will succeed in that period of time," referring to a U.S.-brokered peace process which Washington hopes will lead to a Palestinian statehood accord before George W. Bush leaves the White House in January.
"But I do believe that the administration is making every possible effort to do so," he declared.
When asked about possible negotiations with the Islamist group Hamas, who controls the Gaza Strip, he said that it would be difficult for Israel to negotiate with a group who openly declares its desire to destroy it, but the decision whether or not to attempt negotiations is Israel's.
Earlier Wednesday, Prime Minister Ehud Olmert told McCain that Israel can halt rocket fire from Gaza without a major ground offensive in the densely-populated coastal strip.
"It is possible to stop the Qassams without conquering Gaza," Olmert told McCain. "We will stop the Qassam fire by the creation of deterrence in the South which will make [Gaza militants] think twice before they shoot again," the prime minister continued.
McCain agreed with Olmert that the situation in the South is intolerable.
Answering a question by the presidential hopeful about what was going to happen in Gaza, the prime minister said: "I am skeptical about what appears like a temporary calm, and am doubtful about whether it will continue."
"Israel will not be able to continue suffering Qassam fire and the fact that hundreds of thousands of its residents are living under daily missile fire. In the end, we will stop the Qassam fire," the prime minister said.
Olmert also stressed in the meeting that alongside fighting the rocket attacks, Israel is continuing to build the momentum for a peace process with the moderate elements within the Palestinian Authority.
Earlier Wednesday, McCain told Foreign Minister Tzipi Livni that he believed Palestinian President Mahmoud Abbas was committed to the peace process and opposed the ongoing violence in the Gaza Strip.
At a press conference with Livni, McCain said he had telephoned Abbas, who is based in the West Bank city of Ramallah just a few kilometres away, because he was unable to meet the Palestinian president in person.
McCain said the Western-backed leader wants to move forward in peace talks.
"I again believe that President Abbas wants to get this [peace] process started," McCain said in Jerusalem.
McCain said he shared Israel's concern about the deterioration ofsecurity in the Gaza Strip and the cross-border rocket fire from the Hamas-controlled territory against Israel.
"I believe that [Abbas] does not support the kind of activity that is taking place in Gaza. I know that the United States government is fully committed to trying to stop this ... cross-border violence," McCain said.
A Palestinian official said on Wednesday that McCain told Abbas during their phone conversation that he was committed to a peace settlement after Bush leaves office.
Livni hailed McCain's support for Israel during their early morning meeting.
"I know we share the same understanding on the nature of the threats in the region," she said. I know where you stand on what needs to be done."
McCain was visiting Israel with his Senate allies Democrat-turned-Independent Joe Lieberman of Connecticut and South Carolina Republican Lindsey Graham.
The 71-year-old ex-aviator and Vietnam war POW has denied seeking to improve his electoral prospects on this tour, saying he came as a top member of the Senate Armed Services Committee rather than as Bush's potential successor.
The presidential hopeful expressed concern on Tuesday about Iran's support for terror and its development of nuclear weapons.
Before his arrival in Israel on Tuesday, McCain said he supports Israel's claim to Jerusalem as the capital of the Jewish state. He told reporters in Jordan: "I support Jerusalem as the capital of Israel."
Later, speaking at a meeting with President Shimon Peres in Jerusalem, McCain said he was concerned by Iran's negative influence on the region - namely, the fact that it trains, finances and otherwise assists radical groups - and that his concern had merely been heightened by his current tour of the region.
Peres stressed that Iran is not Israel's problem only. "The combination of terror, nuclear capability and irresponsible leadership is a danger to the entire world," he said. "Even the region's Arab states fear Iran, and they need to say so out loud."
McCain was accompanied by Senators Joe Lieberman and Lindsey Graham. Israeli analysts believe that Lieberman, a former Democrat turned independent who was one of McCain's early backers, is a likely choice for secretary of state should McCain be the next president.