我有一个jsp,其中有选择标签,我想获取
以及从我的Servlet中的jsp中的select中选择的值
<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 Attribute,但它不能正常工作,显示为null
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);
}
在此先感谢您,任何代码都将受到高度赞赏。
最佳答案
只需将您的AJAX open()
请求更改为
var select = document.getElementById("listoffood");
self.xmlHttpReq.open('GET', "InformationServlet?dropdown=" + select.options[select.selectedIndex].value, true);
关于java - 将值从jsp发送到Servlet,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16357111/