我有一個jsp在我所選擇的標籤,我想獲得 和選擇在JSP中我的servlet發送值Servlet的
<select id="listoffood" name="dropdown" onchange="foodname();">
<option value="bg">Burger</option>
<option value="pas">pasta</option>
<option value="pi">pizza</option>
</select>
<div id='content'></div>
此處選擇的值是JavaScript代碼
function foodname()
{
var xmlHttpReq = false;
var self = this;
document.getElementById('content').innerHtml='';
// Mozilla/Safari
if (window.XMLHttpRequest) {
self.xmlHttpReq = new XMLHttpRequest();
}
// IE
else if (window.ActiveXObject) {
self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
}
self.xmlHttpReq.open('GET', "InformationServlet", true);
self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
self.xmlHttpReq.send(null);
self.xmlHttpReq.onreadystatechange= function()
{
//alert(document.getElementById('content'));
if (self.xmlHttpReq.readyState==4)
{
if (self.xmlHttpReq.status == 200)
{
document.getElementById('content').innerHTML=self.xmlHttpReq.responseText;
}
}
};
}
我所做的使用GET屬性這樣的,但它不是工作的顯示空
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
// TODO Auto-generated method stub
String coun = request.getParameter("dropdown");
PrintWriter out=response.getWriter();
System.out.println("here : "+coun);
}
在此先感謝,並高度讚賞任何代碼。
它會去相同的servlet嗎?發佈完整的html表格 – 2013-05-03 10:50:20
'foodName()'方法做了什麼?你在'form'裏面選擇了嗎? – Kshitij 2013-05-03 10:50:32
#kshitj食品名稱轉到javascript和js,而不是由ajax調用servlet。 @Baadshah是的,它轉到onChange方法上的相同servlet – 2013-05-03 10:52:20